Dominus77 /
yii2-basic-start
| 1 | <?php |
||
| 2 | |||
| 3 | namespace modules\rbac\controllers; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\data\ArrayDataProvider; |
||
| 7 | use yii\web\Controller; |
||
| 8 | use yii\filters\AccessControl; |
||
| 9 | use yii\filters\VerbFilter; |
||
| 10 | use yii\web\BadRequestHttpException; |
||
| 11 | use yii\widgets\ActiveForm; |
||
| 12 | use yii\web\Response; |
||
| 13 | use modules\rbac\models\Role; |
||
| 14 | use modules\rbac\Module; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Class RolesController |
||
| 18 | * @package modules\rbac\controllers |
||
| 19 | */ |
||
| 20 | class RolesController extends Controller |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @inheritdoc |
||
| 24 | * @return array |
||
| 25 | */ |
||
| 26 | public function behaviors() |
||
| 27 | { |
||
| 28 | return [ |
||
| 29 | 'access' => [ |
||
| 30 | 'class' => AccessControl::class, |
||
| 31 | 'rules' => [ |
||
| 32 | [ |
||
| 33 | 'allow' => true, |
||
| 34 | 'roles' => ['managerRbac'], |
||
| 35 | ], |
||
| 36 | ], |
||
| 37 | ], |
||
| 38 | 'verbs' => [ |
||
| 39 | 'class' => VerbFilter::class, |
||
| 40 | 'actions' => [ |
||
| 41 | 'delete' => ['POST'] |
||
| 42 | ], |
||
| 43 | ], |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Lists all Role models. |
||
| 49 | * @return mixed |
||
| 50 | */ |
||
| 51 | public function actionIndex() |
||
| 52 | { |
||
| 53 | $auth = Yii::$app->authManager; |
||
| 54 | $dataProvider = new ArrayDataProvider([ |
||
| 55 | 'allModels' => $auth->getRoles(), |
||
| 56 | 'sort' => [ |
||
| 57 | 'attributes' => ['name', 'description', 'ruleName'], |
||
| 58 | ], |
||
| 59 | 'pagination' => [ |
||
| 60 | 'pageSize' => 15, |
||
| 61 | ], |
||
| 62 | ]); |
||
| 63 | return $this->render('index', [ |
||
| 64 | 'dataProvider' => $dataProvider, |
||
| 65 | ]); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Displays a single Role model. |
||
| 70 | * @param string|int $id |
||
| 71 | * @return mixed |
||
| 72 | */ |
||
| 73 | public function actionView($id) |
||
| 74 | { |
||
| 75 | $auth = Yii::$app->authManager; |
||
| 76 | $role = $auth->getRole($id); |
||
| 77 | |||
| 78 | $model = new Role(['name' => $role->name]); |
||
| 79 | return $this->render('view', [ |
||
| 80 | 'role' => $role, |
||
| 81 | 'model' => $model |
||
| 82 | ]); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Creates Role a new Role model. |
||
| 87 | * If creation is successful, the browser will be redirected to the 'view' page. |
||
| 88 | * @return array|string|\yii\web\Response |
||
| 89 | * @throws \Exception |
||
| 90 | */ |
||
| 91 | public function actionCreate() |
||
| 92 | { |
||
| 93 | $model = new Role(['scenario' => Role::SCENARIO_CREATE]); |
||
| 94 | $model->isNewRecord = true; |
||
| 95 | |||
| 96 | if ($model->load(Yii::$app->request->post())) { |
||
| 97 | if ($model->validate()) { |
||
| 98 | $auth = Yii::$app->authManager; |
||
| 99 | $role = $auth->createRole($model->name); |
||
| 100 | $role->description = $model->description; |
||
| 101 | if ($auth->add($role)) { |
||
| 102 | return $this->redirect(['view', 'id' => $model->name]); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | } |
||
| 106 | return $this->render('create', [ |
||
| 107 | 'model' => $model, |
||
| 108 | ]); |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return array|bool |
||
| 113 | */ |
||
| 114 | public function actionAjaxValidateForm() |
||
| 115 | { |
||
| 116 | $model = new Role(['scenario' => Role::SCENARIO_CREATE]); |
||
| 117 | if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { |
||
| 118 | Yii::$app->response->format = Response::FORMAT_JSON; |
||
| 119 | return ActiveForm::validate($model); |
||
| 120 | } |
||
| 121 | return false; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Updates an existing Role model. |
||
| 126 | * If update is successful, the browser will be redirected to the 'view' page. |
||
| 127 | * @param string|int $id |
||
| 128 | * @return string|\yii\web\Response |
||
| 129 | * @throws \Exception |
||
| 130 | */ |
||
| 131 | public function actionUpdate($id) |
||
| 132 | { |
||
| 133 | $auth = Yii::$app->authManager; |
||
| 134 | $role = $auth->getRole($id); |
||
| 135 | |||
| 136 | $model = new Role([ |
||
| 137 | 'scenario' => Role::SCENARIO_UPDATE, |
||
| 138 | 'name' => $role->name, |
||
| 139 | 'description' => $role->description, |
||
| 140 | ]); |
||
| 141 | if ($model->load(Yii::$app->request->post())) { |
||
| 142 | $role->description = $model->description; |
||
| 143 | if ($auth->update($id, $role)) { |
||
| 144 | return $this->redirect(['view', 'id' => $id]); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | return $this->render('update', [ |
||
| 148 | 'model' => $model, |
||
| 149 | ]); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Привязываем роль |
||
| 154 | * @return \yii\web\Response |
||
| 155 | * @throws BadRequestHttpException |
||
| 156 | * @throws \Exception |
||
| 157 | */ |
||
| 158 | public function actionAddRoles() |
||
| 159 | { |
||
| 160 | $model = new Role([ |
||
| 161 | 'scenario' => Role::SCENARIO_UPDATE, |
||
| 162 | ]); |
||
| 163 | if ($model->load(Yii::$app->request->post())) { |
||
| 164 | $auth = Yii::$app->authManager; |
||
| 165 | $role = $auth->getRole($model->name); |
||
| 166 | foreach ($model->itemsRoles as $value) { |
||
| 167 | $add = $auth->getRole($value); |
||
| 168 | // Проверяем, не является добовляемая роль родителем? |
||
| 169 | $result = $this->detectLoop($role, $add); |
||
| 170 | if (!$result) { |
||
| 171 | $auth->addChild($role, $add); |
||
| 172 | } else { |
||
| 173 | Yii::$app->session->setFlash('error', Module::t( |
||
|
0 ignored issues
–
show
|
|||
| 174 | 'module', |
||
| 175 | 'The role of the "{:parent}" is the parent of the "{:role}"!', |
||
| 176 | [':parent' => $add->name, ':role' => $role->name] |
||
| 177 | )); |
||
| 178 | } |
||
| 179 | } |
||
| 180 | return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-roles']); |
||
| 181 | } |
||
| 182 | throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!')); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Отзываем роль |
||
| 187 | * @return array|\yii\web\Response |
||
| 188 | * @throws BadRequestHttpException |
||
| 189 | */ |
||
| 190 | public function actionRemoveRoles() |
||
| 191 | { |
||
| 192 | $model = new Role([ |
||
| 193 | 'scenario' => Role::SCENARIO_UPDATE, |
||
| 194 | ]); |
||
| 195 | if ($model->load(Yii::$app->request->post())) { |
||
| 196 | $auth = Yii::$app->authManager; |
||
| 197 | $role = $auth->getRole($model->name); |
||
| 198 | foreach ($model->rolesByRole as $value) { |
||
| 199 | $remove = $auth->getRole($value); |
||
| 200 | $auth->removeChild($role, $remove); |
||
| 201 | } |
||
| 202 | return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-roles']); |
||
| 203 | } |
||
| 204 | throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!')); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Привязываем разрешение |
||
| 209 | * @return array|\yii\web\Response |
||
| 210 | * @throws BadRequestHttpException |
||
| 211 | * @throws \Exception |
||
| 212 | */ |
||
| 213 | public function actionAddPermissions() |
||
| 214 | { |
||
| 215 | $model = new Role([ |
||
| 216 | 'scenario' => Role::SCENARIO_UPDATE, |
||
| 217 | ]); |
||
| 218 | if ($model->load(Yii::$app->request->post())) { |
||
| 219 | $auth = Yii::$app->authManager; |
||
| 220 | $role = $auth->getRole($model->name); |
||
| 221 | foreach ($model->itemsPermissions as $value) { |
||
| 222 | $add = $auth->getPermission($value); |
||
| 223 | // Проверяем, не является добовляемое разрешение родителем? |
||
| 224 | $result = $this->detectLoop($role, $add); |
||
| 225 | if (!$result) { |
||
| 226 | $auth->addChild($role, $add); |
||
| 227 | } else { |
||
| 228 | Yii::$app->session->setFlash('error', Module::t( |
||
| 229 | 'module', |
||
| 230 | 'The permission of the "{:parent}" is the parent of the "{:permission}"!', |
||
| 231 | [':parent' => $add->name, ':permission' => $role->name] |
||
| 232 | )); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']); |
||
| 236 | } |
||
| 237 | throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!')); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Отзываем разрешение |
||
| 242 | * @return array|\yii\web\Response |
||
| 243 | * @throws BadRequestHttpException |
||
| 244 | */ |
||
| 245 | public function actionRemovePermissions() |
||
| 246 | { |
||
| 247 | $model = new Role([ |
||
| 248 | 'scenario' => Role::SCENARIO_UPDATE, |
||
| 249 | ]); |
||
| 250 | if ($model->load(Yii::$app->request->post())) { |
||
| 251 | $auth = Yii::$app->authManager; |
||
| 252 | $role = $auth->getRole($model->name); |
||
| 253 | foreach ($model->permissionsByRole as $value) { |
||
| 254 | $remove = $auth->getPermission($value); |
||
| 255 | $auth->removeChild($role, $remove); |
||
| 256 | } |
||
| 257 | return $this->redirect(['update', 'id' => $model->name, '#' => 'assign-container-permissions']); |
||
| 258 | } |
||
| 259 | throw new BadRequestHttpException(Module::t('module', 'Not a valid request to the method!')); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Deletes an existing Role model. |
||
| 264 | * If deletion is successful, the browser will be redirected to the 'index' page. |
||
| 265 | * @param string|int $id |
||
| 266 | * @return \yii\web\Response |
||
| 267 | */ |
||
| 268 | public function actionDelete($id) |
||
| 269 | { |
||
| 270 | $auth = Yii::$app->authManager; |
||
| 271 | $role = $auth->getRole($id); |
||
| 272 | if ($auth->remove($role)) { |
||
| 273 | Yii::$app->session->setFlash('success', Module::t( |
||
| 274 | 'module', |
||
| 275 | 'The role "{:name}" have been successfully deleted.', |
||
| 276 | [':name' => $role->name] |
||
| 277 | )); |
||
| 278 | } else { |
||
| 279 | Yii::$app->session->setFlash('error', Module::t('module', 'Error!')); |
||
| 280 | } |
||
| 281 | return $this->redirect(['index']); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param object $parent |
||
| 286 | * @param object $child |
||
| 287 | * @return bool |
||
| 288 | */ |
||
| 289 | protected function detectLoop($parent, $child) |
||
| 290 | { |
||
| 291 | $auth = Yii::$app->authManager; |
||
| 292 | if ($child->name === $parent->name) { |
||
| 293 | return true; |
||
| 294 | } |
||
| 295 | foreach ($auth->getChildren($child->name) as $grandchild) { |
||
| 296 | if ($this->detectLoop($parent, $grandchild)) { |
||
| 297 | return true; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | return false; |
||
| 301 | } |
||
| 302 | } |
||
| 303 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.