Yii2-admin RBAC: Get roles from database and display as Dropdownlist on Signup/User registration

本秂侑毒 提交于 2019-12-11 06:14:13

问题


I am looking for a way to enable an administrator to assign users roles after registering them from the backend. I have configured yii2-admin in yii2 advanced and I have roles already set in the database table.

However I want to get the roles on the user registration form as a dropdown list and the administrator should be able to select a role and assign to the user. The roles on the dropdown list should be those lower than that of the admin or equivalent to his...i.e if there is a sysadmin role that is superuser, the admin should not be able to get the role as one of the options since assigning that role means the user will be higher than his role.

I have searched online but only gotten the code for Yii 1.1 which I tried to customize but does not work at all. The code is provided below:

Dropdown list on the form:

<?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 the controller code is:

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

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

Anyone with an idea of how to transform this to Yii2 ? Please assist; I have been stuck on this problem for some time.

Thank you.


回答1:


Here is an idea on how to proceed with this. Having set up yii2-admin, set up the necessary dbTables and added the authManager settings to your config section like so

$config = [
    ...
    'components' => [
        ...
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
    'as access' => [
        'class' => 'mdm\admin\components\AccessControl',
        'allowActions' => [
            'site/*',
            //'admin/*',
            //'gii/*',
        ]
    ],
];

you have access to the authManager component.

In your controller you can obtain the roles of the current user like so

$current_user_roles = Yii::$app->authManager->getRolesByUser(Yii::$app->user->id);

Next you can get a list of all roles you have defined like so

$available_roles = Yii::$app->authManager->getRoles();

From here you would have to apply your role hierarchy logic to define what roles this user can assign (you should end up with a list of roles this user cannot assign, lets say $forbidden_roles). Once you have these 2 lists that you can remove the $forbidden_roles from the $available roles array with a simple foreach() statement, for example:

foreach($forbidden_roles as $role){
    if(in_array($role,$available_role)){
        $index = array_search($role,$available_role);
        \yii\helpers\ArrayHelper::remove($available_roles,$index);
    }
}

Now you have an array with the roles that the user can assign. Pass this array to your view and subsequently to the dropdown element and you should be set.

I have not personally tried this out but let me know if it works for you. Hope this helps.




回答2:


i use in my app with below code in _form

get load user_id and all name

you only change condition in query

$users= ArrayHelper::map(app\models\User::find()->orderBy('username')->asArray()->all(), 'id', 'username');
$item=  ArrayHelper::map(app\models\Authitem::find()->orderBy('name')->asArray()->all(), 'name', 'name');
    echo $form->field($model, 'item_name')->widget(Select2::classname(), [
        'data' => $item,
        'options' => [
            'placeholder' => 'انتخاب کنید...',
          ],
        'pluginOptions' => [
            'allowClear' => true,
        ],
    ]);
    ?>

    <?php
    echo $form->field($model, 'user_id')->widget(Select2::classname(), [
        'data' => $users,
        'options' => [
            'placeholder' => 'انتخاب کنید...',
          ],
        'pluginOptions' => [
            'allowClear' => true,
        ],
    ]);
    ?>


来源:https://stackoverflow.com/questions/44164746/yii2-admin-rbac-get-roles-from-database-and-display-as-dropdownlist-on-signup-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!