Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace console\controllers;
- use console\rbac\AuthorRule;
- use Yii;
- use yii\console\Controller;
- class RbacController extends Controller
- {
- public function actionInit()
- {
- $auth = Yii::$app->authManager;
- /**
- * Clean all privilges
- */
- $auth->removeAll();
- /*
- * Create Permision
- */
- $createPost = $auth->createPermission('createPost');
- $createPost->description = 'can create post';
- $auth->add($createPost);
- $updatePost = $auth->createPermission('updatePost');
- $updatePost->description = 'can update post';
- $auth->add($updatePost);
- $deletePost = $auth->createPermission('deletePost');
- $deletePost->description = 'can delete post';
- $auth->add($deletePost);
- /*
- * category permission
- */
- $createCategory = $auth->createPermission('createCategory');
- $createCategory->description = 'can create Category';
- $auth->add($createCategory);
- $updateCategory = $auth->createPermission('updateCategory');
- $updateCategory->description = 'can update Category';
- $auth->add($updateCategory);
- $deleteCategory = $auth->createPermission('deleteCategory');
- $deleteCategory->description = 'can delete Category';
- $auth->add($deleteCategory);
- /*
- * Create Role
- */
- $Admin = $auth->createRole('Admin');
- $auth->add($Admin);
- $Author = $auth->createRole('Author');
- $auth->add($Author);
- /*
- * Assigment
- */
- $auth->addChild($Admin, $Author);
- $auth->addChild($Admin, $updatePost);
- $auth->addChild($Admin, $deletePost);
- $auth->addChild($Admin, $updateCategory);
- $auth->addChild($Admin, $deleteCategory);
- $auth->addChild($Author, $createPost);
- $auth->addChild($Author, $createCategory);
- $auth = Yii::$app->authManager;
- }
- public function actionRoleOwnAuthor()
- {
- $auth = Yii::$app->authManager;
- // add the rule
- $rule = new AuthorRule();
- $auth->add($rule);
- // add the "updateOwnPost" permission and associate the rule with it.
- $updateOwnPost = $auth->createPermission('updateOwnPost');
- $updateOwnPost->description = 'Update own post';
- $updateOwnPost->ruleName = $rule->name;
- $auth->add($updateOwnPost);
- // get the "updatePost" permission
- $updatePost = $auth->getPermission('updatePost');
- // get the "author" role
- $author = $auth->getRole('Author');
- // "updateOwnPost" will be used from "updatePost"
- $auth->addChild($updateOwnPost, $updatePost);
- // allow "author" to update their own posts
- $auth->addChild($author, $updateOwnPost);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement