コード例 #1
0
 public function actionUpdate($id)
 {
     $permission = $this->findItem($id);
     $authManager = Yii::$app->getAuthManager();
     $authItemForm = new AuthItemForm(['type' => Item::TYPE_PERMISSION]);
     $authItemForm->setAttributes(ArrayHelper::toArray($permission));
     $authItemForm->setItem($permission);
     $post = Yii::$app->getRequest()->post();
     if ($authItemForm->load($post) && $authItemForm->save()) {
         return $this->message('修改成功', 'success', 'index', 'flash');
     }
     return $this->render('update', ['authItemForm' => $authItemForm, 'children' => $authManager->getChildren($authItemForm->name), 'permissionsDataProvider' => new ArrayDataProvider(['models' => array_filter($authManager->getPermissions(), function ($permission) use($authItemForm) {
         // 过滤自己角色
         return $permission->name !== $authItemForm->name;
     })])]);
 }
コード例 #2
0
ファイル: RoleController.php プロジェクト: rocketyang/huajuan
 /**
  * Updates an existing User model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $role = $this->findItem($id);
     $authManager = Yii::$app->getAuthManager();
     $authItemForm = new AuthItemForm(['type' => Item::TYPE_ROLE]);
     $authItemForm->setAttributes(ArrayHelper::toArray($role));
     $authItemForm->setItem($role);
     $post = Yii::$app->getRequest()->post();
     if ($authItemForm->load($post) && $authItemForm->save()) {
         return $this->message('修改成功', 'success', 'index', 'flash');
     }
     return $this->render('update', ['authItemForm' => $authItemForm, 'children' => $authManager->getChildren($authItemForm->name), 'rolesDataProvider' => new ArrayDataProvider(['models' => array_filter($authManager->getRoles(), function ($role) use($authItemForm, $authManager) {
         return $role->name !== $authItemForm->name && !$authManager->hasChild($role, $authItemForm);
         // 子角色中不能显示父角色和当前角色
     })]), 'permissionsDataProvider' => new ArrayDataProvider(['models' => $authManager->getPermissions()])]);
 }
コード例 #3
0
ファイル: RbacController.php プロジェクト: hackvijay/yii-qa
 /**
  * 修改权限角色
  * @param $item
  * @return string|\yii\web\Response
  */
 protected function update($item)
 {
     if (empty($item)) {
         $this->message('抱歉, 找不到指定的角色或权限', 'error');
     }
     $authManager = Yii::$app->getAuthManager();
     //子Item
     $children = $authManager->getChildren($item->name);
     $authChildItemForm = new AuthChildItemForm();
     $authChildItemFormName = $authChildItemForm->formName();
     // 修改删除子角色或权限
     if (isset($_POST[$authChildItemFormName]['child'])) {
         // 由于childItem有外键约束,必须在item修改前修改
         $newChildNames = [];
         //子item提交修改
         foreach ((array) $_POST[$authChildItemFormName]['child'] as $_type => $child) {
             $methodGet = $_type == Item::TYPE_ROLE ? 'getRole' : 'getPermission';
             !is_array($child) && ($child = []);
             foreach ($child as $k => $childName) {
                 $newChildNames[] = $childName;
                 // 已有的子item不做添加操作
                 if (isset($children[$childName])) {
                     continue;
                 }
                 $childItem = $authManager->{$methodGet}($childName);
                 $this->authChildItemFormSave($childItem, true, $item, $authChildItemForm);
             }
         }
         if ($newChildNames !== []) {
             //子item 删除
             foreach (array_diff(array_keys($children), $newChildNames) as $k => $childName) {
                 //批量删除子item
                 $methodGet = $children[$childName]->type == Item::TYPE_ROLE ? 'getRole' : 'getPermission';
                 $childItem = $authManager->{$methodGet}($childName);
                 $this->authChildItemFormSave($childItem, false, $item, $authChildItemForm);
             }
         }
     }
     $authItemForm = new AuthItemForm(['type' => $item->type]);
     $authItemForm->setAttributes(ArrayHelper::toArray($item));
     //item 提交修改
     if ($authItemForm->load($_POST) && $authItemForm->saveItem($item)) {
         return $this->flash('更新成功', 'success', ['', 'name' => $item->name]);
     }
     if ($item->type == Item::TYPE_ROLE) {
         // 角色
         $params = ['rolesDataProvider' => new ArrayDataProvider(['models' => array_filter($authManager->getRoles(), function ($role) use($item, $authManager) {
             return $role->name !== $item->name && !$authManager->hasChild($role, $item);
             // 过滤父角色和自己的角色
         })]), 'permissionsDataProvider' => new ArrayDataProvider(['models' => $authManager->getPermissions()]), 'childRoles' => array_diff_key($children, $authManager->getPermissionsByRole($item->name)), 'childPermissions' => $authManager->getPermissionsByRole($item->name, false)];
     } else {
         //权限
         $params = ['permissionsDataProvider' => new ArrayDataProvider(['models' => array_filter($authManager->getPermissions(), function ($permission) use($item) {
             return $item->name != $permission->name;
             //过滤掉自己的权限
         })]), 'childPermissions' => $children];
     }
     return $this->render('update', array_merge(['item' => $item, 'opeartion' => '修改', 'type' => $item->type, 'authItemForm' => $authItemForm, 'authChildItemForm' => $authChildItemForm], $params));
 }