11
11
12
12
namespace skeeks \cms \rbac \controllers ;
13
13
14
+ use skeeks \cms \backend \actions \BackendModelAction ;
15
+ use skeeks \cms \backend \controllers \BackendModelStandartController ;
16
+ use skeeks \cms \backend \grid \DefaultActionColumn ;
14
17
use skeeks \cms \helpers \RequestResponse ;
15
18
use skeeks \cms \modules \admin \actions \AdminAction ;
16
19
use skeeks \cms \modules \admin \actions \modelEditor \AdminOneModelEditAction ;
17
- use skeeks \cms \modules \admin \controllers \AdminModelEditorController ;
18
20
use skeeks \cms \rbac \CmsManager ;
19
21
use skeeks \cms \rbac \models \AuthItem ;
22
+ use skeeks \cms \rbac \models \CmsAuthItem ;
20
23
use skeeks \cms \rbac \models \searchs \AuthItem as AuthItemSearch ;
24
+ use skeeks \yii2 \form \fields \HtmlBlock ;
21
25
use Yii ;
22
- use yii \filters \VerbFilter ;
26
+ use yii \base \Event ;
27
+ use yii \data \ActiveDataProvider ;
28
+ use yii \db \ActiveQuery ;
23
29
use yii \helpers \ArrayHelper ;
24
30
use yii \helpers \Html ;
25
31
use yii \rbac \Item ;
26
32
use yii \rbac \Role ;
27
- use yii \web \Controller ;
28
33
use yii \web \NotFoundHttpException ;
29
34
use yii \web \Response ;
30
35
use yii \widgets \ActiveForm ;
31
36
32
37
/**
33
38
* AuthItemController implements the CRUD actions for AuthItem model.
34
39
*/
35
- class AdminRoleController extends AdminModelEditorController
40
+ class AdminRoleController extends BackendModelStandartController
36
41
{
37
42
public function init ()
38
43
{
39
- $ this ->name = \Yii::t ('app ' , "Managing Roles " );
44
+ $ this ->name = \Yii::t ('skeeks/rbac ' , "Managing roles " );
40
45
$ this ->modelShowAttribute = "name " ;
41
46
$ this ->modelPkAttribute = "name " ;
42
- $ this ->modelClassName = Role:: className () ;
47
+ $ this ->modelClassName = CmsAuthItem::class ;
43
48
44
49
$ this ->generateAccessActions = false ;
45
50
$ this ->permissionName = CmsManager::PERMISSION_ROOT_ACCESS ;
@@ -50,35 +55,102 @@ public function init()
50
55
public function actions ()
51
56
{
52
57
return ArrayHelper::merge (parent ::actions (), [
53
- 'index ' =>
54
- [
55
- 'class ' => AdminAction::className (),
56
- 'callback ' => [$ this , 'actionIndex ' ]
58
+ 'index ' => [
59
+ "filters " => [
60
+ 'visibleFilters ' => [
61
+ 'name ' ,
62
+ ],
57
63
],
58
- 'view ' =>
59
- [
60
- "class " => AdminOneModelEditAction::className (),
61
- "name " => \Yii::t ('app ' , "Watch " ),
62
- "icon " => "fa fa-eye " ,
63
- "callback " => [$ this , "actionView " ],
64
- ],
65
- 'create ' =>
66
- [
67
- 'class ' => AdminAction::className (),
68
- 'callback ' => [$ this , 'actionCreate ' ]
69
- ],
70
- "update " =>
71
- [
72
- 'class ' => AdminOneModelEditAction::className (),
73
- 'callback ' => [$ this , 'actionUpdate ' ]
74
- ],
75
- "delete " =>
76
- [
77
- 'callback ' => [$ this , 'actionDelete ' ]
64
+ 'grid ' => [
65
+ 'on init ' => function (Event $ e ) {
66
+ /**
67
+ * @var $dataProvider ActiveDataProvider
68
+ * @var $query ActiveQuery
69
+ */
70
+ $ query = $ e ->sender ->dataProvider ->query ;
71
+ $ dataProvider = $ e ->sender ->dataProvider ;
72
+
73
+ $ query ->andWhere ([
74
+ CmsAuthItem::tableName ().".type " => Role::TYPE_ROLE
75
+ ]);
76
+
77
+ /* $query->select([
78
+ CmsContentProperty::tableName().'.*',
79
+ //'countElementProperties' => new Expression("count(*)"),
80
+ 'countElementProperties' => $subQuery,
81
+ ]);*/
82
+ },
83
+
84
+ 'defaultOrder ' => [
85
+ 'name ' => SORT_DESC ,
86
+ ],
87
+ 'visibleColumns ' => [
88
+ 'checkbox ' ,
89
+ 'actions ' ,
90
+
91
+ 'name ' ,
92
+ ],
93
+
94
+ 'columns ' => [
95
+ 'name ' => [
96
+ 'attribute ' => 'name ' ,
97
+ 'label ' => \Yii::t ('skeeks/rbac ' , 'Role ' ),
98
+ 'format ' => 'raw ' ,
99
+ 'value ' => function (CmsAuthItem $ cmsAuthItem ) {
100
+ return \yii \helpers \Html::a ($ cmsAuthItem ->name , "# " , [
101
+ 'class ' => "sx-trigger-action " ,
102
+ ]). "<div style='color: gray'> {$ cmsAuthItem ->description }</div> " ;
103
+ }
104
+ ]
105
+ ]
78
106
],
107
+ ],
108
+ 'view ' => [
109
+ "class " => BackendModelAction::className (),
110
+ "name " => \Yii::t ('skeeks/rbac ' , "Watch " ),
111
+ "icon " => "fa fa-eye " ,
112
+ "callback " => [$ this , "actionView " ],
113
+ ],
114
+ 'create ' => [
115
+ 'fields ' => [$ this , 'updateFields ' ],
116
+ ],
117
+ 'update ' => [
118
+ 'fields ' => [$ this , 'updateFields ' ],
119
+ ],
120
+ /*'create' => [
121
+ 'class' => AdminAction::className(),
122
+ 'callback' => [$this, 'actionCreate'],
123
+ ],
124
+ "update" => [
125
+ 'class' => AdminOneModelEditAction::className(),
126
+ 'callback' => [$this, 'actionUpdate'],
127
+ ],*/
128
+ "delete " => [
129
+ 'callback ' => [$ this , 'actionDelete ' ],
130
+ ],
79
131
]);
80
132
}
81
133
134
+ public function updateFields ($ action )
135
+ {
136
+ $ model = $ action ->model ;
137
+ $ model ->type = Item::TYPE_ROLE ;
138
+
139
+ return [
140
+ 'name ' ,
141
+ 'description ' ,
142
+ [
143
+ 'class ' => HtmlBlock::class,
144
+ 'content ' => '<div style="display: none"> ' ,
145
+ ],
146
+ 'type ' ,
147
+
148
+ [
149
+ 'class ' => HtmlBlock::class,
150
+ 'content ' => '</div> ' ,
151
+ ],
152
+ ];
153
+ }
82
154
/**
83
155
* @return Role
84
156
* @throws NotFoundHttpException
@@ -93,24 +165,10 @@ public function getModel()
93
165
return $ this ->_model ;
94
166
}
95
167
96
- /**
97
- * Lists all AuthItem models.
98
- * @return mixed
99
- */
100
- public function actionIndex ()
101
- {
102
- $ searchModel = new AuthItemSearch (['type ' => Item::TYPE_ROLE ]);
103
- $ dataProvider = $ searchModel ->search (Yii::$ app ->request ->getQueryParams ());
104
- return $ this ->render ('index ' , [
105
- 'dataProvider ' => $ dataProvider ,
106
- 'searchModel ' => $ searchModel ,
107
- 'controller ' => $ this ,
108
- ]);
109
- }
110
168
111
169
/**
112
170
* Displays a single AuthItem model.
113
- * @param string $id
171
+ * @param string $id
114
172
* @return mixed
115
173
*/
116
174
public function actionView ()
@@ -120,35 +178,35 @@ public function actionView()
120
178
$ model = $ this ->findModel ($ id );
121
179
$ authManager = Yii::$ app ->getAuthManager ();
122
180
$ avaliable = $ assigned = [
123
- 'Roles ' => [],
181
+ 'Roles ' => [],
124
182
'Permission ' => [],
125
- 'Routes ' => [],
183
+ 'Routes ' => [],
126
184
];
127
185
$ children = array_keys ($ authManager ->getChildren ($ id ));
128
186
$ children [] = $ id ;
129
187
foreach ($ authManager ->getRoles () as $ name => $ role ) {
130
188
if (in_array ($ name , $ children )) {
131
189
continue ;
132
190
}
133
- $ avaliable ['Roles ' ][$ name ] = $ name . ' — ' . $ role ->description ;
191
+ $ avaliable ['Roles ' ][$ name ] = $ name. ' — ' . $ role ->description ;
134
192
}
135
193
foreach ($ authManager ->getPermissions () as $ name => $ role ) {
136
194
if (in_array ($ name , $ children )) {
137
195
continue ;
138
196
}
139
197
140
198
if (isset ($ name [0 ])) {
141
- $ avaliable [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name . ' — ' . $ role ->description ;
199
+ $ avaliable [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name. ' — ' . $ role ->description ;
142
200
}
143
201
}
144
202
foreach ($ authManager ->getChildren ($ id ) as $ name => $ child ) {
145
203
if ($ child ->type == Item::TYPE_ROLE ) {
146
- $ assigned ['Roles ' ][$ name ] = $ name . ' — ' . $ child ->description ;
204
+ $ assigned ['Roles ' ][$ name ] = $ name. ' — ' . $ child ->description ;
147
205
} else {
148
206
if (isset ($ name [0 ])) {
149
- $ assigned [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name . ' — ' . $ child ->description ;
207
+ $ assigned [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name. ' — ' . $ child ->description ;
150
208
} else {
151
- $ assigned ['Permission ' ][$ name ] = $ name . ' — ' . $ child ->description ;
209
+ $ assigned ['Permission ' ][$ name ] = $ name. ' — ' . $ child ->description ;
152
210
}
153
211
}
154
212
}
@@ -157,53 +215,6 @@ public function actionView()
157
215
return $ this ->render ('view ' , ['model ' => $ model , 'avaliable ' => $ avaliable , 'assigned ' => $ assigned ]);
158
216
}
159
217
160
- /**
161
- * Creates a new AuthItem model.
162
- * If creation is successful, the browser will be redirected to the 'view' page.
163
- * @return mixed
164
- */
165
- public function actionCreate ()
166
- {
167
- $ model = new AuthItem (null );
168
- $ model ->type = Item::TYPE_ROLE ;
169
- if (\Yii::$ app ->request ->isAjax && !\Yii::$ app ->request ->isPjax ) {
170
- $ model ->load (\Yii::$ app ->request ->post ());
171
- \Yii::$ app ->response ->format = Response::FORMAT_JSON ;
172
- return ActiveForm::validate ($ model );
173
- }
174
- if ($ model ->load (Yii::$ app ->getRequest ()->post ()) && $ model ->save ()) {
175
- return $ this ->redirect (['view ' , 'id ' => $ model ->name ]);
176
- } else {
177
- return $ this ->render ('create ' , ['model ' => $ model ,]);
178
- }
179
- }
180
-
181
- /**
182
- * Updates an existing AuthItem model.
183
- * If update is successful, the browser will be redirected to the 'view' page.
184
- * @param string $id
185
- * @return mixed
186
- */
187
- public function actionUpdate ()
188
- {
189
- $ model = $ this ->model ;
190
- $ id = $ model ->name ;
191
- $ model = $ this ->findModel ($ id );
192
- if (\Yii::$ app ->request ->isAjax && !\Yii::$ app ->request ->isPjax ) {
193
- $ model ->load (\Yii::$ app ->request ->post ());
194
- \Yii::$ app ->response ->format = Response::FORMAT_JSON ;
195
- return ActiveForm::validate ($ model );
196
- }
197
- if (\Yii::$ app ->request ->isAjax ) {
198
- if ($ model ->load (\Yii::$ app ->request ->post ()) && $ model ->save ()) {
199
- \Yii::$ app ->getSession ()->setFlash ('success ' , \Yii::t ('app ' , 'Saved successfully ' ));
200
- } else {
201
- \Yii::$ app ->getSession ()->setFlash ('error ' , \Yii::t ('app ' , 'Failed to save ' ));
202
- }
203
- }
204
- return $ this ->render ('update ' , ['model ' => $ model ,]);
205
- }
206
-
207
218
/**
208
219
* Deletes an existing Game model.
209
220
* If deletion is successful, the browser will be redirected to the 'index' page.
@@ -280,7 +291,7 @@ public function actionAssign()
280
291
return [
281
292
$ this ->actionRoleSearch ($ id , 'avaliable ' , $ post ['search_av ' ]),
282
293
$ this ->actionRoleSearch ($ id , 'assigned ' , $ post ['search_asgn ' ]),
283
- $ error
294
+ $ error,
284
295
];
285
296
}
286
297
@@ -294,9 +305,9 @@ public function actionAssign()
294
305
public function actionRoleSearch ($ id , $ target , $ term = '' )
295
306
{
296
307
$ result = [
297
- 'Roles ' => [],
308
+ 'Roles ' => [],
298
309
'Permission ' => [],
299
- 'Routes ' => [],
310
+ 'Routes ' => [],
300
311
];
301
312
$ authManager = Yii::$ app ->authManager ;
302
313
if ($ target == 'avaliable ' ) {
@@ -307,7 +318,7 @@ public function actionRoleSearch($id, $target, $term = '')
307
318
continue ;
308
319
}
309
320
if (empty ($ term ) or strpos ($ name , $ term ) !== false ) {
310
- $ result ['Roles ' ][$ name ] = $ name ;
321
+ $ result ['Roles ' ][$ name ] = $ name. ' — ' . $ role -> description ;
311
322
}
312
323
}
313
324
foreach ($ authManager ->getPermissions () as $ name => $ role ) {
@@ -316,19 +327,22 @@ public function actionRoleSearch($id, $target, $term = '')
316
327
}
317
328
if (empty ($ term ) or strpos ($ name , $ term ) !== false ) {
318
329
if (isset ($ name [0 ])) {
319
- $ result [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name ;
330
+ $ result [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name. " — " . $ role -> description ;
320
331
} else {
321
- $ result ['Permission ' ][$ name ] = $ name ;
332
+ $ result ['Permission ' ][$ name ] = $ name. " — " . $ role -> description ;
322
333
}
323
334
}
324
335
}
325
336
} else {
326
337
foreach ($ authManager ->getChildren ($ id ) as $ name => $ child ) {
327
338
if (empty ($ term ) or strpos ($ name , $ term ) !== false ) {
328
339
if ($ child ->type == Item::TYPE_ROLE ) {
329
- $ result ['Roles ' ][$ name ] = $ name ;
340
+ $ result ['Roles ' ][$ name ] = $ name. " — " . $ child -> description ;
330
341
} else {
331
- $ result [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name ;
342
+ if (isset ($ name [0 ])) {
343
+ $ result [$ name [0 ] === '/ ' ? 'Routes ' : 'Permission ' ][$ name ] = $ name . " — " .$ child ->description ;
344
+ }
345
+
332
346
}
333
347
}
334
348
}
@@ -339,7 +353,7 @@ public function actionRoleSearch($id, $target, $term = '')
339
353
/**
340
354
* Finds the AuthItem model based on its primary key value.
341
355
* If the model is not found, a 404 HTTP exception will be thrown.
342
- * @param string $id
356
+ * @param string $id
343
357
* @return AuthItem the loaded model
344
358
* @throws HttpException if the model cannot be found
345
359
*/
0 commit comments