- <?php
- /**
- * RBAC Command
- */
- class RbacCommand extends CConsoleCommand
- {
- private $authManager;
- public function getHelp()
- {
- $usage = "USAGE\n"
- . "\trbac\n"
- . "DESCRIPTION\n"
- . "\tThis command creates the basic Structure for Role-based Access Control hierarchy.";
- return $usage;
- }
- public function run($args)
- {
- // ensure that the auth manager is configured properly
- $this->authManager = Yii::app()->authManager;
- if ($this->authManager === null)
- {
- fwrite(STDERR, "Error: The auth manager needs to be configured to use this command.");
- exit(1);
- }
- // remove all previous roles, relationships and operations.
- $this->authManager->clearAll();
- $this->authManager->createOperation('createPost', 'create a post');
- $this->authManager->createOperation('readPost', 'read a post');
- $this->authManager->createOperation('updatePost', 'update a post');
- $this->authManager->createOperation('deletePost', 'delete a post');
- $bizRule = 'return Yii::app()->user->id === $params[post]->author_id';
- $task = $this->authManager->createTask('updateOwnPost', 'update a post by author himself', $bizRule);
- $task->addChild('updatePost');
- $role = $this->createRole('reader');
- $role->addChild('readPost');
- $role = $this->authManager->createRole('author');
- $role->addChild('reader');
- $role->addChild('createPost');
- $role->addChild('updatePost');
- $role = $this->authManager->createRole('editor');
- $role->addChild('reader');
- $role->addChild('updatePost');
- $role = $this->authManager->createRole('admin');
- $role->addChild('editor');
- $role->addChild('author');
- $role->addChild('deletePost');
- fwrite(STDOUT, 'Authorization hierarchy successfully generated.');
- }
- }