Exemple #1
0
 /**
  * Saves item.
  *
  * @return bool
  */
 public function save()
 {
     if ($this->validate() == false) {
         return false;
     }
     if ($isNewItem = $this->item === null) {
         $this->item = $this->createItem($this->name);
     } else {
         $oldName = $this->item->name;
     }
     $this->item->name = $this->name;
     $this->item->description = $this->description;
     if (!empty($this->rule)) {
         $rule = \Yii::createObject($this->rule);
         if (null === $this->manager->getRule($rule->name)) {
             $this->manager->add($rule);
         }
         $this->item->ruleName = $rule->name;
     } else {
         $this->item->ruleName = null;
     }
     if ($isNewItem) {
         \Yii::$app->session->setFlash('success', \Yii::t('rbac', 'Item has been created'));
         $this->manager->add($this->item);
     } else {
         \Yii::$app->session->setFlash('success', \Yii::t('rbac', 'Item has been updated'));
         $this->manager->update($oldName, $this->item);
     }
     $this->updateChildren();
     return true;
 }
Exemple #2
0
 /**
  * @inheritdoc
  */
 public function rules()
 {
     return [['name', 'required'], [['name', 'description', 'rule'], 'trim'], ['name', function () {
         if ($this->manager->getItem($this->name) !== null) {
             $this->addError('name', \Yii::t('rbac', 'Auth item with such name already exists'));
         }
     }, 'when' => function () {
         return $this->scenario == 'create' || $this->item->name != $this->name;
     }], ['children', RbacValidator::className()], ['rule', function () {
         $rule = $this->manager->getRule($this->rule);
         if (!$rule) {
             $this->addError('rule', \Yii::t('rbac', 'Rule {0} does not exist', $this->rule));
         }
     }], ['data', function () {
         try {
             Json::decode($this->data);
         } catch (InvalidParamException $e) {
             $this->addError('data', \Yii::t('rbac', 'Data must be type of JSON ({0})', $e->getMessage()));
         }
     }]];
 }