RoleController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace common\modules\rbac\controllers;
  3. use common\modules\rbac\components\MenuHelper;
  4. use common\modules\rbac\models\AuthItem;
  5. use common\modules\rbac\models\searchs\AuthItem as AuthItemSearch;
  6. use Yii;
  7. use yii\filters\VerbFilter;
  8. use yii\helpers\ArrayHelper;
  9. use yii\rbac\Item;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. /**
  13. * AuthItemController implements the CRUD actions for AuthItem model.
  14. *
  15. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  16. *
  17. * @since 1.0
  18. */
  19. class RoleController extends Controller
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function behaviors()
  25. {
  26. return [
  27. 'verbs' => [
  28. 'class' => VerbFilter::className(),
  29. 'actions' => [
  30. 'delete' => ['post'],
  31. ],
  32. ],
  33. ];
  34. }
  35. /**
  36. * Lists all AuthItem models.
  37. *
  38. * @return mixed
  39. */
  40. public function actionIndex()
  41. {
  42. $searchModel = new AuthItemSearch(['type' => Item::TYPE_ROLE]);
  43. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
  44. return $this->render('index', [
  45. 'dataProvider' => $dataProvider,
  46. 'searchModel' => $searchModel,
  47. ]);
  48. }
  49. /**
  50. * Displays a single AuthItem model.
  51. *
  52. * @param string $id
  53. *
  54. * @return mixed
  55. */
  56. public function actionView($id)
  57. {
  58. $model = $this->findModel($id);
  59. $permissions = Yii::$app->authManager->getPermissions();
  60. unset($permissions['/*']);
  61. $permissions = array_keys($permissions);
  62. $routeGroups = [];
  63. foreach ($permissions as $permission) {
  64. $pos = strrpos($permission, '/');
  65. $controller = substr($permission, 0, $pos);
  66. $action = substr($permission, $pos+1);
  67. $routeGroups[$controller][] = $action;
  68. }
  69. return $this->render('view', [
  70. 'model' => $model,
  71. 'routeGroups' => $routeGroups
  72. ]);
  73. }
  74. /**
  75. * Creates a new AuthItem model.
  76. * If creation is successful, the browser will be redirected to the 'view' page.
  77. *
  78. * @return mixed
  79. */
  80. public function actionCreate()
  81. {
  82. $model = new AuthItem(null);
  83. $model->type = Item::TYPE_ROLE;
  84. if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
  85. MenuHelper::invalidate();
  86. return $this->redirect(['view', 'id' => $model->name]);
  87. } else {
  88. return $this->render('create', ['model' => $model]);
  89. }
  90. }
  91. /**
  92. * Updates an existing AuthItem model.
  93. * If update is successful, the browser will be redirected to the 'view' page.
  94. *
  95. * @param string $id
  96. *
  97. * @return mixed
  98. */
  99. public function actionUpdate($id)
  100. {
  101. $model = $this->findModel($id);
  102. if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
  103. MenuHelper::invalidate();
  104. return $this->redirect(['index']);
  105. }
  106. return $this->render('update', ['model' => $model]);
  107. }
  108. /**
  109. * Deletes an existing AuthItem model.
  110. * If deletion is successful, the browser will be redirected to the 'index' page.
  111. *
  112. * @param string $id
  113. *
  114. * @return mixed
  115. */
  116. public function actionDelete($id)
  117. {
  118. $model = $this->findModel($id);
  119. Yii::$app->getAuthManager()->remove($model->item);
  120. MenuHelper::invalidate();
  121. return $this->redirect(['index']);
  122. }
  123. /**
  124. * Assign or remove items.
  125. *
  126. * @param string $id
  127. * @param string $action
  128. *
  129. * @return array
  130. */
  131. public function actionAssign()
  132. {
  133. $post = Yii::$app->getRequest()->post();
  134. $id = $post['id'];
  135. $action = $post['action'];
  136. $roles = $post['roles'];
  137. $manager = Yii::$app->getAuthManager();
  138. $parent = $manager->getRole($id);
  139. $error = [];
  140. if ($action == 'assign') {
  141. foreach ($roles as $role) {
  142. $child = $manager->getRole($role);
  143. $child = $child ?: $manager->getPermission($role);
  144. try {
  145. $manager->addChild($parent, $child);
  146. } catch (\Exception $e) {
  147. $error[] = $e->getMessage();
  148. }
  149. }
  150. } else {
  151. foreach ($roles as $role) {
  152. $child = $manager->getRole($role);
  153. $child = $child ?: $manager->getPermission($role);
  154. try {
  155. $manager->removeChild($parent, $child);
  156. } catch (\Exception $e) {
  157. $error[] = $e->getMessage();
  158. }
  159. }
  160. }
  161. MenuHelper::invalidate();
  162. Yii::$app->response->format = 'json';
  163. return[
  164. 'type' => 'S',
  165. 'errors' => $error,
  166. ];
  167. }
  168. /**
  169. * Search role.
  170. *
  171. * @param string $id
  172. * @param string $target
  173. * @param string $term
  174. *
  175. * @return array
  176. */
  177. public function actionSearch($id, $target, $term = '')
  178. {
  179. $result = [
  180. 'Roles' => [],
  181. 'Permissions' => [],
  182. 'Routes' => [],
  183. ];
  184. $authManager = Yii::$app->authManager;
  185. if ($target == 'avaliable') {
  186. $children = array_keys($authManager->getChildren($id));
  187. $children[] = $id;
  188. foreach ($authManager->getRoles() as $name => $role) {
  189. if (in_array($name, $children)) {
  190. continue;
  191. }
  192. if (empty($term) or strpos($name, $term) !== false) {
  193. $result['Roles'][$name] = $name;
  194. }
  195. }
  196. foreach ($authManager->getPermissions() as $name => $role) {
  197. if (in_array($name, $children)) {
  198. continue;
  199. }
  200. if (empty($term) or strpos($name, $term) !== false) {
  201. $result[$name[0] === '/' ? 'Routes' : 'Permissions'][$name] = $name;
  202. }
  203. }
  204. } else {
  205. foreach ($authManager->getChildren($id) as $name => $child) {
  206. if (empty($term) or strpos($name, $term) !== false) {
  207. if ($child->type == Item::TYPE_ROLE) {
  208. $result['Roles'][$name] = $name;
  209. } else {
  210. $result[$name[0] === '/' ? 'Routes' : 'Permissions'][$name] = $name;
  211. }
  212. }
  213. }
  214. }
  215. Yii::$app->response->format = 'json';
  216. return array_filter($result);
  217. }
  218. /**
  219. * Finds the AuthItem model based on its primary key value.
  220. * If the model is not found, a 404 HTTP exception will be thrown.
  221. *
  222. * @param string $id
  223. *
  224. * @return AuthItem the loaded model
  225. *
  226. * @throws HttpException if the model cannot be found
  227. */
  228. protected function findModel($id)
  229. {
  230. $item = Yii::$app->getAuthManager()->getRole($id);
  231. if ($item) {
  232. return new AuthItem($item);
  233. } else {
  234. throw new NotFoundHttpException('The requested page does not exist.');
  235. }
  236. }
  237. }