RuleController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace common\modules\rbac\controllers;
  3. use common\modules\rbac\components\MenuHelper;
  4. use common\modules\rbac\models\BizRule;
  5. use common\modules\rbac\models\searchs\BizRule as BizRuleSearch;
  6. use Yii;
  7. use yii\filters\VerbFilter;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. /**
  11. * Description of RuleController.
  12. *
  13. * @author Misbahul D Munir <misbahuldmunir@gmail.com>
  14. *
  15. * @since 1.0
  16. */
  17. class RuleController extends Controller
  18. {
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  28. 'delete' => ['post'],
  29. ],
  30. ],
  31. ];
  32. }
  33. /**
  34. * Lists all AuthItem models.
  35. *
  36. * @return mixed
  37. */
  38. public function actionIndex()
  39. {
  40. $searchModel = new BizRuleSearch();
  41. $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
  42. return $this->render('index', [
  43. 'dataProvider' => $dataProvider,
  44. 'searchModel' => $searchModel,
  45. ]);
  46. }
  47. /**
  48. * Displays a single AuthItem model.
  49. *
  50. * @param string $id
  51. *
  52. * @return mixed
  53. */
  54. public function actionView($id)
  55. {
  56. $model = $this->findModel($id);
  57. return $this->render('view', ['model' => $model]);
  58. }
  59. /**
  60. * Creates a new AuthItem model.
  61. * If creation is successful, the browser will be redirected to the 'view' page.
  62. *
  63. * @return mixed
  64. */
  65. public function actionCreate()
  66. {
  67. $model = new BizRule(null);
  68. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  69. MenuHelper::invalidate();
  70. return $this->redirect(['view', 'id' => $model->name]);
  71. } else {
  72. return $this->render('create', ['model' => $model]);
  73. }
  74. }
  75. /**
  76. * Updates an existing AuthItem model.
  77. * If update is successful, the browser will be redirected to the 'view' page.
  78. *
  79. * @param string $id
  80. *
  81. * @return mixed
  82. */
  83. public function actionUpdate($id)
  84. {
  85. $model = $this->findModel($id);
  86. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  87. MenuHelper::invalidate();
  88. return $this->redirect(['view', 'id' => $model->name]);
  89. }
  90. return $this->render('update', ['model' => $model]);
  91. }
  92. /**
  93. * Deletes an existing AuthItem model.
  94. * If deletion is successful, the browser will be redirected to the 'index' page.
  95. *
  96. * @param string $id
  97. *
  98. * @return mixed
  99. */
  100. public function actionDelete($id)
  101. {
  102. $model = $this->findModel($id);
  103. Yii::$app->authManager->remove($model->item);
  104. MenuHelper::invalidate();
  105. return $this->redirect(['index']);
  106. }
  107. /**
  108. * Finds the AuthItem model based on its primary key value.
  109. * If the model is not found, a 404 HTTP exception will be thrown.
  110. *
  111. * @param string $id
  112. *
  113. * @return AuthItem the loaded model
  114. *
  115. * @throws HttpException if the model cannot be found
  116. */
  117. protected function findModel($id)
  118. {
  119. $item = Yii::$app->authManager->getRule($id);
  120. if ($item) {
  121. return new BizRule($item);
  122. } else {
  123. throw new NotFoundHttpException('The requested page does not exist.');
  124. }
  125. }
  126. }