cacheService = app('CacheService'); } function hasRoles(User $user, $roles){ $thisRoles=$this->cacheService->getOrExecute("user{$user['id']}->roles",function()use($user){ return $user->roles; }); return !!$roles->intersect($thisRoles)->count(); } function getPermittingOwnerIds($user=null){ if(!$user)return []; return $this->cacheService->getOrExecute("user{$user['id']}->getPermittingOwnerIds",function()use($user){ return $user->getPermittingOwnerIdsAttribute() ?? []; })??[]; } /** * 检查用户的管理员身份 * * @param integer $userId * * @return bool */ private function checkAdminIdentity($userId):bool { if ($userId == Auth::id())return array_search(Auth::user()["name"],config("users.superAdmin"))!==false; /** @var User|\stdClass $user */ $user = User::query()->select("name")->find($userId); if (!$user)return false; return array_search($user->name,config("users.superAdmin"))!==false; } /** * @param integer|null $userId * * @return array */ public function getUserHasOwners($userId = null) { if (!$userId)$userId = Auth::id(); $key = "owners:user_".$userId; if (!Cache::has($key)){ if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(Owner::query()->select("id")->whereNull("deleted_at")->get()->toArray(),"id")); else{ $owners = []; /** @var User|\stdClass $user */ $user = new User(); $user->id = $userId; $user->load("roles.owners"); $user->roles->each(function ($role)use (&$owners){ $owners = array_merge($owners,array_column($role->owners->toArray(),"id")); }); Cache::forever($key,$owners); } } return Cache::get($key); } /** * @param integer|null $userId * * @return array */ public function getUserHasUserWorkGroups($userId = null) { if (!$userId)$userId = Auth::id(); $key = "userWorkGroups:user_".$userId; if (!Cache::has($key)){ if ($this->checkAdminIdentity($userId))Cache::forever($key,array_column(UserWorkgroup::query()->select("id")->get()->toArray(),"id")); else{ $userWorkGroups = []; /** @var User|\stdClass $user */ $user = new User(); $user->id = $userId; $user->load("roles.userWorkGroups"); $user->roles->each(function ($role)use (&$userWorkGroups){ $userWorkGroups = array_merge($userWorkGroups,array_column($role->userWorkGroups->toArray(),"id")); }); Cache::forever($key,$userWorkGroups); } } return Cache::get($key); } /** * 清除用户缓存 * * @param User $user */ public function clearUserCache(User $user) { Cache::tags("authorities:user")->forget("authorities:user_".$user->id); Cache::forget("owners:user_".$user->id); Cache::forget("userWorkGroups:user_".$user->id); Cache::tags("AUTHORITY_MENU_MAPPING")->forget("am_mapping_".$user->id); } /** * 获取JWT token * * @param User|\stdClass $user * @param mixed $key * @return string */ public function getJWTToken($user,$key):string { $time = time(); $payload = [ 'iss' => $_SERVER["HTTP_HOST"], //签发者 'iat' => $time, 'nbf' => $time, 'exp' => $time+7200, 'data' => [ 'id' => $user->id, 'username' => $user->name ] ]; $alg = 'RS256'; return JWT::encode($payload, $key, $alg); } /** * 获取JWT token * * @param User|\stdClass $user * @param mixed $key * @return string */ public function checkJWTToken($user,$key):string { $time = time(); $payload = [ 'iss' => $_SERVER["HTTP_HOST"], //签发者 'iat' => $time, 'nbf' => $time, 'exp' => $time+7200, 'data' => [ 'id' => $user->id, 'username' => $user->name ] ]; $alg = 'RS256'; return JWT::encode($payload, $key, $alg); } }