64], [['rule_name'], 'exist', 'skipOnError' => true, 'targetClass' => AuthRule::className(), 'targetAttribute' => ['rule_name' => 'name']], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'name' => '名称', 'type' => '类型', 'description' => '描述', 'rule_name' => '规则名称', 'data' => 'Data', 'created_at' => '创建时间', 'updated_at' => '更新时间', ]; } /** * @return \yii\db\ActiveQuery */ public function getAuthAssignments() { return $this->hasMany(AuthAssignment::className(), ['item_name' => 'name']); } /** * @return \yii\db\ActiveQuery */ public function getRuleName() { return $this->hasOne(AuthRule::className(), ['name' => 'rule_name']); } /** * @return \yii\db\ActiveQuery */ public function getAuthItemChildren() { return $this->hasMany(AuthItemChild::className(), ['parent' => 'name']); } /** * @return \yii\db\ActiveQuery */ public function getAuthItemChildren0() { return $this->hasMany(AuthItemChild::className(), ['child' => 'name']); } /** * @return \yii\db\ActiveQuery */ public function getChildren() { return $this->hasMany(AuthItem::className(), ['name' => 'child'])->viaTable('auth_item_child', ['parent' => 'name']); } /** * @return \yii\db\ActiveQuery */ public function getParents() { return $this->hasMany(AuthItem::className(), ['name' => 'parent'])->viaTable('auth_item_child', ['child' => 'name']); } /** * 场景设置 * @see \yii\base\Model::scenarios() */ public function scenarios() { $scenarios = [ self:: SCENARIOS_CREATE => ['name', 'type', 'description'], self:: SCENARIOS_DELETE => ['name'], self:: SCENARIOS_UPDATE => ['name', 'type', 'description'], ]; return array_merge(parent:: scenarios(), $scenarios); } /** * 角色&权限的创建方法 * @return boolean 返回成功或者失败的状态值 */ public function addItem() { //实例化AuthManager类 $auth = Yii::$app->authManager; if($this->type == self::T_ROLE){ $item = $auth->createRole($this->name); $item->description = $this->description?:'创建['.$this->name.']角色'; }else{ $item = $auth->createPermission($this->name); $item->description = $this->description?:'创建['.$this->name.']权限'; } return $auth->add($item); } /** * 角色&权限的删除方法 * @return boolean 返回成功或者失败的状态值 */ public function romoveItem() { $this->name = trim($this->name); if($this->validate()){ $auth = Yii::$app->authManager; $item = $auth->getRole($this->name)?:$auth->getPermission($this->name); return $auth->remove($item); } return false; } /** * 获取权限 * @param unknown $id * @throws \Exception */ public function getItem($id) { $model = AuthItem:: findOne(['name'=>$id]); if(!$model) throw new \Exception( '编辑的角色或权限不存在!' ); $this-> name = $model-> name; $this-> type = $model-> type; $this-> description = $model-> description; return $this; } /** * 编辑权限 * @param unknown $item_name * @return boolean */ public function updateItem($item_name) { $auth = Yii:: $app-> authManager; if($this->type == self::T_ROLE){ $item = $auth->createRole($this-> name); $item-> description = $this-> description?: '创建['.$this-> name. ']角色'; }else{ $item = $auth->createPermission($this-> name); $item-> description = $this-> description?: '创建['.$this-> name. ']权限'; } return $auth->update($item_name, $item); } //获取所有的权限 public function getAllPermission(){ $permission = self::find()->where(['type'=>2])->all(); return $permission; } //关联菜单数据 public function getPermissionName(){ return $this->hasOne(Menu::className(), ['route' => 'name']); } }