<?php 
 
namespace App\Security\Voter; 
 
use App\Entity\User; 
use App\Repository\UserRepository; 
use App\Utils\Commons; 
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; 
use Symfony\Component\Security\Core\Authorization\Voter\Voter; 
use Symfony\Component\Security\Core\Security; 
use Symfony\Component\Security\Core\User\UserInterface; 
 
class UserVoter extends Voter 
{ 
    const USER_VIEW = 'USER_VIEW'; 
    const USER_CREATE = 'USER_CREATE'; 
    const USER_SHOW = 'USER_SHOW'; 
    const USER_EDIT = 'USER_EDIT'; 
    const USER_DELETE = 'USER_DELETE'; 
    const USER_MULTI_DELETE = 'USER_MULTI_DELETE'; 
    const USER_CLONE = 'USER_CLONE'; 
 
    /** 
     * @var Security 
     */ 
    private $security; 
    /** 
     * @var Commons 
     */ 
    private $commons; 
    /** 
     * @var array 
     */ 
    private $permissions; 
 
    /** 
     * @var UserRepository 
     */ 
    private $userRepository; 
 
    /** 
     * DeviceGroupVoter constructor. 
     * @param Security $security 
     * @param Commons $commons 
     * @param UserRepository $userRepository 
     */ 
    public function __construct(Security $security, Commons $commons, UserRepository $userRepository) 
    { 
        $this->security = $security; 
        $this->commons = $commons; 
        $this->userRepository = $userRepository; 
        $this->permissions = []; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @return bool 
     */ 
    protected function supports($attribute, $subject): bool 
    { 
 
        if (!in_array($attribute, 
            [self::USER_VIEW, self::USER_SHOW, self::USER_EDIT, self::USER_CREATE, self::USER_DELETE, self::USER_CLONE, self::USER_MULTI_DELETE] 
        )) { 
            return false; 
        } 
        return true; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @param TokenInterface $token 
     * @return bool 
     */ 
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool 
    { 
        /** @var User $user */ 
        $user = $token->getUser(); 
        // if the user is anonymous, do not grant access 
        if (!$user instanceof UserInterface) { 
            return false; 
        } 
        $this->permissions = $this->commons->getUserPermissions($user); 
 
        switch ($attribute) { 
            case self::USER_VIEW: 
                return $this->canView(); 
            case self::USER_SHOW: 
                return ($this->canShow() && $this->isOwner($user, $subject)); 
            case self::USER_CREATE: 
                return $this->canCreate(); 
            case self::USER_EDIT: 
                return ($this->canEdit() && $this->isOwner($user, $subject)); 
            case self::USER_DELETE: 
                return ($this->canDelete() && $this->isOwner($user, $subject)); 
            case self::USER_MULTI_DELETE: 
                return $this->canMultiDelete(); 
            case self::USER_CLONE: 
                return ($this->canClone() && $this->isOwner($user, $subject)); 
        } 
 
        return false; 
    } 
 
    /** 
     * @param User|null $user 
     * @param $subject 
     * @return bool 
     */ 
    private function isOwner(User $user, $subject): bool 
    { 
 
        if (!is_object($subject)) { 
            $subject = $this->userRepository->findOneBy(["id" => intval($subject)]); 
        } 
 
        if (!$subject instanceof User) { 
            return false; 
        } 
 
        return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId(); 
    } 
 
    /** 
     * Return True if have View Permission else return false 
     * @return bool 
     */ 
    private function canView(): bool 
    { 
 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('View', $this->permissions['users'])) { 
                return true; 
            } 
 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have View Permission else return false 
     * @return bool 
     */ 
    private function canShow(): bool 
    { 
 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Show', $this->permissions['users'])) { 
                return true; 
            } 
 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Edit Permission else return false 
     * @return bool 
     */ 
    private function canEdit(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['users'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Create Permission else return false 
     * @return bool 
     */ 
    private function canCreate(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Create', $this->permissions['users'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Delete Permission else return false 
     * @return bool 
     */ 
    private function canDelete(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Delete', $this->permissions['users'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Delete Permission else return false 
     * @return bool 
     */ 
    private function canMultiDelete(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Delete', $this->permissions['users'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Clone Permission else return false 
     * @return bool 
     */ 
    private function canClone(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('users', $this->permissions)) { 
            if (in_array('Clone', $this->permissions['users'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
}