修改权限模型

This commit is contained in:
wuyanwen 2020-01-14 08:23:29 +08:00
parent b6b0b5170d
commit 2acf23d296
2 changed files with 58 additions and 58 deletions

View File

@ -49,4 +49,20 @@ class Permissions extends CatchModel
{ {
return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id'); return $this->belongsToMany(Roles::class, 'role_has_permissions', 'role_id', 'permission_id');
} }
/**
* 获取当前用户权限
*
* @time 2020年01月14日
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\Collection
*/
public static function getCurrentUserPermissions(): \think\Collection
{
return parent::whereIn('id', request()->user()->getPermissionsBy())
->field(['permission_name as title', 'route', 'icon'])
->select();
}
} }

View File

@ -8,16 +8,24 @@ use catcher\base\CatchModel;
class Roles extends CatchModel class Roles extends CatchModel
{ {
use HasDepartmentsTrait; use HasDepartmentsTrait;
use HasPermissionsTrait;
use RolesSearch; use RolesSearch;
protected $name = 'roles'; protected $name = 'roles';
public const ALL_DATA = 1; // 全部数据
public const SELF_CHOOSE = 2; // 自定义数据
public const SELF_DATA = 3; // 本人数据
public const DEPARTMENT_DATA = 4; // 部门数据
public const DEPARTMENT_DOWN_DATA = 5; // 部门及以下数据
protected $field = [ protected $field = [
'id', // 'id', //
'role_name', // 角色名 'role_name', // 角色名
'parent_id', // 父级ID 'parent_id', // 父级ID
'creator_id', 'creator_id', // 创建者
'data_range', 'data_range', // 数据范围
'description', // 角色备注 'description', // 角色备注
'created_at', // 创建时间 'created_at', // 创建时间
'updated_at', // 更新时间 'updated_at', // 更新时间
@ -43,65 +51,41 @@ class Roles extends CatchModel
return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id'); return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
} }
/**
*
* @time 2019年12月09日
* @return \think\model\relation\BelongsToMany
*/
public function permissions(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Permissions::class, 'role_has_permissions', 'permission_id', 'role_id');
}
/** public static function getDepartmentUserIdsBy($roles)
*
* @time 2019年12月08日
* @param array $condition
* @param array $field
* @return mixed
*/
public function getPermissions($condition = [], $field = [])
{ {
return $this->permissions() $uids = [];
->when(!empty($field), function ($query) use ($field){
$query->field($field);
})
->when(!empty($condition), function ($query) use ($condition){
$query->where($condition);
})
->select();
}
/** $isAll = false;
*
* @time 2019年12月08日 $user = request()->user();
* @param array $permissions
* @return mixed foreach ($roles as $role) {
* @throws \think\db\exception\DbException switch ($role->data_range) {
*/ case self::ALL_DATA:
public function attach(array $permissions) $isAll = true;
{ break;
if (empty($permissions)) { case self::SELF_CHOOSE:
return true; $departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
$uids = array_merge($uids, Users::getUserIdsByDepartmentIds($departmentIds));
break;
case self::SELF_DATA:
$uids[] = $user->id;
break;
case self::DEPARTMENT_DOWN_DATA:
case self::DEPARTMENT_DATA:
$uids = array_merge($uids, Users::getUserIdsByDepartmentIds([$user->department_id]));
break;
default:
break;
}
// 如果有全部数据 直接跳出
if ($isAll) {
break;
}
} }
sort($permissions); return $uids;
return $this->permissions()->attach($permissions);
}
/**
*
* @time 2019年12月08日
* @param array $roles
* @return mixed
*/
public function detach(array $roles = [])
{
if (empty($roles)) {
return $this->permissions()->detach();
}
return $this->permissions()->detach($roles);
} }
} }