| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- namespace App\Services;
- use App\Authority;
- use App\User;
- use Illuminate\Database\Eloquent\Builder;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use App\Traits\ServiceAppAop;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Gate;
- class AuthorityService
- {
- use ServiceAppAop;
- protected $modelClass=Authority::class;
- /**
- * 携带缓存的权限数据
- *
- * @return array|Collection|mixed
- */
- public function getUserAuthority()
- {
- if (!Auth::user())return new Collection();
- $key = "authorities:user_".Auth::id();
- $isAdmin = $this->checkAdminIdentity();
- $tag = $isAdmin ? "authorities:admin" : "authorities:user";
- if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin));
- return Cache::tags($tag)->get($key);
- }
- public function getUserAndroidAuthority()
- {
- if (!Auth::user())return new Collection();
- $key = "authorities:android:user_".Auth::id();
- $isAdmin = $this->checkAdminIdentity();
- $tag = $isAdmin ? "authorities:admin" : "authorities:user";
- if (!Cache::tags($tag)->has($key))Cache::tags($tag)->forever($key,$this->getUserAuthorityData($isAdmin,true));
- return Cache::tags($tag)->get($key);
- }
- private function checkAdminIdentity():bool
- {
- if (!Auth::user())return false;
- return array_search(Auth::user()["name"],config("users.superAdmin"))!==false;
- }
- /**
- * 即时的权限数据
- *
- * @param bool $isAdmin
- * @param bool $isAndroid
- *
- * @return Collection
- */
- public function getUserAuthorityData(bool $isAdmin = null, bool $isAndroid = false):Collection
- {
- $query = Authority::query();
- if ($isAndroid)$query->whereNotNull("method");
- if ($isAdmin===null)$isAdmin = $this->checkAdminIdentity();
- if ($isAdmin) return $query->get();
- return $query->whereHas("roles",function (Builder $query){
- $query->whereHas("users",function (Builder $query){
- $query->where(DB::raw("users.id"),Auth::id());
- });
- })->get();
- }
- public function removeAdminAuth()
- {
- Cache::tags("authorities:admin")->flush();
- app("MenuService")->removeMenuMapping();
- }
- public function removeAllAuth()
- {
- Cache::tags("authorities:admin")->flush();
- Cache::tags("authorities:user")->flush();
- app("MenuService")->removeMenuMapping();
- }
- /**
- * 格式化为树状结构
- *
- * @param Collection $authorities
- * @return array
- */
- public function format(Collection $authorities):array
- {
- $authMap = [];
- foreach ($authorities as $authority){
- $item = $authority->toArray();
- $item["child"] = [];
- $authMap[$authority->id] = $item;
- }
- foreach ($authorities as $authority){
- if ($authority->parent_id){
- if (!isset($authMap[$authority->parent_id])){
- $authTem = $this->formatAuthority($authMap,$authMap[$authority->id]);
- if ($authTem)$authMap = $authTem;
- } else $authMap[$authority->parent_id]["child"][] = $authMap[$authority->id];
- unset($authMap[$authority->id]);
- }
- }
- return $authMap;
- }
- /**
- * 递归格式化权限组
- *
- * @param array $authMap
- * @param array $authorities
- *
- * @return array|bool
- */
- private function formatAuthority(array $authMap,array $authorities)
- {
- foreach ($authMap as $index=>$data){
- if ($data["id"]==$authorities["parent_id"]){
- $authMap[$index]["child"][] = $authorities;
- unset($authMap[$authorities["id"]]);
- return $authMap;
- }
- if ($data["child"]){
- $re = $this->formatAuthority($data["child"],$authorities);
- if ($re){
- $authMap[$index]["child"] = $re;
- return $authMap;
- }
- }
- }
- return false;
- }
- /**
- * 通过权限CODE获取用户
- */
- public function authorityGetUsers(string $aliasName)
- {
- return User::query()->whereHas("roles",function ($query)use($aliasName){
- $query->whereHas("authorities",function ($query)use($aliasName){
- $query->where("alias_name",$aliasName);
- });
- })->orWhereIn("name",config("users.superAdmin"))->get();
- }
- public function checkAllOwner($userId): bool
- {
- $authorityName = "货主-可见全部";
- if ($userId != Auth::id()){
- return Authority::query()->selectRaw("1")->whereHas("roles",function ($query)use($userId){
- $query->whereHas("users",function ($query)use($userId){
- $query->where("users.id",$userId);
- });
- })->where("alias_name",$authorityName)->first() != null;
- }
- return Gate::allows("货主-可见全部");
- }
- }
|