Guest User

Untitled

a guest
Sep 29th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <?php
  2.  
  3. namespace consolecontrollers;
  4.  
  5. use commonmodelsUser;
  6. use Yii;
  7. use yiiconsoleController;
  8.  
  9. class RbacController extends Controller
  10. {
  11. public function actionInit()
  12. {
  13. $auth = Yii::$app->authManager;
  14.  
  15. //CREATE CONTENT MANAGER ROLE
  16. $contentManagerRole = Yii::$app->authManager->createRole('content_manager');
  17. $contentManagerRole->description = 'Content manager';
  18.  
  19. //CREATE ADMIN ROLE
  20. $adminRole = Yii::$app->authManager->createRole('admin');
  21. $adminRole->description = 'Administrator';
  22. $auth->add($adminRole);
  23.  
  24.  
  25.  
  26. //COMPLAIN PERMISSIONS
  27. $indexComplain = $auth->createPermission('complain-index');
  28. $viewComplain = $auth->createPermission('complain-view');
  29. $createComplain = $auth->createPermission('complain-create');
  30. $updateComplain = $auth->createPermission('complain-update');
  31. $deleteComplain = $auth->createPermission('complain-delete');
  32.  
  33. //ANNOUNCEMENT PERMISSIONS
  34. $indexAnnouncement = $auth->createPermission('announcement-index');
  35. $viewAnnouncement = $auth->createPermission('announcement-view');
  36. $createAnnouncement = $auth->createPermission('announcement-create');
  37. $updateAnnouncement = $auth->createPermission('announcement-update');
  38. $deleteAnnouncement = $auth->createPermission('announcement-delete');
  39.  
  40. $auth->add($indexComplain);
  41. $auth->add($viewComplain);
  42. $auth->add($createComplain);
  43. $auth->add($updateComplain);
  44. $auth->add($deleteComplain);
  45.  
  46. $auth->add($indexAnnouncement);
  47. $auth->add($viewAnnouncement);
  48. $auth->add($createAnnouncement);
  49. $auth->add($updateAnnouncement);
  50. $auth->add($deleteAnnouncement);
  51.  
  52.  
  53. $auth->add($contentManagerRole);
  54.  
  55.  
  56. //ADDING PERMISSIONS IN ROLE CONTENT MANAGER
  57. $auth->addChild($contentManagerRole, $indexComplain);
  58. $auth->addChild($contentManagerRole, $viewComplain);
  59. $auth->addChild($contentManagerRole, $createComplain);
  60. $auth->addChild($contentManagerRole, $updateComplain);
  61. $auth->addChild($contentManagerRole, $deleteComplain);
  62.  
  63.  
  64. $auth->addChild($contentManagerRole, $indexAnnouncement);
  65. $auth->addChild($contentManagerRole, $viewAnnouncement);
  66. $auth->addChild($contentManagerRole, $createAnnouncement);
  67. $auth->addChild($contentManagerRole, $updateAnnouncement);
  68. $auth->addChild($contentManagerRole, $deleteAnnouncement);
  69.  
  70.  
  71. $auth->addChild($adminRole, $contentManagerRole);
  72.  
  73.  
  74.  
  75. $auth->assign($contentManagerRole, User::getContentManagerUser()->id);
  76. $auth->assign($adminRole, User::getAdminUser()->id);
  77. }
  78. }
  79.  
  80. 'as beforeRequest' => [
  81. 'class' => 'yiifiltersAccessControl',
  82. 'rules' => [
  83.  
  84. //COMMON
  85. [
  86. 'actions' => ['logout', 'index'],
  87. 'allow' => true,
  88. 'roles' => ['admin', 'content_manager'],
  89. ],
  90.  
  91. //ADMIN
  92. [
  93. 'allow' => true,
  94. 'roles' => ['admin'],
  95. ],
  96.  
  97. //CONTENT MANAGER
  98. [
  99. 'allow' => true,
  100. 'roles' => ['content_manager'],
  101. ],
  102.  
  103. [
  104. 'actions' => ['login'],
  105. 'allow' => true,
  106. 'roles' => ['?']
  107. ],
  108.  
  109. ],
  110. ],
  111.  
  112. 'access' => [
  113. 'class' => AccessControl::className(),
  114. 'rules' => [
  115. [
  116. 'allow' => true,
  117. 'actions' => ['index'],
  118. 'roles' => ['complain-index'],
  119. ],
  120. [
  121. 'allow' => true,
  122. 'actions' => ['view'],
  123. 'roles' => ['complain-view'],
  124. ],
  125. [
  126. 'allow' => true,
  127. 'actions' => ['create'],
  128. 'roles' => ['complain-create'],
  129. ],
  130. [
  131. 'allow' => true,
  132. 'actions' => ['update'],
  133. 'roles' => ['complain-update'],
  134. ],
  135. [
  136. 'allow' => true,
  137. 'actions' => ['delete'],
  138. 'roles' => ['complain-delete'],
  139. ],
  140. ],
  141. ],
Add Comment
Please, Sign In to add comment