Advertisement
Guest User

Untitled

a guest
Oct 11th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 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.          * Clean all  privilges
  17.          */
  18.         $auth->removeAll();
  19.  
  20.         /*
  21.          * Create Permision
  22.          */
  23.  
  24.         $createPost = $auth->createPermission('createPost');
  25.         $createPost->description = 'can create post';
  26.         $auth->add($createPost);
  27.  
  28.         $updatePost = $auth->createPermission('updatePost');
  29.         $updatePost->description = 'can update post';
  30.         $auth->add($updatePost);
  31.  
  32.         $deletePost = $auth->createPermission('deletePost');
  33.         $deletePost->description = 'can delete post';
  34.         $auth->add($deletePost);
  35.  
  36.         /*
  37.          * category permission
  38.          */
  39.  
  40.         $createCategory = $auth->createPermission('createCategory');
  41.         $createCategory->description = 'can create Category';
  42.         $auth->add($createCategory);
  43.  
  44.         $updateCategory = $auth->createPermission('updateCategory');
  45.         $updateCategory->description = 'can update Category';
  46.         $auth->add($updateCategory);
  47.  
  48.         $deleteCategory = $auth->createPermission('deleteCategory');
  49.         $deleteCategory->description = 'can delete Category';
  50.         $auth->add($deleteCategory);
  51.  
  52.  
  53.         /*
  54.          * Create Role
  55.          */
  56.         $Admin = $auth->createRole('Admin');
  57.         $auth->add($Admin);
  58.  
  59.         $Author = $auth->createRole('Author');
  60.         $auth->add($Author);
  61.  
  62.         /*
  63.          * Assigment
  64.          */
  65.  
  66.         $auth->addChild($Admin, $Author);
  67.         $auth->addChild($Admin, $updatePost);
  68.         $auth->addChild($Admin, $deletePost);
  69.         $auth->addChild($Admin, $updateCategory);
  70.         $auth->addChild($Admin, $deleteCategory);
  71.         $auth->addChild($Author, $createPost);
  72.         $auth->addChild($Author, $createCategory);
  73.  
  74.  
  75.  
  76.         $auth = Yii::$app->authManager;
  77.  
  78.  
  79.  
  80.     }
  81.  
  82.     public  function actionRoleOwnAuthor()
  83.     {
  84.         $auth = Yii::$app->authManager;
  85.  
  86.         // add the rule
  87.         $rule = new AuthorRule();
  88.  
  89.         $auth->add($rule);
  90.  
  91.         // add the "updateOwnPost" permission and associate the rule with it.
  92.         $updateOwnPost = $auth->createPermission('updateOwnPost');
  93.         $updateOwnPost->description = 'Update own post';
  94.         $updateOwnPost->ruleName = $rule->name;
  95.         $auth->add($updateOwnPost);
  96.  
  97.         // get the "updatePost" permission
  98.         $updatePost = $auth->getPermission('updatePost');
  99.  
  100.         // get the "author" role
  101.         $author = $auth->getRole('Author');
  102.  
  103.         // "updateOwnPost" will be used from "updatePost"
  104.         $auth->addChild($updateOwnPost, $updatePost);
  105.  
  106.         // allow "author" to update their own posts
  107.         $auth->addChild($author, $updateOwnPost);
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement