UserService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\Owner;
  5. use App\User;
  6. use App\UserWorkgroup;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Gate;
  11. use App\Traits\ServiceAppAop;
  12. class UserService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=User::class;
  16. /** @var CacheService $cacheService */
  17. private $cacheService;
  18. function __construct(){
  19. $this->cacheService = app('CacheService');
  20. }
  21. function hasRoles(User $user, $roles){
  22. $thisRoles=$this->cacheService->getOrExecute("user{$user['id']}->roles",function()use($user){
  23. return $user->roles;
  24. });
  25. return !!$roles->intersect($thisRoles)->count();
  26. }
  27. function getPermittingOwnerIds($user=null){
  28. if(!$user)return [];
  29. return $this->cacheService->getOrExecute("user{$user['id']}->getPermittingOwnerIds",function()use($user){
  30. return $user->getPermittingOwnerIdsAttribute() ?? [];
  31. })??[];
  32. }
  33. /**
  34. * 检查用户的管理员身份
  35. *
  36. * @param integer $userId
  37. *
  38. * @return bool
  39. */
  40. private function checkAdminIdentity($userId):bool
  41. {
  42. if ($userId == Auth::id())return array_search(Auth::user()["name"],config("users.superAdmin"))!==false;
  43. /** @var User|\stdClass $user */
  44. $user = User::query()->select("name")->find($userId);
  45. if (!$user)return false;
  46. return array_search($user->name,config("users.superAdmin"))!==false;
  47. }
  48. /**
  49. * @param integer|null $userId
  50. *
  51. * @return array
  52. */
  53. public function getUserHasOwners($userId = null)
  54. {
  55. if (!$userId)$userId = Auth::id();
  56. $key = "owners:user_".$userId;
  57. if (!Cache::has($key)){
  58. if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(Owner::query()->select("id")->whereNull("deleted_at")->get()->toArray(),"id"));
  59. else{
  60. $owners = [];
  61. /** @var User|\stdClass $user */
  62. $user = new User();
  63. $user->id = $userId;
  64. $user->load("roles.owners");
  65. $user->roles->each(function ($role)use (&$owners){
  66. $owners = array_merge($owners,array_column($role->owners->toArray(),"id"));
  67. });
  68. Cache::forever($key,$owners);
  69. }
  70. }
  71. return Cache::get($key);
  72. }
  73. /**
  74. * @param integer|null $userId
  75. *
  76. * @return array
  77. */
  78. public function getUserHasUserWorkGroups($userId = null)
  79. {
  80. if (!$userId)$userId = Auth::id();
  81. $key = "userWorkGroups:user_".$userId;
  82. if (!Cache::has($key)){
  83. if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(UserWorkgroup::query()->select("id")->get()->toArray(),"id"));
  84. else{
  85. $userWorkGroups = [];
  86. /** @var User|\stdClass $user */
  87. $user = new User();
  88. $user->id = $userId;
  89. $user->load("roles.userWorkGroups");
  90. $user->roles->each(function ($role)use (&$userWorkGroups){
  91. $userWorkGroups = array_merge($userWorkGroups,array_column($role->userWorkGroups->toArray(),"id"));
  92. });
  93. Cache::forever($key,$userWorkGroups);
  94. }
  95. }
  96. return Cache::get($key);
  97. }
  98. }