AuthorityService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use App\User;
  5. use Illuminate\Database\Eloquent\Builder;
  6. use Illuminate\Database\Eloquent\Collection;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Support\Facades\Cache;
  9. use App\Traits\ServiceAppAop;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Gate;
  12. class AuthorityService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=Authority::class;
  16. /**
  17. * 携带缓存的权限数据
  18. *
  19. * @return array|Collection|mixed
  20. */
  21. public function getUserAuthority()
  22. {
  23. if (!Auth::user())return new Collection();
  24. $key = "authorities:user_".Auth::id();
  25. $isAdmin = $this->checkAdminIdentity();
  26. $tag = $isAdmin ? "authorities:admin" : "authorities:user";
  27. if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin));
  28. return Cache::tags($tag)->get($key);
  29. }
  30. public function getUserAndroidAuthority()
  31. {
  32. if (!Auth::user())return new Collection();
  33. $key = "authorities:android:user_".Auth::id();
  34. $isAdmin = $this->checkAdminIdentity();
  35. $tag = $isAdmin ? "authorities:admin" : "authorities:user";
  36. if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin,true));
  37. return Cache::tags($tag)->get($key);
  38. }
  39. private function checkAdminIdentity():bool
  40. {
  41. if (!Auth::user())return false;
  42. return array_search(Auth::user()["name"],config("users.superAdmin"))!==false;
  43. }
  44. /**
  45. * 即时的权限数据
  46. *
  47. * @param bool $isAdmin
  48. * @param bool $isAndroid
  49. *
  50. * @return Collection
  51. */
  52. public function getUserAuthorityData(bool $isAdmin = null, bool $isAndroid = false):Collection
  53. {
  54. $query = Authority::query();
  55. if ($isAndroid)$query->whereNotNull("method");
  56. if ($isAdmin===null)$isAdmin = $this->checkAdminIdentity();
  57. if ($isAdmin) return $query->get();
  58. return $query->whereHas("roles",function (Builder $query){
  59. $query->whereHas("users",function (Builder $query){
  60. $query->where(DB::raw("users.id"),Auth::id());
  61. });
  62. })->get();
  63. }
  64. public function removeAdminAuth()
  65. {
  66. Cache::tags("authorities:admin")->flush();
  67. app("MenuService")->removeMenuMapping();
  68. }
  69. public function removeAllAuth()
  70. {
  71. Cache::tags("authorities:admin")->flush();
  72. Cache::tags("authorities:user")->flush();
  73. app("MenuService")->removeMenuMapping();
  74. }
  75. /**
  76. * 格式化为树状结构
  77. *
  78. * @param Collection $authorities
  79. * @return array
  80. */
  81. public function format(Collection $authorities):array
  82. {
  83. $authMap = [];
  84. foreach ($authorities as $authority){
  85. $item = $authority->toArray();
  86. $item["child"] = [];
  87. $authMap[$authority->id] = $item;
  88. }
  89. foreach ($authorities as $authority){
  90. if ($authority->parent_id){
  91. if (!isset($authMap[$authority->parent_id])){
  92. $authTem = $this->formatAuthority($authMap,$authMap[$authority->id]);
  93. if ($authTem)$authMap = $authTem;
  94. } else $authMap[$authority->parent_id]["child"][] = $authMap[$authority->id];
  95. unset($authMap[$authority->id]);
  96. }
  97. }
  98. return $authMap;
  99. }
  100. /**
  101. * 递归格式化权限组
  102. *
  103. * @param array $authMap
  104. * @param array $authorities
  105. *
  106. * @return array|bool
  107. */
  108. private function formatAuthority(array $authMap,array $authorities)
  109. {
  110. foreach ($authMap as $index=>$data){
  111. if ($data["id"]==$authorities["parent_id"]){
  112. $authMap[$index]["child"][] = $authorities;
  113. unset($authMap[$authorities["id"]]);
  114. return $authMap;
  115. }
  116. if ($data["child"]){
  117. $re = $this->formatAuthority($data["child"],$authorities);
  118. if ($re){
  119. $authMap[$index]["child"] = $re;
  120. return $authMap;
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. /**
  127. * 通过权限CODE获取用户
  128. */
  129. public function authorityGetUsers(string $aliasName)
  130. {
  131. return User::query()->whereHas("roles",function ($query)use($aliasName){
  132. $query->whereHas("authorities",function ($query)use($aliasName){
  133. $query->where("alias_name",$aliasName);
  134. });
  135. })->orWhereIn("name",config("users.superAdmin"))->get();
  136. }
  137. public function checkAllOwner($userId): bool
  138. {
  139. $authorityName = "货主-可见全部";
  140. if ($userId != Auth::id()){
  141. return Authority::query()->selectRaw("1")->whereHas("roles",function ($query)use($userId){
  142. $query->whereHas("users",function ($query)use($userId){
  143. $query->where("users.id",$userId);
  144. });
  145. })->where("alias_name",$authorityName)->first() != null;
  146. }
  147. return Gate::allows("货主-可见全部");
  148. }
  149. }