Yii2-admin RBAC:从数据库中获取角色并在注册/用户注册时显示为下拉列表 [英] Yii2-admin RBAC: Get roles from database and display as Dropdownlist on Signup/User registration

查看:19
本文介绍了Yii2-admin RBAC:从数据库中获取角色并在注册/用户注册时显示为下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,使管理员能够在从后端注册用户角色后为其分配角色.我已经在 yii2 advanced 中配置了 yii2-admin 并且我已经在数据库表中设置了角色.

但是我想将用户注册表上的角色作为下拉列表获取,管理员应该能够选择一个角色并分配给用户.下拉列表中的角色应该是低于 admin 的角色或等同于他的角色......即如果有一个超级用户的 sysadmin 角色,那么 admin 应该无法将角色作为选项之一,因为分配该角色意味着用户将高于他的角色.

我在网上搜索过,但只得到了 Yii 1.1 的代码,我尝试自定义但根本不起作用.代码如下:

表单上的下拉列表:

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

<?php}?>

控制器代码为:

if(Yii::app()->user->isSuperuser)$type=$_POST['Type'];别的$type='用户';$authorizer = Yii::app()->getModule("rights")->authorizer;$authorizer->authManager->assign($type, $model->id);

有人知道如何将其转换为 Yii2 吗?请协助;我已经被这个问题困住了一段时间.

谢谢.

解决方案

这里有一个关于如何处理这个问题的想法.设置 yii2-admin 后,设置必要的 dbTables 并将 authManager 设置添加到您的配置部分,如下所示

$config = [...'组件' =>[...'authManager' =>['类' =>'yii\rbac\DbManager',],],'作为访问' =>['类' =>'mdm\admin\components\AccessControl','allowActions' =>['地点/*',//'行政/*',//'gii/*',]],];

您可以访问 authManager 组件.

在您的控制器中,您可以像这样获取当前用户的角色

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

接下来你可以得到你定义的所有角色的列表

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

从这里开始,您必须应用您的角色层次结构逻辑来定义该用户可以分配的角色(您最终应该得到该用户无法分配的角色列表,比如 $forbidden_​​roles).获得这两个列表后,您可以使用简单的 foreach() 语句从 $available 角色数组中删除 $forbidden_​​roles,例如:

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

现在您有一个包含用户可以分配的角色的数组.将此数组传递给您的视图,然后传递给下拉元素,您应该被设置.

我没有亲自尝试过,但请告诉我它是否适合您.希望这会有所帮助.

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.

解决方案

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.

这篇关于Yii2-admin RBAC:从数据库中获取角色并在注册/用户注册时显示为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆