Yii User and Rights Implementation

9 followers

This wiki article has not been tagged with a corresponding Yii version yet.
Help us improve the wiki by updating the version information.

implementing some features of yii user and rights.

some tips..

After Installing Yii Users and Rights to Newly Created Yii app we have to assign dynamic roles to a user at the time of user creation .

Create a role manually

first we have to create a role manually and add some operations and/or tasks to the role according to the needs

assume the role created was 'clients'

Giving role

This is very simple , achieved by using two lines of code

save the user(assume $model->id is the user saved/created)

//assign role
$authorizer = Yii::app()->getModule("rights")->getAuthorizer();
$authorizer->authManager->assign('clients', $model->id);

Getting user role

This is also very simple

the signed user id will get using Yii::app()->user->Id

$roles=Rights::getAssignedRoles(Yii::app()->user->Id); // check for single role
foreach($roles as $role)
if($role->name == 'clients')
{
//some actions here ..
$this->redirect(array('/mailbox'));
}

Making some actions public

this should be a common need of an app.

this is also very simple when using yii rights

just use a '-' (minus) operator like

public function filters()
    {
        return array(
            'rights - publicprofile', // perform access control for CRUD operations
 
        );
    }

here the publicprofile action is public, all other actions in the same controller are under rights.

Getting all the roles

getting all the roles in the application

put this line in protected/config/main

'import'=>array(
 
        'application.modules.rights.components.dataproviders.*',
 
    ),

get all roles as a dropdown

<?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
}
?>

Thank you Chris for the awesome module. and thanks mishamx.

Total 4 comments

#14956 report it
Rajith R at 2013/09/24 07:58am
Finding Users with same role

Finding Users with same role

$role = 'role-name' //role name here
$users = AuthAssignment::model()->findAllByAttributes(array('itemname'=>$role));
foreach($users as $user)
{
 echo $user->userid;
}
#13991 report it
Rajith R at 2013/07/11 12:46am
Behaviors

c:qwerty To show user roles you can also use behaviors. In user`s model add code:

public function behaviors()
{
    Yii::import('application.modules.rights.components.behaviors.*');
    return CMap::mergeArray(array(
        'RUserBehavior'=>array(
            'class'=>'RUserBehavior',
            'idColumn'=>'id',
            'nameColumn'=>'name',
        ),
    ),  parent::behaviors());
}

Now you can simply use:

echo $model->getAssignmentsText(CAuthItem::TYPE_ROLE);
#13838 report it
Rajith R at 2013/07/01 04:25am
Secure

i think user+rights is more secure and easy to implement than RABC, and they are error free built-in modules

#13836 report it
alexsergin at 2013/07/01 03:52am
RBAC

Why you don't use RBAC?

Leave a comment

Please to leave your comment.

Write new article