1 | <?php |
||
14 | class RolesController extends \yii\web\Controller |
||
15 | { |
||
16 | use ModelTrait; |
||
17 | |||
18 | 4 | public function behaviors() |
|
30 | |||
31 | 4 | public function actions() |
|
47 | |||
48 | 4 | public function actionIndex() |
|
58 | |||
59 | 3 | public function actionEdit($name = null) |
|
60 | { |
||
61 | 3 | $model = new AuthItem(); |
|
62 | 3 | $auth = Yii::$app->authManager; |
|
63 | |||
64 | 3 | $roles = ArrayHelper::index($auth->getRoles(), 'name'); |
|
65 | 3 | $permissions = ArrayHelper::index($auth->getPermissions(), 'name'); |
|
66 | |||
67 | 3 | if ($name) { |
|
68 | 2 | $model = $this->findModel($model, $name); |
|
69 | 1 | $model = $this->preparePermissionsToSave($model); |
|
|
|||
70 | 1 | $model = $this->prepareRolesToSave($model); |
|
71 | |||
72 | 1 | unset($roles[$model->name]); |
|
73 | } |
||
74 | |||
75 | 2 | 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 | 2 | return $this->render('edit', [ |
|
95 | 2 | 'model' => $model, |
|
96 | 2 | 'roles' => $roles, |
|
97 | 2 | 'permissions' => $permissions |
|
98 | ]); |
||
99 | } |
||
100 | |||
101 | private function setRoles(AuthItem $model, array $roles, array $permissions): void |
||
127 | |||
128 | 1 | private function preparePermissionsToSave(AuthItem $model): AuthItem |
|
136 | |||
137 | 1 | private function prepareRolesToSave(AuthItem $model): AuthItem |
|
145 | } |
||
146 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.