/** @inheritdoc */ public function init() { parent::init(); $this->manager = \Yii::$app->authManager; if ($this->item instanceof Item) { $this->name = $this->item->name; $this->description = $this->item->description; $this->children = array_keys($this->manager->getChildren($this->item->name)); if ($this->item->ruleName !== null) { $this->rule = get_class($this->manager->getRule($this->item->ruleName)); } } }
/** * Update role using string name. * * @param string $id * @return mixed * @throws NotFoundHttpException */ public function actionUpdate($id) { $this->layout = '@app/views/layouts/one-column'; $role = $this->findRole($id); $model = RoleForm::createFromRole($role, $this->authManager->getChildren($role->name)); /* @var $systemAlert Alert */ $systemAlert = Yii::$app->systemAlert; if (Yii::$app->request->isAjax && $model->load($_POST)) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } if ($model->load($_POST) && $model->validate()) { $transaction = Yii::$app->db->beginTransaction(); try { // update role description $role->description = $model->description; if (!$this->authManager->update($role->name, $role)) { throw new Exception(); } // update role permissions $this->authManager->removeChildren($role); foreach ($model->getPermissionModels() as $permission) { $this->authManager->addChild($role, $permission); } $transaction->commit(); $systemAlert->setMessage(Alert::SUCCESS, Yii::t('user', 'Role updated successfully')); return $this->redirect(['index']); } catch (Exception $ex) { $transaction->rollback(); $systemAlert->setMessage(Alert::DANGER, Yii::t('app', 'System error: {message}', ['message' => $ex->getMessage()])); } } return $this->render('update', ['model' => $model]); }
/** * load permissions for selected * @return array */ public function loadPermissions() { $auth = new DbManager(); $auth->init(); $children = $auth->getChildren($this->role_name); $dbPermissions = $this->serializePermissions($children); $selectedValue = []; foreach ($dbPermissions as $key => $value) { $selectedValue[$key] = array_keys($value); } return $selectedValue; }
/** * @param string $id * * @return string */ public function actionView($id) { $role = $this->findModel($id); $authManager = new DbManager(); $allRoles = Role::find()->asArray()->andWhere('name != :current_name', [':current_name' => $id])->all(); $permissions = Permission::find()->andWhere(Yii::$app->getModule(\Yii::$app->user->moduleAliasName)->auth_item_table . '.name != :commonPermissionName', [':commonPermissionName' => Yii::$app->getModule(\Yii::$app->user->moduleAliasName)->commonPermissionName])->joinWith('group')->all(); $permissionsByGroup = []; foreach ($permissions as $permission) { $permissionsByGroup[@$permission->group->name][] = $permission; } $childRoles = $authManager->getChildren($role->name); $currentRoutesAndPermissions = AuthHelper::separateRoutesAndPermissions($authManager->getPermissionsByRole($role->name)); $currentPermissions = $currentRoutesAndPermissions->permissions; return $this->renderIsAjax('view', compact('role', 'allRoles', 'childRoles', 'currentPermissions', 'permissionsByGroup')); }