Question

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...

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

"Authenticated" can be replaced with any other role.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top