| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- namespace App\Services;
- use App\Authority;
- use App\Role;
- use Exception;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- class AuthorityService
- {
- use ServiceAppAop;
- protected $modelClass=Authority::class;
- // /**
- // * @return Authority[]
- // */
- // function getAll(){
- // $authorities = Cache::get('authorities');
- // if(!$authorities){
- // $authorities = Authority::with('roles')->get();
- // Cache::put('authorities', $authorities, config('cache.expirations.authorities'));
- // }
- // return $authorities;
- // }
- //
- // /**
- // * @param Authority $authority
- // * @return Role[]
- // * @throws Exception
- // */
- // function getRoles(Authority $authority){
- // if(!$authority['id']??'') throw new Exception('User对象或id不能为空');
- // $roles = Cache::get('authorityGetRoles'.$authority['id']);
- // if(!$roles){
- // $roles = Authority::with('roles')->get();
- // Cache::put('authorities', $roles, config('cache.expirations.authorities'));
- // }
- // return $roles;
- // }
- public function getUserAuthority()
- {
- $key = "authorities:user_".Auth::id();
- if (!Cache::has($key)){
- if (array_search(Auth::user()["name"],config("users.superAdmin"))===false){
- Cache::tags("authorities:user")->forever($key,Authority::query()->whereHas("roles",function (Builder $query){
- $query->whereHas("users",function (Builder $query){
- $query->where("id",Auth::id());
- });
- })->get());
- }else Cache::tags("authorities:admin")->forever($key,$authorities = Authority::query()->get());
- }
- return Cache::get($key);
- }
- public function removeAdminAuth()
- {
- Cache::tags("authorities:admin")->flush();
- }
- public function removeAllAuth()
- {
- Cache::tags("authorities:admin")->flush();
- Cache::tags("authorities:user")->flush();
- }
- }
|