<?php


namespace App\Services;


use App\Authority;
use App\Owner;
use App\User;
use Illuminate\Support\Facades\Cache;
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() ?? [];
        })??[];
    }
}
