PermissionController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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\rbac\Item;
  9. use yii\web\Controller;
  10. use yii\web\NotFoundHttpException;
  11. use yii\web\Response;
  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 PermissionController 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_PERMISSION]);
  43. $dataProvider = $searchModel->search(Yii::$app->getRequest()->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. return $this->render('view', ['model' => $model]);
  60. }
  61. /**
  62. * Creates a new AuthItem model.
  63. * If creation is successful, the browser will be redirected to the 'view' page.
  64. *
  65. * @return mixed
  66. */
  67. public function actionCreate()
  68. {
  69. $model = new AuthItem(null);
  70. $model->type = Item::TYPE_PERMISSION;
  71. if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
  72. MenuHelper::invalidate();
  73. return $this->redirect(['view', 'id' => $model->name]);
  74. } else {
  75. return $this->render('create', ['model' => $model]);
  76. }
  77. }
  78. /**
  79. * Updates an existing AuthItem model.
  80. * If update is successful, the browser will be redirected to the 'view' page.
  81. *
  82. * @param string $id
  83. *
  84. * @return mixed
  85. */
  86. public function actionUpdate($id)
  87. {
  88. $model = $this->findModel($id);
  89. if ($model->load(Yii::$app->getRequest()->post()) && $model->save()) {
  90. MenuHelper::invalidate();
  91. return $this->redirect(['view', 'id' => $model->name]);
  92. }
  93. return $this->render('update', ['model' => $model]);
  94. }
  95. /**
  96. * Deletes an existing AuthItem model.
  97. * If deletion is successful, the browser will be redirected to the 'index' page.
  98. *
  99. * @param string $id
  100. *
  101. * @return mixed
  102. */
  103. public function actionDelete($id)
  104. {
  105. $model = $this->findModel($id);
  106. Yii::$app->getAuthManager()->remove($model->item);
  107. MenuHelper::invalidate();
  108. return $this->redirect(['index']);
  109. }
  110. /**
  111. * Assign or remove items.
  112. *
  113. * @param string $id
  114. * @param string $action
  115. *
  116. * @return array
  117. */
  118. public function actionAssign()
  119. {
  120. $post = Yii::$app->getRequest()->post();
  121. $id = $post['id'];
  122. $action = $post['action'];
  123. $roles = $post['roles'];
  124. $manager = Yii::$app->getAuthManager();
  125. $parent = $manager->getPermission($id);
  126. $error = [];
  127. if ($action == 'assign') {
  128. foreach ($roles as $role) {
  129. $child = $manager->getPermission($role);
  130. try {
  131. $manager->addChild($parent, $child);
  132. } catch (\Exception $exc) {
  133. $error[] = $exc->getMessage();
  134. }
  135. }
  136. } else {
  137. foreach ($roles as $role) {
  138. $child = $manager->getPermission($role);
  139. try {
  140. $manager->removeChild($parent, $child);
  141. } catch (\Exception $exc) {
  142. $error[] = $exc->getMessage();
  143. }
  144. }
  145. }
  146. MenuHelper::invalidate();
  147. Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  148. return[
  149. 'type' => 'S',
  150. 'errors' => $error,
  151. ];
  152. }
  153. /**
  154. * Search role.
  155. *
  156. * @param string $id
  157. * @param string $target
  158. * @param string $term
  159. *
  160. * @return array
  161. */
  162. public function actionSearch($id, $target, $term = '')
  163. {
  164. $result = [
  165. 'Permission' => [],
  166. 'Routes' => [],
  167. ];
  168. $authManager = Yii::$app->getAuthManager();
  169. if ($target == 'avaliable') {
  170. $children = array_keys($authManager->getChildren($id));
  171. $children[] = $id;
  172. foreach ($authManager->getPermissions() as $name => $role) {
  173. if (in_array($name, $children)) {
  174. continue;
  175. }
  176. if (empty($term) or strpos($name, $term) !== false) {
  177. $result[$name[0] === '/' ? 'Routes' : 'Permissions'][$name] = $name;
  178. }
  179. }
  180. } else {
  181. foreach ($authManager->getChildren($id) as $name => $child) {
  182. if (empty($term) or strpos($name, $term) !== false) {
  183. $result[$name[0] === '/' ? 'Routes' : 'Permissions'][$name] = $name;
  184. }
  185. }
  186. }
  187. Yii::$app->getResponse()->format = Response::FORMAT_JSON;
  188. return array_filter($result);
  189. }
  190. /**
  191. * Finds the AuthItem model based on its primary key value.
  192. * If the model is not found, a 404 HTTP exception will be thrown.
  193. *
  194. * @param string $id
  195. *
  196. * @return AuthItem the loaded model
  197. *
  198. * @throws HttpException if the model cannot be found
  199. */
  200. protected function findModel($id)
  201. {
  202. $item = Yii::$app->getAuthManager()->getPermission($id);
  203. if ($item) {
  204. return new AuthItem($item);
  205. } else {
  206. throw new NotFoundHttpException('The requested page does not exist.');
  207. }
  208. }
  209. }