johnitvn / yii2-rbac-plus

Database role base access control manager for yii2
49 stars 52 forks source link

Call to undefined method getDb() while creating Role #38

Closed hgrami closed 6 years ago

hgrami commented 6 years ago

When creating a new role I get a call to undefined method Role::getDb(). To fix this issue modifications on johnitvn\rbac-plus\src\models\AuthItem.php needs to be made.

public function unique needs to be renamed as well under rules unique needs to be renamed as follow:

public function uniqueValidator() {
        $authManager = Yii::$app->authManager;
        $value = $this->name;
        if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) {
            $message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
            $params = [
                'attribute' => $this->getAttributeLabel('name'),
                'value' => $value,
            ];
            $this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
        }
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['ruleName'], 'in',
                'range' => array_keys(Yii::$app->authManager->getRules()),
                'message' => Yii::t('rbac', 'Rule not exists')],
            [['name'], 'required'],
            [['name'], 'uniqueValidator', 'when' => function() {
            return $this->isNewRecord || ($this->item->name != $this->name);
        }],
            [['description', 'data', 'ruleName'], 'default'],
            [['name'], 'string', 'max' => 64]
        ];
    }

I suggest overriding model

hgrami commented 6 years ago
public function uniqueValidator() {
        $authManager = Yii::$app->authManager;
        $value = $this->name;
        if ($authManager->getRole($value) !== null || $authManager->getPermission($value) !== null) {
            $message = Yii::t('yii', '{attribute} "{value}" has already been taken.');
            $params = [
                'attribute' => $this->getAttributeLabel('name'),
                'value' => $value,
            ];
            $this->addError('name', Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
        }
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['ruleName'], 'in',
                'range' => array_keys(Yii::$app->authManager->getRules()),
                'message' => Yii::t('rbac', 'Rule not exists')],
            [['name'], 'required'],
            [['name'], 'uniqueValidator', 'when' => function() {
            return $this->isNewRecord || ($this->item->name != $this->name);
        }],
            [['description', 'data', 'ruleName'], 'default'],
            [['name'], 'string', 'max' => 64]
        ];
    }
AndreyTSpb commented 5 years ago

Thanks for helping out)))))

sasban9 commented 3 years ago

That helped me in 2021!

yanutsh commented 2 years ago

Thanks. It's help me. But I made changes in amintado\rbac-plus\src\models\AuthItem.php