![](/img/trans.png)
[英]Yii2 RBAC DbManager error Call to a member function getRole() on null
[英]yii\rbac\DbManager setup
嘗試為Yii2設置DbManager。 有很多關於php版本的線程,但是,關於數據庫版本並不多。
我所知道的:
第1步:遷移腳本
./yii migrate --migrationPath=@yii/rbac/migrations/
第2步:配置
...
'authManager' => [
'class' => 'yii\rbac\DbManager',
'defaultRoles' => ['admin', 'user', 'guest'],
],
...
第3步:設置角色/規則
????
$auth = Yii::$app->authManager;
// add "createPost" permission
$createPost = $auth->createPermission('createPost');
$createPost->description = 'Create a post';
$auth->add($createPost);
// add "updatePost" permission
$updatePost = $auth->createPermission('updatePost');
$updatePost->description = 'Update post';
$auth->add($updatePost);
// add "author" role and give this role the "createPost" permission
$author = $auth->createRole('author');
$auth->add($author);
$auth->addChild($author, $createPost);
// add "admin" role and give this role the "updatePost" permission
// as well as the permissions of the "author" role
$admin = $auth->createRole('admin');
$auth->add($admin);
$auth->addChild($admin, $updatePost);
$auth->addChild($admin, $author);
// Assign roles to users. 1 and 2 are IDs returned by IdentityInterface::getId()
// usually implemented in your User model.
$auth->assign($author, 2);
$auth->assign($admin, 1);
http://www.yiiframework.com/doc-2.0/guide-security-authorization.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.