2014-09-23 69 views
3

嘗試爲Yii2設置DbManager。關於php版本有很多線索,但是,對於數據庫版本並不多。yii rbac DbManager設置

我所知道的:

第1步:遷移腳本

./yii migrate [email protected]/rbac/migrations/ 

第2步:配置

... 
'authManager' => [ 
    'class' => 'yii\rbac\DbManager', 
    'defaultRoles' => ['admin', 'user', 'guest'], 
], 
... 

第3步:設置角色/規則

???? 

回答

2
$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

+0

這正是PhpManager的工作原理,你對dbmanager做同樣的事情?這是哪裏去的?在爲phpmanager創建的同一個rbac文件中?如果兩者相同,兩者之間的真正區別是什麼? – 2014-10-06 11:59:25

+0

PhpManager和DbManager實現了一個接口ManagerInterface!看到這個鏈接http://www.yiiframework.com/doc-2.0/yii-rbac-managerinterface.html – 2014-10-07 10:34:52

+0

這是一個偉大的鏈接奧列格。但是我仍然不確定這兩者之間的區別是什麼,如果你以完全相同的方式創建DBManager和PhpManager。 – 2014-10-07 12:26:26