1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yii2mod\rbac\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\filters\VerbFilter; |
7
|
|
|
use yii\web\Controller; |
8
|
|
|
use yii\web\NotFoundHttpException; |
9
|
|
|
use yii2mod\rbac\models\BizRuleModel; |
10
|
|
|
use yii2mod\rbac\models\search\BizRuleSearch; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class RuleController |
14
|
|
|
* |
15
|
|
|
* @package yii2mod\rbac\controllers |
16
|
|
|
*/ |
17
|
|
|
class RuleController extends Controller |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string search class name for rules search |
21
|
|
|
*/ |
22
|
|
|
public $searchClass = [ |
23
|
|
|
'class' => BizRuleSearch::class, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns a list of behaviors that this component should behave as. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function behaviors(): array |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'verbs' => [ |
35
|
|
|
'class' => VerbFilter::class, |
36
|
|
|
'actions' => [ |
37
|
|
|
'index' => ['get'], |
38
|
|
|
'view' => ['get'], |
39
|
|
|
'create' => ['get', 'post'], |
40
|
|
|
'update' => ['get', 'post'], |
41
|
|
|
'delete' => ['post'], |
42
|
|
|
], |
43
|
|
|
], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of all rules |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function actionIndex() |
53
|
|
|
{ |
54
|
|
|
$searchModel = Yii::createObject($this->searchClass); |
55
|
|
|
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams()); |
56
|
|
|
|
57
|
|
|
return $this->render('index', [ |
58
|
|
|
'dataProvider' => $dataProvider, |
59
|
|
|
'searchModel' => $searchModel, |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Displays a single Rule item. |
65
|
|
|
* |
66
|
|
|
* @param string $id |
67
|
|
|
* |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function actionView(string $id) |
71
|
|
|
{ |
72
|
|
|
$model = $this->findModel($id); |
73
|
|
|
|
74
|
|
|
return $this->render('view', ['model' => $model]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Creates a new Rule item. |
79
|
|
|
* |
80
|
|
|
* If creation is successful, the browser will be redirected to the 'view' page. |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
public function actionCreate() |
85
|
|
|
{ |
86
|
|
|
$model = new BizRuleModel(); |
87
|
|
|
|
88
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
89
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
90
|
|
|
|
91
|
|
|
return $this->redirect(['view', 'id' => $model->name]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->render('create', ['model' => $model]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Updates an existing Rule item. |
99
|
|
|
* |
100
|
|
|
* If update is successful, the browser will be redirected to the 'view' page. |
101
|
|
|
* |
102
|
|
|
* @param string $id |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function actionUpdate(string $id) |
107
|
|
|
{ |
108
|
|
|
$model = $this->findModel($id); |
109
|
|
|
|
110
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
111
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.')); |
112
|
|
|
|
113
|
|
|
return $this->redirect(['view', 'id' => $model->name]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->render('update', ['model' => $model]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Deletes an existing Rule item. |
121
|
|
|
* |
122
|
|
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
123
|
|
|
* |
124
|
|
|
* @param string $id |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
View Code Duplication |
public function actionDelete(string $id) |
129
|
|
|
{ |
130
|
|
|
$model = $this->findModel($id); |
131
|
|
|
Yii::$app->authManager->remove($model->item); |
|
|
|
|
132
|
|
|
Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been deleted.')); |
133
|
|
|
|
134
|
|
|
return $this->redirect(['index']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Finds the BizRuleModel based on its primary key value. |
139
|
|
|
* |
140
|
|
|
* If the model is not found, a 404 HTTP exception will be thrown. |
141
|
|
|
* |
142
|
|
|
* @param string $id |
143
|
|
|
* |
144
|
|
|
* @return BizRuleModel the loaded model |
145
|
|
|
* |
146
|
|
|
* @throws \yii\web\NotFoundHttpException |
147
|
|
|
*/ |
148
|
|
|
protected function findModel(string $id) |
149
|
|
|
{ |
150
|
|
|
$item = Yii::$app->authManager->getRule($id); |
151
|
|
|
|
152
|
|
|
if (!empty($item)) { |
153
|
|
|
return new BizRuleModel($item); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.')); |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.