在Yii Rbac扩展名权限中为管理员创建用户分配用户角色

在Yii Rbac扩展名权限中为管理员创建用户分配用户角色

问题描述:

I have installed rights as suggested by most people, its easy to implement Role based access control. but I am stuck at a problem..

I need to assign a user a role from admin, who can change their roles..etc..

As rights doesn't create models for the tables I cant insert in them..and as such there is no documentation, how to do it...

我已经按照大多数人的建议安装了权限,它易于实现基于角色的访问控制。 但我遇到了问题.. p>

我需要为管理员分配一个角色,他可以更改角色..等等.. p> 由于权限没有为表创建模型,我无法在其中插入..因此没有文档,如何做... p> div>

First you need a drop-down of all roles in the system, for admin to select..

<?php
if (Yii::app()->user->isSuperuser) {
       $all_roles=new RAuthItemDataProvider('roles', array( 
    'type'=>2,
    ));
      $data=$all_roles->fetchData();
?>
    <div>
        <label for="type_id">Type</label>
        <?php echo CHtml::dropDownList("Type",'',CHtml::listData($data,'name','name'));?    > 
    </div>
<?php
}
?>

And then on backend you'll need to asign a role to the user based on the Type values selected by the admin...

if(Yii::app()->user->isSuperuser)
    $type=$_POST['Type'];
else
    $type='User';

$authorizer = Yii::app()->getModule("rights")->authorizer;
$authorizer->authManager->assign($type, $model->id);

This code checks if the registration was from admin, then it sets the role selected by the admin, else it sets User as role, ie, same code will work for admin user create and normal signup..

There are such other issues in Rights which are not preperly documented...I have written some in my blog.. My blog post about Yii Rights

I'm not too familiar with Rights - I use rbam extension instead. Have you tried to search your answer in Rights manual?

Well that is actually Yii and not rights

Yii::app()->authManager->assign($role, $userid);

http://www.yiiframework.com/doc/api/1.1/IAuthManager#assign-detail

if ($user->save())
    Rights::assign('Authenticated', $user->id);

"Authenticated" can be replaced with any other role.