Advertisement
mint

RbacCommand.php

Jun 26th, 2012
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2. class RbacCommand extends CConsoleCommand
  3. {
  4. private $_authManager;
  5. public function getHelp()
  6. {
  7. return <<<EOD
  8. USAGE
  9. rbac
  10. DESCRIPTION
  11. This command generates an initial RBAC authorization hierarchy.
  12. EOD;
  13. }
  14. /**
  15. * Execute the action.
  16. * @param array command line parameters specific for this command
  17. */
  18. public function run($args)
  19. {
  20. //ensure that an authManager is defined as this is mandatory for creating an auth heirarchy
  21. if(($this->_authManager=Yii::app()->authManager)===null)
  22. {
  23. echo "Error: an authorization manager, named 'authManager'
  24. must be configured to use this command.\n";
  25. echo "If you already added 'authManager' component in
  26. application configuration,\n";
  27. echo "please quit and re-enter the yiic shell.\n";
  28. return;
  29. }
  30. //provide the oportunity for the use to abort the request
  31. echo "This command will create three roles: superadmin, admin1, and
  32. normaluser and the following premissions:\n";
  33. echo "create, read, update and delete user\n";
  34. echo "Would you like to continue? [Yes|No] ";
  35. //check the input from the user and continue if they indicated yes to the above question
  36. if(!strncasecmp(trim(fgets(STDIN)),'y',1))
  37. {
  38. //first we need to remove all operations, roles, child relationship and assignments
  39. $this->_authManager->clearAll();
  40. //create the lowest level operations for users
  41. $this->_authManager->createOperation("createUser","create a new user");
  42. $this->_authManager->createOperation("readUser","read user profile information");
  43. $this->_authManager->createOperation("updateUser","update a users information");
  44. $this->_authManager->createOperation("deleteUser","remove a user");
  45.  
  46. $role=$this->_authManager->createRole("normaluser");
  47. $role->addChild("readUser");
  48.  
  49. $role=$this->_authManager->createRole("admin1");
  50. $role->addChild("normaluser");
  51. $role->addChild("createUser");
  52. $role->addChild("updateUser");
  53.  
  54. $role=$this->_authManager->createRole("superadmin");
  55. $role->addChild("normaluser");
  56. $role->addChild("admin1");
  57. $role->addChild("createUser");
  58. $role->addChild("updateUser");
  59. $role->addChild("deleteUser");
  60. //provide a message indicating success
  61. echo "Authorization hierarchy successfully generated.";
  62. }
  63. }
  64. }
  65. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement