RoleService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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\sys;
  12. use app\dict\sys\RoleStatusDict;
  13. use app\model\sys\SysRole;
  14. use app\model\sys\SysUserRole;
  15. use app\service\admin\auth\AuthService;
  16. use app\service\admin\site\SiteService;
  17. use core\base\BaseAdminService;
  18. use core\exception\AdminException;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Cache;
  23. /**
  24. * admin授权服务层
  25. * Class BaseService
  26. * @package app\service
  27. */
  28. class RoleService extends BaseAdminService
  29. {
  30. public static $cache_tag_name = 'role_cache';
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. $this->model = new SysRole();
  35. }
  36. /**
  37. * 管理端获取角色列表
  38. * @param array $data
  39. * @return array
  40. */
  41. public function getPage(array $data)
  42. {
  43. $where = [['site_id', '=', $this->site_id]];
  44. if(!empty($data['role_name'])) {
  45. $where[] = ['role_name', 'like', "%".$data['role_name']."%"];
  46. }
  47. $field = 'role_id,role_name,status,create_time';
  48. $search_model = $this->model->where($where)->field($field)->order('create_time desc')->append(['status_name']);
  49. return $this->pageQuery($search_model);
  50. }
  51. /**
  52. * 获取权限信息
  53. * @param int $role_id
  54. * @return array
  55. */
  56. public function getInfo(int $role_id){
  57. return $this->model->append(['status_name'])->findOrEmpty($role_id)->toArray();
  58. }
  59. /**
  60. * 获取站点下的所有权限
  61. * @return array
  62. * @throws DataNotFoundException
  63. * @throws DbException
  64. * @throws ModelNotFoundException
  65. */
  66. public function getAll()
  67. {
  68. $where = array(
  69. ['site_id', '=', $this->site_id],
  70. ['status', '=', 1]
  71. );
  72. $site_role_all = $this->model->where($where)->field('role_id,role_name,rules,status,create_time')->select()->toArray();
  73. foreach ($site_role_all as $key => $value) {
  74. $site_role_all[$key]['disabled'] = false;
  75. }
  76. if (AuthService::isSuperAdmin()) {
  77. $is_admin = 1;
  78. } else {
  79. $user_role_info = (new AuthService())->getAuthRole($this->site_id);
  80. if(empty($user_role_info))
  81. return [];
  82. $is_admin = $user_role_info['is_admin'];//是否是超级管理员组
  83. }
  84. if (!$is_admin) {
  85. $user_role_ids = $user_role_info['role_ids'];
  86. $role_service = new RoleService();
  87. $menu_keys = $role_service->getMenuIdsByRoleIds($this->site_id, $user_role_ids);
  88. foreach ($site_role_all as $key => $value) {
  89. if (!empty(array_diff($value['rules'], $menu_keys))) {
  90. $site_role_all[$key]['disabled'] = true;
  91. }
  92. }
  93. }
  94. foreach ($site_role_all as &$value) {
  95. unset($value['rules']);
  96. }
  97. return $site_role_all;
  98. }
  99. /**
  100. * 新增权限
  101. * @param array $data
  102. * @return true
  103. */
  104. public function add(array $data){
  105. $data['create_time'] = time();
  106. $data['app_type'] = $this->app_type;
  107. $data['site_id'] = $this->site_id;
  108. $this->model->save($data);
  109. Cache::tag(self::$cache_tag_name.$this->site_id)->clear();
  110. return true;
  111. }
  112. /**
  113. * 更新权限
  114. * @param int $role_id
  115. * @param array $data
  116. * @return true
  117. */
  118. public function edit(int $role_id, array $data){
  119. $where = array(
  120. ['role_id', '=', $role_id],
  121. ['site_id', '=', $this->site_id],
  122. );
  123. $data['update_time'] = time();
  124. $this->model->update($data, $where);
  125. Cache::tag(self::$cache_tag_name.$this->site_id)->clear();
  126. return true;
  127. }
  128. /**
  129. * 获取模型对象
  130. * @param int $site_id
  131. * @param int $role_id
  132. * @return mixed
  133. */
  134. public function find(int $site_id, int $role_id){
  135. $where = array(
  136. ['role_id', '=', $role_id],
  137. ['site_id', '=', $site_id],
  138. );
  139. $role = $this->model->where($where)->findOrEmpty();
  140. if ($role->isEmpty())
  141. throw new AdminException('USER_ROLE_NOT_EXIST');
  142. return $role;
  143. }
  144. /**
  145. * 删除权限(saas应该不允许删除)
  146. * @param int $role_id
  147. * @return mixed
  148. * @throws DbException
  149. */
  150. public function del(int $role_id){
  151. $role = $this->find($this->site_id, $role_id);
  152. if(SysUserRole::where([['role_ids', 'like',['%"'.$role_id.'"%']]])->count() > 0)
  153. throw new AdminException('USER_ROLE_NOT_ALLOW_DELETE');
  154. $res = $role->delete();
  155. Cache::tag(self::$cache_tag_name.$this->site_id)->clear();
  156. return $res;
  157. }
  158. /**
  159. * 获取角色id为健名,角色名为键值的数据
  160. * @param int $site_id
  161. * @return mixed|string
  162. */
  163. public function getColumn(int $site_id){
  164. $cache_name = 'role_column_'.$site_id;
  165. return cache_remember(
  166. $cache_name,
  167. function() use($site_id) {
  168. $where = [
  169. ['site_id', '=', $site_id]
  170. ];
  171. return $this->model->where($where)->column('role_name', 'role_id');
  172. },
  173. [MenuService::$cache_tag_name, self::$cache_tag_name.$this->site_id]
  174. );
  175. }
  176. /**
  177. * 通过权限组id获取菜单id
  178. * @param int $site_id
  179. * @param array $role_ids
  180. * @return array
  181. * @throws DataNotFoundException
  182. * @throws DbException
  183. * @throws ModelNotFoundException
  184. */
  185. public function getMenuIdsByRoleIds(int $site_id, array $role_ids){
  186. $menu_keys = (new SiteService())->getMenuIdsBySiteId($site_id, 1);
  187. $allow_role_ids = array_merge($role_ids, $menu_keys);
  188. sort($allow_role_ids);
  189. $cache_name = 'user_role_'.$site_id.'_'.md5(implode('_', $allow_role_ids));
  190. return cache_remember(
  191. $cache_name,
  192. function() use($role_ids, $menu_keys) {
  193. $rules = $this->model->where([['role_id', 'IN', $role_ids], ['status', '=', RoleStatusDict::ON]])->field('rules')->select()->toArray();
  194. if(!empty($rules)){
  195. $temp = [];
  196. foreach($rules as $k => $v){
  197. $temp = array_merge($temp, $v['rules']);
  198. }
  199. $temp = array_unique($temp);
  200. if(empty($menu_keys)) return [];
  201. if(empty($temp)) return [];
  202. return array_intersect($temp, $menu_keys);
  203. }
  204. return [];
  205. },
  206. [MenuService::$cache_tag_name, self::$cache_tag_name.$site_id]
  207. );
  208. }
  209. }