熊本污 2016-05-03 20:15:27 3769次浏览 1条评论 0 0 0
<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "auth_item".
 *
 * @property string $name
 * @property integer $type
 * @property string $description
 * @property string $rule_name
 * @property string $data
 * @property integer $created_at
 * @property integer $updated_at
 *
 * @property AuthAssignment[] $authAssignments
 * @property AuthRule $ruleName
 * @property AuthItemChild[] $authItemChildren
 * @property AuthItemChild[] $authItemChildren0
 */
class Permission extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'auth_item';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name','description'], 'required','message'=>'必填'],
            [['type', 'created_at', 'updated_at'], 'integer'],
            [['description', 'data'], 'string'],
            [['name', 'rule_name'], 'string', 'max' => 64]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'name' => '权限名称 格式为User/index',
            'description' => "权限描述",
            
        ];
    }

    /**
     * @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']);
    }
    /** 
     * 
     *  创建权限
     */
        public function createPermission($name)
    {    
        $auth = Yii::$app->authManager;    
        $createPost = $auth->createPermission($name['Permission']['name']);    
        $createPost->description =  $name['Permission']['description'];    
        return $auth->add($createPost);
    }
    /**
     * 创建角色
     */
        public function createRole($name)
    {    
        $auth = Yii::$app->authManager;    
        $role = $auth->createRole($name['Permission']['name']);    
        $role->description = $name['Permission']['description'];    
        return   $auth->add($role);
    }
     
    /**
     *  给用户分配角色(用户-角色关联表)
     */
            public function addUserRole($items)
     {    
         $auth = Yii::$app->authManager;    
         $role = $auth->createRole($items['role']);                //创建角色对象
         $user_id = $items['user'];                                             //获取用户id,此处假设用户id=1
         return  $auth->assign($role, $user_id);                           //添加对应关系
     }
     
     /**
      *  角色分配权限(角色-权限关联表)
      */
        
         public function addRoleNode($items)
    {    
        $auth = Yii::$app->authManager;    
        $parent = $auth->createRole($items['role']);                //创建角色对象
        $child = $auth->createPermission($items['permission']);     //创建权限对象
        return $auth->addChild($parent, $child);                           //添加对应关系
    }
           
}

您需要登录后才可以评论。登录 | 立即注册