UserService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\Owner;
  5. use App\User;
  6. use App\UserWorkgroup;
  7. use Firebase\JWT\JWT;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Gate;
  12. use App\Traits\ServiceAppAop;
  13. class UserService
  14. {
  15. use ServiceAppAop;
  16. protected $modelClass=User::class;
  17. /** @var CacheService $cacheService */
  18. private $cacheService;
  19. function __construct(){
  20. $this->cacheService = app('CacheService');
  21. }
  22. function hasRoles(User $user, $roles){
  23. $thisRoles=$this->cacheService->getOrExecute("user{$user['id']}->roles",function()use($user){
  24. return $user->roles;
  25. });
  26. return !!$roles->intersect($thisRoles)->count();
  27. }
  28. function getPermittingOwnerIds($user=null){
  29. if(!$user)return [];
  30. return $this->cacheService->getOrExecute("user{$user['id']}->getPermittingOwnerIds",function()use($user){
  31. return $user->getPermittingOwnerIdsAttribute() ?? [];
  32. })??[];
  33. }
  34. /**
  35. * 检查用户的管理员身份
  36. *
  37. * @param integer $userId
  38. *
  39. * @return bool
  40. */
  41. private function checkAdminIdentity($userId):bool
  42. {
  43. if ($userId == Auth::id())return array_search(Auth::user()["name"],config("users.superAdmin"))!==false;
  44. /** @var User|\stdClass $user */
  45. $user = User::query()->select("name")->find($userId);
  46. if (!$user)return false;
  47. return array_search($user->name,config("users.superAdmin"))!==false;
  48. }
  49. /**
  50. * @param integer|null $userId
  51. *
  52. * @return array
  53. */
  54. public function getUserHasOwners($userId = null)
  55. {
  56. if (!$userId)$userId = Auth::id();
  57. $key = "owners:user_".$userId;
  58. if (!Cache::has($key)){
  59. if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(Owner::query()->select("id")->whereNull("deleted_at")->get()->toArray(),"id"));
  60. else{
  61. $owners = [];
  62. /** @var User|\stdClass $user */
  63. $user = new User();
  64. $user->id = $userId;
  65. $user->load("roles.owners");
  66. $user->roles->each(function ($role)use (&$owners){
  67. $owners = array_merge($owners,array_column($role->owners->toArray(),"id"));
  68. });
  69. Cache::forever($key,$owners);
  70. }
  71. }
  72. return Cache::get($key);
  73. }
  74. /**
  75. * @param integer|null $userId
  76. *
  77. * @return array
  78. */
  79. public function getUserHasUserWorkGroups($userId = null)
  80. {
  81. if (!$userId)$userId = Auth::id();
  82. $key = "userWorkGroups:user_".$userId;
  83. if (!Cache::has($key)){
  84. if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(UserWorkgroup::query()->select("id")->get()->toArray(),"id"));
  85. else{
  86. $userWorkGroups = [];
  87. /** @var User|\stdClass $user */
  88. $user = new User();
  89. $user->id = $userId;
  90. $user->load("roles.userWorkGroups");
  91. $user->roles->each(function ($role)use (&$userWorkGroups){
  92. $userWorkGroups = array_merge($userWorkGroups,array_column($role->userWorkGroups->toArray(),"id"));
  93. });
  94. Cache::forever($key,$userWorkGroups);
  95. }
  96. }
  97. return Cache::get($key);
  98. }
  99. /**
  100. * 清除用户缓存
  101. *
  102. * @param User $user
  103. */
  104. public function clearUserCache(User $user)
  105. {
  106. Cache::tags("authorities:user")->forget("authorities:user_".$user->id);
  107. Cache::forget("owners:user_".$user->id);
  108. Cache::forget("userWorkGroups:user_".$user->id);
  109. Cache::tags("AUTHORITY_MENU_MAPPING")->forget("am_mapping_".$user->id);
  110. }
  111. /**
  112. * 获取JWT token
  113. *
  114. * @param User|\stdClass $user
  115. * @param mixed $key
  116. * @return string
  117. */
  118. public function getJWTToken($user,$key):string
  119. {
  120. $time = time();
  121. $payload = [
  122. 'iss' => $_SERVER["HTTP_HOST"], //签发者
  123. 'iat' => $time,
  124. 'nbf' => $time,
  125. 'exp' => $time+7200,
  126. 'data' => [
  127. 'id' => $user->id,
  128. 'username' => $user->name
  129. ]
  130. ];
  131. $alg = 'RS256';
  132. return JWT::encode($payload, $key, $alg);
  133. }
  134. /**
  135. * 获取JWT token
  136. *
  137. * @param User|\stdClass $user
  138. * @param mixed $key
  139. * @return string
  140. */
  141. public function checkJWTToken($user,$key):string
  142. {
  143. $time = time();
  144. $payload = [
  145. 'iss' => $_SERVER["HTTP_HOST"], //签发者
  146. 'iat' => $time,
  147. 'nbf' => $time,
  148. 'exp' => $time+7200,
  149. 'data' => [
  150. 'id' => $user->id,
  151. 'username' => $user->name
  152. ]
  153. ];
  154. $alg = 'RS256';
  155. return JWT::encode($payload, $key, $alg);
  156. }
  157. }