模块yii2的独立rbac设置

时间:2017-05-16 09:57:59

标签: module yii2 rbac

我的yii2-basic应用程序中有两个模块。对于普通用户,我有"用户"表和superadmin用户我有第二个" superadmin"表。我的应用中有两个用户拥有不同的会话 - 用户和超级用户。我需要为superadmin用户表使用不同的独立rbac。是否可以在两个模块中设置两个不同的rbac?现在它在全局的web.php文件中设置。也许有可能以某种方式单独在模块中设置authManager,而不是在全局web.php文件中? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

我明白了!在你的module.php文件中指定具有不同表名的authManager,你应该在之前创建它们。如果您也想使用它,也可以指定mdm \ admin的rbac子模块。在下面的代码中指定另一个usertable - SuperAdmin。

; WITH DuplicateKanji AS -- Query for duplicate values
(
    SELECT 
        Kanji 
    FROM Phrase
    GROUP BY Kanji
    HAVING COUNT(*)>1
)
SELECT -- Query to retrieve rows that were duplicates from above query
    p.*
FROM DuplicateKanji dk
    INNER JOIN Phrase p
    ON dk.Kanji = p.Kanji

重写的AccessControl类:

   public function init()
{
    parent::init();
     $this->modules = [
    'rbac' => [
       'class' => 'mdm\admin\Module',
       'controllerMap' => [
             'assignment' => [
                'class' => 'mdm\admin\controllers\AssignmentController',
                'userClassName' => 'app\models\SuperAdmin', 
                'idField' => 'id',
                'usernameField' => 'username',
            ],
        ],
    'layout' => 'left-menu',
    'mainLayout' => '@app/views/layouts/rbac.php',
    ],
    ];

 $config = [
  'components' => [
    'authManager' => [
        'class' => 'yii\rbac\DbManager',
         'itemTable' => 'superadmin_auth_item',
         'assignmentTable' => 'superadmin_auth_assignment',
         'itemChildTable' => 'superadmin_auth_item_child',
         'ruleTable' => 'superadmin_auth_rule',
    ],
],
    'as access' => [
    'class' => 'app\components\SuperAdminAccessControl',//your overridden AccessControl class
    'allowActions' => [
        'admin/rbac/*',
        'admin/default/login',
        'admin/default/logout',
    ]
   ],
 ];
     \Yii::$app->authManager->itemTable = 'superadmin_auth_item';
     \Yii::$app->authManager->assignmentTable = 'superadmin_auth_assignment';
     \Yii::$app->authManager->itemChildTable = 'superadmin_auth_item_child';
     \Yii::$app->authManager->ruleTable = 'superadmin_auth_rule';

     \Yii::configure(\Yii::$app, $config);

  }