Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2. namespace console\controllers;
  3.  
  4.  
  5. use console\rbac\AuthorRule;
  6. use Yii;
  7. use yii\console\Controller;
  8.  
  9.  
  10. class RbacController extends Controller
  11. {
  12.     public function actionInit()
  13.     {
  14.         $auth = Yii::$app->authManager;
  15.  
  16.         /**
  17.          * Permissions
  18.          */
  19.  
  20.         // create and add "createPost" permission
  21.         $createPost = $auth->createPermission('createPost');
  22.         $createPost->description = 'User can create a post';
  23.         $auth->add($createPost);
  24.  
  25.         // create and add "updatePost" permission
  26.         $updatePost = $auth->createPermission('updatePost');
  27.         $updatePost->description = 'User can update post';
  28.         $auth->add($updatePost);
  29.  
  30.  
  31.  
  32.         /**
  33.          * Roles
  34.          */
  35.  
  36.         // create and add "user" role
  37.         $user = $auth->createRole('user');
  38.         $auth->add($user);
  39.  
  40.         // create and add "author" role
  41.         $author = $auth->createRole('author');
  42.         $auth->add($author);
  43.  
  44.         // create and add "admin" role
  45.         $admin = $auth->createRole('admin');
  46.         $auth->add($admin);
  47.  
  48.         /**
  49.          * Mutual connections
  50.          */
  51.  
  52.         // "author" can create new Post
  53.         $auth->addChild($author, $createPost);
  54.  
  55.         // "admin" can do everything what "author" can
  56.         $auth->addChild($admin, $author);
  57.         // ... and ...
  58.         // "admin" can update ALL Posts
  59.         $auth->addChild($admin, $updatePost);
  60.  
  61.         $auth = Yii::$app->authManager;
  62.  
  63. // add the rule
  64.         $rule = new AuthorRule();
  65.         $auth->add($rule);
  66.  
  67. // add the "updateOwnPost" permission and associate the rule with it.
  68.         $updateOwnPost = $auth->createPermission('updateOwnPost');
  69.         $updateOwnPost->description = 'Update own post';
  70.         $updateOwnPost->ruleName = $rule->name;
  71.         $auth->add($updateOwnPost);
  72.  
  73. // "updateOwnPost" will be used from "updatePost"
  74.         $auth->addChild($updateOwnPost, $updatePost);
  75.  
  76. // allow "author" to update their own posts
  77.         $auth->addChild($author, $updateOwnPost);
  78.  
  79.     }
  80.  
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement