| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Services;
- use App\Authority;
- use App\Owner;
- use App\User;
- use App\UserWorkgroup;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- use App\Traits\ServiceAppAop;
- class UserService
- {
- use ServiceAppAop;
- protected $modelClass=User::class;
- /** @var CacheService $cacheService */
- private $cacheService;
- function __construct(){
- $this->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);
- }
- }
|