示例#1
0
 /**
  * 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()])]);
 }
 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;
     })])]);
 }