permission 模型 [ 2.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); //添加对应关系
}
}
熊本污 天下熊熊一般污
注册时间:2016-05-03
最后登录:2016-09-21
在线时长:6小时36分
最后登录:2016-09-21
在线时长:6小时36分
- 粉丝8
- 金钱615
- 威望140
- 积分2075
共 1 条评论
不错哟!值得收藏