update() public method

Updates the specified role, permission or rule in the system.
public update ( string $name, Role | Permission | Rule $object ) : boolean
$name string the old name of the role, permission or rule
$object Role | Permission | Rule
return boolean whether the update is successful
コード例 #1
0
ファイル: Permission.php プロジェクト: apurey/cmf
 /**
  * @return bool
  */
 public function updatePermission()
 {
     if ($this->validate()) {
         $permission = $this->authManager->getPermission($this->name);
         $permission->description = $this->description;
         return $this->authManager->update($this->name, $permission);
     }
     return false;
 }
コード例 #2
0
ファイル: AuthItemModel.php プロジェクト: yii2mod/yii2-rbac
 /**
  * Save role to [[\yii\rbac\authManager]]
  *
  * @return bool
  */
 public function save()
 {
     if ($this->validate()) {
         if ($this->_item === null) {
             if ($this->type == Item::TYPE_ROLE) {
                 $this->_item = $this->manager->createRole($this->name);
             } else {
                 $this->_item = $this->manager->createPermission($this->name);
             }
             $isNew = true;
             $oldName = false;
         } else {
             $isNew = false;
             $oldName = $this->_item->name;
         }
         $this->_item->name = $this->name;
         $this->_item->description = $this->description;
         $this->_item->ruleName = $this->ruleName;
         $this->_item->data = Json::decode($this->data);
         if ($isNew) {
             $this->manager->add($this->_item);
         } else {
             $this->manager->update($oldName, $this->_item);
         }
         return true;
     }
     return false;
 }
コード例 #3
0
 public function testUpdateRule()
 {
     $this->prepareData();
     $rule = $this->auth->getRule('isAuthor');
     $rule->name = "newName";
     $rule->reallyReally = false;
     $this->auth->update('isAuthor', $rule);
     $rule = $this->auth->getRule('isAuthor');
     $this->assertEquals(null, $rule);
     $rule = $this->auth->getRule('newName');
     $this->assertEquals("newName", $rule->name);
     $this->assertEquals(false, $rule->reallyReally);
     $rule->reallyReally = true;
     $this->auth->update('newName', $rule);
     $rule = $this->auth->getRule('newName');
     $this->assertEquals(true, $rule->reallyReally);
 }
コード例 #4
0
ファイル: RoleController.php プロジェクト: wsdslm/yii2-rbac
 public function actionView($name)
 {
     $role = $this->findRole($name);
     if (Yii::$app->request->isPost) {
         $rule = Yii::$app->request->post('rule');
         $role->ruleName = $rule ? $rule : null;
         $role->description = Yii::$app->request->post('desc');
         $role->data = Yii::$app->request->post('data');
         $this->auth->update($name, $role);
         return $this->redirect(['view', 'name' => $role->name]);
     }
     return $this->render('view', ['model' => $role]);
 }
コード例 #5
0
ファイル: RoleForm.php プロジェクト: yujin1st/yii2-user
 /**
  * @param bool $validate
  * @return bool
  */
 public function save($validate = true)
 {
     if ($validate && !$this->validate()) {
         return false;
     }
     if ($this->isNewRecord) {
         $this->role = $this->auth->createRole($this->name);
         $this->role->description = $this->description;
         if (!$this->auth->add($this->role)) {
             return false;
         }
     } else {
         $this->role->name = $this->name;
         $this->role->description = $this->description;
         if (!$this->auth->update($this->oldName, $this->role)) {
             return false;
         }
         $this->auth->removeChildren($this->role);
     }
     foreach ($this->actions as $action) {
         $this->auth->addChild($this->role, $this->auth->getPermission($action));
     }
     return true;
 }
コード例 #6
0
ファイル: Role.php プロジェクト: apurey/cmf
 /**
  * @param string $name
  * @param array $permissions
  * @param array $roles
  * @return bool
  */
 public function updateRole($name, array $permissions, array $roles)
 {
     if ($this->validate()) {
         $object = $this->authManager->getRole($name);
         $object->description = $this->description;
         if ($this->authManager->update($name, $object)) {
             $this->authManager->removeChildren($object);
             foreach ($permissions as $permission) {
                 $this->authManager->addChild($object, $this->authManager->getPermission($permission));
             }
             foreach ($roles as $role) {
                 $this->authManager->addChild($object, $this->authManager->getRole($role));
             }
             return true;
         }
     }
     return false;
 }
コード例 #7
0
ファイル: BizRuleModel.php プロジェクト: yii2mod/yii2-rbac
 /**
  * Save rule
  *
  * @return bool
  */
 public function save()
 {
     if ($this->validate()) {
         $class = $this->className;
         if ($this->_item === null) {
             $this->_item = new $class();
             $isNew = true;
             $oldName = false;
         } else {
             $isNew = false;
             $oldName = $this->_item->name;
         }
         $this->_item->name = $this->name;
         if ($isNew) {
             $this->manager->add($this->_item);
         } else {
             $this->manager->update($oldName, $this->_item);
         }
         return true;
     }
     return false;
 }