1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\modules\admin\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\web\Response; |
7
|
|
|
use yii\filters\VerbFilter; |
8
|
|
|
use yii\helpers\Url; |
9
|
|
|
use yii\helpers\ArrayHelper; |
10
|
|
|
use app\traits\ModelTrait; |
11
|
|
|
use app\models\AuthItem; |
12
|
|
|
use app\modules\admin\models\search\AuthItemSearch; |
13
|
|
|
|
14
|
|
|
class RolesController extends \yii\web\Controller |
15
|
|
|
{ |
16
|
|
|
use ModelTrait; |
17
|
|
|
|
18
|
13 |
|
public function behaviors() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
13 |
|
'verbs' => [ |
22
|
|
|
'class' => VerbFilter::class, |
23
|
|
|
'actions' => [ |
24
|
|
|
'delete' => ['post'], |
25
|
|
|
'operations' => ['post'], |
26
|
|
|
], |
27
|
|
|
], |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
13 |
|
public function actions() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
13 |
|
'operations' => [ |
35
|
|
|
'class' => 'app\modules\admin\controllers\common\OperationsAction', |
36
|
|
|
'modelClass' => 'app\models\AuthItem', |
37
|
|
|
'operations' => [ |
38
|
|
|
'delete' => [], |
39
|
|
|
] |
40
|
|
|
], |
41
|
|
|
'delete' => [ |
42
|
|
|
'class' => 'app\modules\admin\controllers\common\DeleteAction', |
43
|
|
|
'modelClass' => 'app\models\AuthItem', |
44
|
|
|
], |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
13 |
|
public function actionIndex() |
49
|
|
|
{ |
50
|
13 |
|
$authItemSearch = new AuthItemSearch(); |
51
|
13 |
|
$dataProvider = $authItemSearch->search(Yii::$app->request->get()); |
52
|
|
|
|
53
|
13 |
|
return $this->render('index', [ |
54
|
13 |
|
'authItemSearch' => $authItemSearch, |
55
|
13 |
|
'dataProvider' => $dataProvider, |
56
|
|
|
]); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function actionEdit($name = null) |
60
|
|
|
{ |
61
|
|
|
$model = new AuthItem(); |
62
|
|
|
$auth = Yii::$app->authManager; |
63
|
|
|
|
64
|
|
|
$roles = ArrayHelper::index($auth->getRoles(), 'name'); |
65
|
|
|
$permissions = ArrayHelper::index($auth->getPermissions(), 'name'); |
66
|
|
|
|
67
|
|
|
if ($name) { |
68
|
|
|
$model = $this->findModel($model, $name); |
|
|
|
|
69
|
|
|
$model = $this->preparePermissionsToSave($model); |
70
|
|
|
$model = $this->prepareRolesToSave($model); |
71
|
|
|
|
72
|
|
|
unset($roles[$model->name]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (Yii::$app->request->isPost) { |
76
|
|
|
$model->type = \yii\rbac\Item::TYPE_ROLE; |
77
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->save()) { |
78
|
|
|
if (!$model->isSuperUser()) { |
79
|
|
|
$this->setRoles($model, $roles, $permissions); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.messages', 'Saved successfully')); |
83
|
|
|
$urlToModel = Url::toRoute(['edit', 'name' => $model->name]); |
84
|
|
|
if (Yii::$app->request->isAjax) { |
85
|
|
|
return $this->asJson(['redirect' => $urlToModel]); |
86
|
|
|
} |
87
|
|
|
return $this->redirect($urlToModel); |
88
|
|
|
} |
89
|
|
|
if (Yii::$app->request->isAjax) { |
90
|
|
|
return $this->asJson($this->collectErrors($model)); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->render('edit', [ |
95
|
|
|
'model' => $model, |
96
|
|
|
'roles' => $roles, |
97
|
|
|
'permissions' => $permissions |
98
|
|
|
]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function setRoles($model, $roles, $permissions) |
102
|
|
|
{ |
103
|
|
|
$auth = Yii::$app->authManager; |
104
|
|
|
|
105
|
|
|
$role = $auth->getRole($model->name); |
106
|
|
|
$auth->removeChildren($role); |
107
|
|
|
|
108
|
|
|
if (is_array($model->roles)) { |
109
|
|
|
foreach ($model->roles as $r) { |
110
|
|
|
$auth->addChild($role, $roles[$r]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (is_array($model->permissions)) { |
115
|
|
|
$currPermissions = ArrayHelper::index( |
116
|
|
|
$auth->getPermissionsByRole($model->name), |
117
|
|
|
'name', |
118
|
|
|
[] |
119
|
|
|
); |
120
|
|
|
foreach ($model->permissions as $permission) { |
121
|
|
|
if (!array_key_exists($permission, $currPermissions)) { |
122
|
|
|
$auth->addChild($role, $permissions[$permission]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function preparePermissionsToSave($model) |
129
|
|
|
{ |
130
|
|
|
$permissions = Yii::$app->authManager->getPermissionsByRole($model->name); |
131
|
|
|
$model->permissions = ArrayHelper::index($permissions, 'name', []); |
132
|
|
|
$model->permissions = array_keys($model->permissions); |
133
|
|
|
|
134
|
|
|
return $model; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function prepareRolesToSave($model) |
138
|
|
|
{ |
139
|
|
|
$roles = Yii::$app->authManager->getChildren($model->name); |
140
|
|
|
$model->roles = ArrayHelper::index($roles, 'name', []); |
141
|
|
|
$model->roles = array_keys($model->roles); |
142
|
|
|
|
143
|
|
|
return $model; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: