AuthorityService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Services;
  3. use App\Authority;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Database\Eloquent\Collection;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Cache;
  8. use App\Traits\ServiceAppAop;
  9. use Illuminate\Support\Facades\DB;
  10. class AuthorityService
  11. {
  12. use ServiceAppAop;
  13. protected $modelClass=Authority::class;
  14. /**
  15. * 携带缓存的权限数据
  16. *
  17. * @return array|Collection|mixed
  18. */
  19. public function getUserAuthority()
  20. {
  21. if (!Auth::user())return new Collection();
  22. $key = "authorities:user_".Auth::id();
  23. $isAdmin = $this->checkAdminIdentity();
  24. $tag = $isAdmin ? "authorities:admin" : "authorities:user";
  25. if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin));
  26. return Cache::tags($tag)->get($key);
  27. }
  28. private function checkAdminIdentity():bool
  29. {
  30. if (!Auth::user())return false;
  31. return array_search(Auth::user()["name"],config("users.superAdmin"))!==false;
  32. }
  33. /**
  34. * 即时的权限数据
  35. *
  36. * @param bool $isAdmin
  37. *
  38. * @return Collection
  39. */
  40. public function getUserAuthorityData(bool $isAdmin = null):Collection
  41. {
  42. if ($isAdmin===null)$isAdmin = $this->checkAdminIdentity();
  43. if ($isAdmin) return Authority::query()->get();
  44. return Authority::query()->whereHas("roles",function (Builder $query){
  45. $query->whereHas("users",function (Builder $query){
  46. $query->where(DB::raw("users.id"),Auth::id());
  47. });
  48. })->get();
  49. }
  50. public function removeAdminAuth()
  51. {
  52. Cache::tags("authorities:admin")->flush();
  53. app("MenuService")->removeMenuMapping();
  54. }
  55. public function removeAllAuth()
  56. {
  57. Cache::tags("authorities:admin")->flush();
  58. Cache::tags("authorities:user")->flush();
  59. app("MenuService")->removeMenuMapping();
  60. }
  61. /**
  62. * 格式化为树状结构
  63. *
  64. * @param Collection $authorities
  65. * @return array
  66. */
  67. public function format(Collection $authorities):array
  68. {
  69. $authMap = [];
  70. foreach ($authorities as $authority){
  71. $item = $authority->toArray();
  72. $item["child"] = [];
  73. $authMap[$authority->id] = $item;
  74. }
  75. foreach ($authorities as $authority){
  76. if ($authority->parent_id){
  77. if (!isset($authMap[$authority->parent_id])){
  78. $authTem = $this->formatAuthority($authMap,$authMap[$authority->id]);
  79. if ($authTem)$authMap = $authTem;
  80. } else $authMap[$authority->parent_id]["child"][] = $authMap[$authority->id];
  81. unset($authMap[$authority->id]);
  82. }
  83. }
  84. return $authMap;
  85. }
  86. /**
  87. * 递归格式化权限组
  88. *
  89. * @param array $authMap
  90. * @param array $authorities
  91. *
  92. * @return array|bool
  93. */
  94. private function formatAuthority(array $authMap,array $authorities)
  95. {
  96. foreach ($authMap as $index=>$data){
  97. if ($data["id"]==$authorities["parent_id"]){
  98. $authMap[$index]["child"][] = $authorities;
  99. unset($authMap[$authorities["id"]]);
  100. return $authMap;
  101. }
  102. if ($data["child"]){
  103. $re = $this->formatAuthority($data["child"],$authorities);
  104. if ($re){
  105. $authMap[$index]["child"] = $re;
  106. return $authMap;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. }