我试图在我的项目中使用Yii2高级应用程序版本配置rbac与phpmanager。但是'Yii::$app->user->can没有返回预期的。
我编写了RbacController并成功执行了yii rbac/init
更新后的common/components/items.php如下所示
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => 'create a X',],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX => [ 'type' => 2, 'description' => 'update a X', ],
];
在SignupForm::signup中,我添加了:
$auth = Yii::$app->authManager;
$roleObj = $auth->getRole('user'); // this role is defined by the RBAC Controller's init action
$auth->assign($roleObj, $user->getId());
assignments.php
return [ 2 => [ 'user', ], ];
我认为2对应用户id。
rules.php
return [];
常见/main.php
...
'components' => [
...
'authManager' => [
'class' => 'yii'rbac'PhpManager',
'defaultRoles' => ['user','admin'],
'itemFile' => '@common/components/rbac/items.php',
'assignmentFile' => '@common/components/rbac/assignments.php',
'ruleFile' => '@common/components/rbac/rules.php'
],
],
...
当我获得角色权限时,它打印:
Array ([createX] => yii'rbac'Permission Object ([type] => 2 [name] => createX [description] => create a X [ruleName] => [data] => [createdAt] => 1438601819 [updatedAt] => 1438601819)
我希望用户没有更新权限,但在方法XController::update
中echo 'Yii::$app->user->can('updateX');
// returns 1, just the same than 'Yii::$app->user->can('createX') returning
请帮忙
第一项文件应为:
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => [ 'create a X', ], ],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX' => [ 'type' => 2, 'description' => ['update a X', ], ],
];
您可以使用var_dump:
验证代码是否正常。var_dump('Yii::$app->authManager);