UserRoleService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\user;
  12. use app\dict\sys\UserDict;
  13. use app\model\sys\SysRole;
  14. use app\model\sys\SysUserRole;
  15. use app\service\admin\sys\RoleService;
  16. use core\base\BaseAdminService;
  17. use core\exception\AdminException;
  18. use core\exception\CommonException;
  19. use think\facade\Cache;
  20. use think\Model;
  21. /**
  22. * 用户服务层
  23. * Class BaseService
  24. * @package app\service
  25. */
  26. class UserRoleService extends BaseAdminService
  27. {
  28. public static $role_cache_name = 'user_role_cache';
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->model = new SysUserRole();
  33. }
  34. /**
  35. * 添加用户权限(添加站点用户)
  36. * @param int $uid
  37. * @param array $data
  38. * @param int $site_id
  39. * @return true
  40. */
  41. public function add(int $uid, array $data, int $site_id = 0){
  42. $user_role_model = new SysUserRole();
  43. $is_exist = $user_role_model->where([ ['uid', '=', $uid], ['site_id', '=', $site_id] ])->count();
  44. if ($is_exist) throw new CommonException('SITE_USER_EXIST');
  45. $is_admin = $data['is_admin'] ?? 0;
  46. $role_data = array(
  47. 'uid' => $uid,
  48. 'is_admin' => $is_admin,
  49. 'site_id' => $site_id == 0 ? $this->site_id : $site_id,
  50. 'status' => $data['status'] ?? UserDict::ON
  51. );
  52. if(!$is_admin){
  53. //校验权限越界
  54. $role_data['role_ids'] = $data['role_ids'] ?? [];
  55. }
  56. $user_role_model->save($role_data);
  57. return true;
  58. }
  59. /**
  60. * 更新用户权限(编辑站点用户)
  61. * @param int $site_id
  62. * @param int $uid
  63. * @param array $role_ids
  64. * @return bool
  65. */
  66. public function edit(int $site_id, int $uid, array $role_ids){
  67. $user_role = $this->model->where([['uid', '=', $uid], ['site_id', '=', $site_id]])->findOrEmpty();
  68. if ($user_role->isEmpty())
  69. throw new AdminException('NO_SITE_USER_ROLE');
  70. $is_admin = $user_role->is_admin;
  71. if($is_admin)//超级管理员不允许改动权限
  72. throw new AdminException('ADMIN_NOT_ALLOW_EDIT_ROLE');
  73. if (!empty(array_diff_assoc($role_ids, $user_role->role_ids))) {
  74. //校验权限越界
  75. $user_role->save(['role_ids' => $role_ids]);
  76. $cache_name = 'user_role_'.$uid.'_'.$site_id;
  77. Cache::delete($cache_name);
  78. return true;
  79. }
  80. return false;
  81. }
  82. /**
  83. * 用户权限信息(获取用户对应站点权限)
  84. * @param int $site_id
  85. * @param int $uid
  86. * @return mixed
  87. */
  88. public function getUserRole(int $site_id, int $uid){
  89. $cache_name = 'user_role_'.$uid.'_'.$site_id;
  90. return cache_remember(
  91. $cache_name,
  92. function() use($uid, $site_id) {
  93. $user_role_model = new SysUserRole();
  94. $where = array(
  95. ['uid', '=', $uid],
  96. ['site_id', '=', $site_id]
  97. );
  98. return $user_role_model->where($where)->findOrEmpty()->toArray();
  99. },
  100. [self::$role_cache_name, RoleService::$cache_tag_name.$this->site_id]
  101. );
  102. }
  103. /**
  104. * 获取用户默认站点(切勿用于平台管理端)
  105. * @param int $uid
  106. * @return SysUserRole|array|mixed|Model
  107. */
  108. public function getUserDefaultSiteId(int $uid){
  109. $user_role_model = new SysUserRole();
  110. $default_site_id = $this->request->defaultSiteId();
  111. return $user_role_model->where([['uid', '=', $uid], ['site_id', '<>', $default_site_id]])->findOrEmpty()?->site_id;
  112. }
  113. /**
  114. * 通过角色id组获取角色
  115. * @param array $role_ids
  116. * @param int $site_id
  117. * @return mixed
  118. */
  119. public function getRoleByUserRoleIds(array $role_ids, int $site_id){
  120. sort($role_ids);
  121. $cache_name = 'role_by_ids_'.md5(implode(',', $role_ids)).'_'.$site_id;
  122. return cache_remember(
  123. $cache_name,
  124. function() use($role_ids, $site_id) {
  125. $where = array(
  126. ['role_id', 'in', $role_ids],
  127. ['site_id', '=', $site_id]
  128. );
  129. return SysRole::where($where)->column('role_name');
  130. },
  131. [self::$role_cache_name, RoleService::$cache_tag_name.$this->site_id]
  132. );
  133. }
  134. }