AuthorityService.php 3.9 KB

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