AuthorityService.php 4.5 KB

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