123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace catchAdmin\permissions\model;
- use catchAdmin\permissions\model\search\RolesSearch;
- use catchAdmin\permissions\model\DataRangScopeTrait;
- use catcher\base\CatchModel;
- use think\facade\Db;
- use catcher\Utils;
- class Roles extends CatchModel
- {
- use HasDepartmentsTrait;
- use HasPermissionsTrait;
- use RolesSearch;
- use DataRangScopeTrait;
-
- 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 = [
- 'id',
- 'role_name',
- 'identify',
- 'parent_id',
- 'creator_id',
- 'data_range',
- 'description',
- 'created_at',
- 'updated_at',
- 'deleted_at',
-
- ];
- public function getList()
- {
- $where = array();
- if (!Utils::isSuperAdmin()) {
- $user = request()->user();
-
-
- $role_ids = Db::table("user_has_roles")->where('uid',$user->id)->column('role_id');
- $childIds = $this->getChildrenRoleIds($role_ids);
- $roleIds = array_merge($role_ids,$childIds);
- if (!empty($roleIds)) {
- $where[] = ['id','in',$roleIds];
- }
- }
-
-
- return $this->where($where)
-
- ->catchSearch()
- ->order('id', 'desc')
- ->where('id','<>',1)
- ->select()
- ->toTree();
- }
-
- public function isAllDataRange(){
- $user = request()->user();
- if (empty($roles)) {
- $roles = $user->getRoles();
- }
-
- foreach ($roles as $role) {
-
- if ($role->data_range == Roles::ALL_DATA) {
- return true;
- }
- }
- return false;
- }
-
- public function getPartList()
- {
- $where = array();
- if (!Utils::isSuperAdmin()) {
- $user = request()->user();
- $roleIds = Db::table("user_has_roles")->where('uid',$user->id)->column('role_id');
- if (!empty($roleIds)) {
- $where[] = ['id','in',$roleIds];
- }
- }
-
- $no_display_roles = static::getDeviceUserRoleids();
- return $this->where($where)
- ->whereNotIn('id',$no_display_roles)
- ->catchSearch()
- ->order('id', 'desc')
- ->select()
- ->toTree();
- }
-
- public function users(): \think\model\relation\BelongsToMany
- {
- return $this->belongsToMany(Users::class, 'user_has_roles', 'uid', 'role_id');
- }
-
- public static function getChildrenRoleIds($roleIds)
- {
- $roleIds = Roles::field(['id', 'parent_id'])->select()->getAllChildrenIds($roleIds);
-
- return $roleIds;
- }
-
- public function getParentRoleIds($id)
- {
- $ids = [];
- $pid = Roles::where('id', $id)->value('parent_id');
-
- if ($pid > 0) {
- $ids[] = $pid;
- $parentId = $this->getParentRoleIds($pid);
- $ids = array_merge($ids, $parentId);
- }
- return $ids;
- }
-
- public static function getDeviceUserRoleids()
- {
- return static::whereIn('identify','personal,group_card_user,group_badge_user')->column('id');
- }
- }
|