Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.99 KB  |  hits: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4. * RBAC Command
  5. */
  6. class RbacCommand extends CConsoleCommand
  7. {
  8.     private $authManager;
  9.  
  10.     public function getHelp()
  11.     {
  12.         $usage = "USAGE\n"
  13.                . "\trbac\n"
  14.                . "DESCRIPTION\n"
  15.                . "\tThis command creates the basic Structure for Role-based Access Control hierarchy.";
  16.  
  17.         return $usage;
  18.     }
  19.  
  20.     public function run($args)
  21.     {
  22.         // ensure that the auth manager is configured properly
  23.         $this->authManager = Yii::app()->authManager;
  24.  
  25.         if ($this->authManager === null)
  26.         {
  27.             fwrite(STDERR, "Error: The auth manager needs to be configured to use this command.");
  28.             exit(1);
  29.         }
  30.  
  31.         // remove all previous roles, relationships and operations.
  32.         $this->authManager->clearAll();
  33.  
  34.         $this->authManager->createOperation('createPost', 'create a post');
  35.         $this->authManager->createOperation('readPost', 'read a post');
  36.         $this->authManager->createOperation('updatePost', 'update a post');
  37.         $this->authManager->createOperation('deletePost', 'delete a post');
  38.  
  39.         $bizRule = 'return Yii::app()->user->id === $params[post]->author_id';
  40.         $task = $this->authManager->createTask('updateOwnPost', 'update a post by author himself', $bizRule);
  41.         $task->addChild('updatePost');
  42.  
  43.         $role = $this->createRole('reader');
  44.         $role->addChild('readPost');
  45.  
  46.         $role = $this->authManager->createRole('author');
  47.         $role->addChild('reader');
  48.         $role->addChild('createPost');
  49.         $role->addChild('updatePost');
  50.  
  51.         $role = $this->authManager->createRole('editor');
  52.         $role->addChild('reader');
  53.         $role->addChild('updatePost');
  54.  
  55.         $role = $this->authManager->createRole('admin');
  56.         $role->addChild('editor');
  57.         $role->addChild('author');
  58.         $role->addChild('deletePost');
  59.  
  60.         fwrite(STDOUT, 'Authorization hierarchy successfully generated.');
  61.     }
  62. }