92 lines
2.5 KiB
PHP
Raw Normal View History

2019-12-09 16:22:00 +08:00
<?php
namespace catchAdmin\permissions\model;
2020-01-13 21:23:24 +08:00
use catchAdmin\permissions\model\search\RolesSearch;
2019-12-09 16:22:00 +08:00
use catchAdmin\user\model\Users;
2019-12-11 21:00:14 +08:00
use catcher\base\CatchModel;
2019-12-09 16:22:00 +08:00
2019-12-11 21:00:14 +08:00
class Roles extends CatchModel
2019-12-09 16:22:00 +08:00
{
2020-01-12 12:54:59 +08:00
use HasDepartmentsTrait;
2020-01-14 08:23:29 +08:00
use HasPermissionsTrait;
2020-01-13 21:23:24 +08:00
use RolesSearch;
2020-01-12 12:54:59 +08:00
2019-12-09 16:22:00 +08:00
protected $name = 'roles';
2020-01-14 08:23:29 +08:00
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; // 部门及以下数据
2019-12-09 16:22:00 +08:00
protected $field = [
'id', //
2019-12-11 21:00:14 +08:00
'role_name', // 角色名
2019-12-09 16:22:00 +08:00
'parent_id', // 父级ID
2020-01-14 08:23:29 +08:00
'creator_id', // 创建者
'data_range', // 数据范围
2019-12-09 16:22:00 +08:00
'description', // 角色备注
'created_at', // 创建时间
'updated_at', // 更新时间
'deleted_at', // 删除状态0未删除 >0 已删除
];
2020-01-13 21:23:24 +08:00
public function getList()
2019-12-09 16:22:00 +08:00
{
2020-01-13 21:23:24 +08:00
return $this->catchSearch()
->order('id', 'desc')
->select()
->toArray();
2019-12-09 16:22:00 +08:00
}
/**
*
* @time 2019年12月08日
* @return \think\model\relation\BelongsToMany
*/
public function users(): \think\model\relation\BelongsToMany
{
2019-12-12 09:13:29 +08:00
return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
2019-12-09 16:22:00 +08:00
}
2020-01-14 08:23:29 +08:00
public static function getDepartmentUserIdsBy($roles)
2019-12-09 16:22:00 +08:00
{
2020-01-14 08:23:29 +08:00
$uids = [];
2019-12-09 16:22:00 +08:00
2020-01-14 08:23:29 +08:00
$isAll = false;
2019-12-11 21:00:14 +08:00
2020-01-14 08:23:29 +08:00
$user = request()->user();
2019-12-12 18:52:33 +08:00
2020-01-14 08:23:29 +08:00
foreach ($roles as $role) {
switch ($role->data_range) {
case self::ALL_DATA:
$isAll = true;
break;
case self::SELF_CHOOSE:
$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;
}
2019-12-09 16:22:00 +08:00
2020-01-14 08:23:29 +08:00
// 如果有全部数据 直接跳出
if ($isAll) {
break;
}
2019-12-11 21:00:14 +08:00
}
2020-01-14 08:23:29 +08:00
return $uids;
2019-12-09 16:22:00 +08:00
}
2019-12-26 09:03:09 +08:00
}