AuthService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\auth;
  12. use app\dict\site\SiteDict;
  13. use app\model\sys\SysUserRole;
  14. use app\Request;
  15. use app\service\admin\site\SiteUserService;
  16. use app\service\admin\sys\MenuService;
  17. use app\service\admin\sys\RoleService;
  18. use app\service\admin\user\UserRoleService;
  19. use app\service\admin\user\UserService;
  20. use app\service\core\site\CoreSiteService;
  21. use core\base\BaseAdminService;
  22. use core\exception\AuthException;
  23. use Exception;
  24. use think\facade\Cache;
  25. /**
  26. * 用户服务层
  27. * Class AuthService
  28. * @package app\service\admin\auth
  29. */
  30. class AuthService extends BaseAdminService
  31. {
  32. /**
  33. * 校验用户和传入站点是否存在从属关系
  34. * @param Request $request
  35. * @return true
  36. */
  37. public function checkSiteAuth(Request $request){
  38. $site_id = $request->adminSiteId();
  39. //todo 将站点编号转化为站点id
  40. $site_info = (new CoreSiteService())->getSiteCache($site_id);
  41. //站点不存在
  42. if(empty($site_info)) throw new AuthException('SITE_NOT_EXIST');
  43. //没有当前站点的信息
  44. if (!AuthService::isSuperAdmin()) {
  45. if(!$this->getAuthRole($site_id)) throw new AuthException('NO_SITE_PERMISSION');
  46. }
  47. $request->siteId($site_id);
  48. $request->appType($site_info['app_type']);
  49. return true;
  50. }
  51. /**
  52. * 校验权限
  53. * @param Request $request
  54. * @return bool
  55. * @throws Exception
  56. */
  57. public function checkRole(Request $request){
  58. $rule = strtolower(trim($request->rule()->getRule()));
  59. $method = strtolower(trim($request->method()));
  60. $site_info = (new AuthSiteService())->getSiteInfo();
  61. if($method != 'get'){
  62. if($site_info['status'] == SiteDict::EXPIRE) throw new AuthException('SITE_EXPIRE_NOT_ALLOW');
  63. if($site_info['status'] == SiteDict::CLOSE) throw new AuthException('SITE_CLOSE_NOT_ALLOW');
  64. }
  65. $menu_service = new MenuService();
  66. $all_menu_list = $menu_service->getAllApiList($this->app_type);
  67. //先判断当前访问的接口是否收到权限的限制
  68. $method_menu_list = $all_menu_list[$method] ?? [];
  69. if(!in_array($rule, $method_menu_list))
  70. return true;
  71. $auth_role_list = $this->getAuthApiList();
  72. if(!empty($auth_role_list[$method]) && in_array($rule, $auth_role_list[$method]))
  73. return true;
  74. throw new AuthException('NO_PERMISSION');
  75. }
  76. /**
  77. * 获取授权用户的权限信息
  78. * @return mixed
  79. */
  80. public function getAuthRole(int $site_id){
  81. $user_role_service = new UserRoleService();
  82. return $user_role_service->getUserRole($site_id, $this->uid);
  83. }
  84. /**
  85. * 当前授权用户接口权限
  86. * @return array
  87. */
  88. public function getAuthApiList(){
  89. if (AuthService::isSuperAdmin()) {
  90. $is_admin = 1;
  91. } else {
  92. $user_role_info = $this->getAuthRole($this->site_id);
  93. if (empty($user_role_info))
  94. return [];
  95. $is_admin = $user_role_info['is_admin'];//是否是超级管理员组
  96. }
  97. $menu_service = new MenuService();
  98. if($is_admin){//查询全部启用的权限
  99. //获取站点信息
  100. return (new AuthSiteService())->getApiList(1);
  101. }else{
  102. $user_role_ids = $user_role_info['role_ids'];
  103. $role_service = new RoleService();
  104. $menu_keys = $role_service->getMenuIdsByRoleIds($this->site_id, $user_role_ids);
  105. return $menu_service->getApiListByMenuKeys($menu_keys, $this->app_type);
  106. }
  107. }
  108. /**
  109. * 当前授权用户菜单权限
  110. * @return array
  111. */
  112. public function getAuthMenuList(int $is_tree = 0, $addon = 'all'){
  113. if (AuthService::isSuperAdmin()) {
  114. $is_admin = 1;
  115. } else {
  116. $user_role_info = $this->getAuthRole($this->site_id);
  117. if(empty($user_role_info))
  118. return [];
  119. $is_admin = $user_role_info['is_admin'];//是否是超级管理员组
  120. }
  121. $menu_service = new MenuService();
  122. if($is_admin){//查询全部启用的权限
  123. return ( new MenuService() )->getAllMenuList($this->app_type, 1, $is_tree, 1);
  124. }else{
  125. $user_role_ids = $user_role_info['role_ids'];
  126. $role_service = new RoleService();
  127. $menu_keys = $role_service->getMenuIdsByRoleIds($this->site_id, $user_role_ids);
  128. return $menu_service->getMenuListByMenuKeys($this->site_id, $menu_keys, $this->app_type, $is_tree, $addon);
  129. }
  130. }
  131. /**
  132. * 获取授权用户信息
  133. */
  134. public function getAuthInfo(){
  135. return (new SiteUserService())->getInfo($this->uid);
  136. }
  137. /**
  138. * 修改用户权限
  139. * @param string $field
  140. * @param $data
  141. * @return bool
  142. */
  143. public function modifyAuth(string $field, $data){
  144. return (new SiteUserService())->modify($this->uid, $field, $data);
  145. }
  146. /**
  147. * 修改用户
  148. * @param array $data
  149. * @return true
  150. */
  151. public function editAuth(array $data){
  152. if(!empty($data['password'])){
  153. //检测原始密码是否正确
  154. $user = (new UserService())->find($this->uid);
  155. if(!check_password($data['original_password'], $user->password))
  156. throw new AuthException('OLD_PASSWORD_ERROR');
  157. }
  158. return (new UserService())->edit($this->uid, $data);
  159. }
  160. /**
  161. * 是否是超级管理员
  162. * @return bool
  163. */
  164. public static function isSuperAdmin() {
  165. $super_admin_uid = Cache::get('super_admin_uid');
  166. if (!$super_admin_uid) {
  167. $super_admin_uid = (new SysUserRole())->where([
  168. ['site_id', '=', request()->defaultSiteId()],
  169. ['is_admin', '=', 1]
  170. ])->value('uid');
  171. Cache::set('super_admin_uid', $super_admin_uid);
  172. }
  173. return $super_admin_uid == (new self())->uid;
  174. }
  175. }