/** * Updates an existing AuthRule model. * If update is successful, the browser will be redirected to the 'view' page. * @param string $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $dynamicModel = new DynamicModel(['ruleNamespace']); $dynamicModel->addRule(['ruleNamespace'], function ($attribute, $params) use($dynamicModel) { $this->validateClass($dynamicModel, $attribute, ['extends' => \yii\rbac\Rule::className()]); }); $dynamicModel->addRule(['ruleNamespace'], 'required'); if ($model->data && ($ruleNamespace = unserialize($model->data)) !== false) { if (method_exists($ruleNamespace, 'className')) { $dynamicModel->ruleNamespace = $ruleNamespace::className(); } } $post = Yii::$app->request->post(); if ($model->load($post) && $dynamicModel->load($post)) { if ($model->validate() && $dynamicModel->validate()) { $ruleModel = new $dynamicModel->ruleNamespace(); $time = time(); $ruleModel->createdAt = $time; $ruleModel->updatedAt = $time; $model->data = serialize($ruleModel); if ($model->save(false)) { Yii::$app->getSession()->setFlash('success', Adm::t('', 'Data successfully changed!')); return Adm::redirect(['update', 'id' => $model->name]); } } } return $this->render('update', ['model' => $model, 'dynamicModel' => $dynamicModel]); }
/** * Validate class exists */ public function classExists() { if (!class_exists($this->className)) { $message = Yii::t('rbac-admin', "Unknown class '{class}'", ['class' => $this->className]); $this->addError('className', $message); return; } if (!is_subclass_of($this->className, Rule::className())) { $message = Yii::t('rbac-admin', "'{class}' must extend from 'yii\rbac\\Rule' or its child class", ['class' => $this->className]); $this->addError('className', $message); } }
/** * Validate class exists */ public function classExists() { $message = null; if (!class_exists($this->className)) { $message = 'Class "{className}" not exist'; } else { if (!is_subclass_of($this->className, \yii\rbac\Rule::className())) { $message = 'Class "{className}" must extends yii\\rbac\\Rule'; } else { if ((new $this->className())->name === null) { $message = 'The "{className}::\\$name" is not set'; } else { if ((new $this->className())->name !== $this->name) { $message = 'The "{className}::\\$name" is incorrect with the name of rule you have set'; } } } } if ($message !== null) { $this->addError('className', Yii::t('rbac', $message, ['className' => $this->className])); } }
/** * Validate class exists */ public function classExists() { if (!class_exists($this->className) || !is_subclass_of($this->className, Rule::className())) { $this->addError('className', "Unknown Class: {$this->className}"); } }
/** * 检查规则类是否存在 */ public function classExists() { if (!class_exists($this->className) || !is_subclass_of($this->className, \yii\rbac\Rule::className())) { $this->addError('className', "未知的规则类: {$this->className}"); } }