<?php 
 
namespace App\Security\Voter; 
 
use App\Entity\Device; 
use App\Entity\User; 
use App\Repository\DeviceRepository; 
use App\Utils\Commons; 
use LogicException; 
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 DeviceVoter extends Voter 
{ 
    const DEVICE_VIEW = 'DEVICE_VIEW'; 
    const DEVICE_SHOW = 'DEVICE_SHOW'; 
    const DEVICE_CREATE = 'DEVICE_CREATE'; 
    const DEVICE_EDIT = 'DEVICE_EDIT'; 
    const DEVICE_GROUPING = 'DEVICE_GROUPING'; 
    const DEVICE_ADD_TO_BLACKLIST = 'DEVICE_ADD_TO_BLACKLIST'; 
    const DEVICE_UNGROUP = 'DEVICE_UNGROUP'; 
    const DEVICE_MULTI_GROUP = 'DEVICE_MULTI_GROUP'; 
    const DEVICE_MULTI_BLACKLIST = 'DEVICE_MULTI_BLACKLIST'; 
    const DEVICE_MULTI_UN_BLACKLIST = 'DEVICE_MULTI_UN_BLACKLIST'; 
    const DEVICE_MULTI_UNGROUP = 'DEVICE_MULTI_UNGROUP'; 
    /** 
     * @var Security 
     */ 
    private $security; 
 
    /** 
     * @var Commons 
     */ 
    private $commons; 
 
    /** 
     * @var array 
     */ 
    private $permissions; 
 
    /** 
     * @var DeviceRepository 
     */ 
    private $deviceRepository; 
 
 
    /** 
     * DeviceVoter constructor. 
     * @param Security $security 
     * @param Commons $commons 
     * @param DeviceRepository $deviceRepository 
     */ 
    public function __construct(Security $security, Commons $commons, DeviceRepository $deviceRepository) 
    { 
        $this->security = $security; 
        $this->commons = $commons; 
        $this->permissions = []; 
        $this->deviceRepository = $deviceRepository; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @return bool 
     */ 
    protected function supports($attribute, $subject): bool 
    { 
        // if the attribute isn't one we support, return false 
        if (!in_array($attribute, [self::DEVICE_VIEW, self::DEVICE_UNGROUP, self::DEVICE_SHOW, self::DEVICE_EDIT, 
            self::DEVICE_CREATE, self::DEVICE_ADD_TO_BLACKLIST, self::DEVICE_GROUPING, 
            self::DEVICE_MULTI_GROUP, self::DEVICE_MULTI_BLACKLIST, self::DEVICE_MULTI_UN_BLACKLIST, self::DEVICE_MULTI_UNGROUP])) { 
            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::DEVICE_VIEW: 
                return $this->canView(); 
            case self::DEVICE_CREATE: 
                return $this->canCreate(); 
            case self::DEVICE_EDIT: 
                return ($this->canEdit() && $this->isOwner($user, $subject)); 
            case self::DEVICE_SHOW: 
                return ($this->canShow() && $this->isOwner($user, $subject)); 
            case self::DEVICE_GROUPING: 
                return ($this->canGroup() && $this->isOwner($user, $subject)); 
            case self::DEVICE_ADD_TO_BLACKLIST: 
                return ($this->canAddToBlacklist() && $this->isOwner($user, $subject)); 
            case self::DEVICE_UNGROUP: 
                return ($this->canUngroup() && $this->isOwner($user, $subject)); 
            case self::DEVICE_MULTI_GROUP: 
                return $this->canMultiGroup(); 
            case self::DEVICE_MULTI_UNGROUP: 
                return $this->canMultiUnGroup(); 
            case self::DEVICE_MULTI_BLACKLIST: 
                return $this->canMultiBlacklist(); 
            case self::DEVICE_MULTI_UN_BLACKLIST: 
                return $this->canMultiUnBlacklist(); 
        } 
 
        throw new LogicException('Invalid attribute: ' . $attribute); 
    } 
 
    /** 
     * @param User|null $user 
     * @param $subject 
     * @return bool 
     */ 
    private function isOwner(User $user, $subject): bool 
    { 
 
        if (!is_object($subject)) { 
            $subject = $this->deviceRepository->findOneBy(["id" => intval($subject)]); 
        } 
 
        if (!$subject instanceof Device) { 
            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('device', $this->permissions)) { 
            if (in_array('View', $this->permissions['device'])) { 
                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('device', $this->permissions)) { 
            if (in_array('Show', $this->permissions['device'])) { 
                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('device', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** Return True if have Create Permission else return false */ 
    private function canCreate(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('Create', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
 
    /** 
     * Return True if have Grouping Permission else return false 
     * @return bool 
     */ 
    private function canGroup(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('GROUPING', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * Return True if have UnGrouping Permission else return false 
     * @return bool 
     */ 
    private function canUngroup(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('UNGROUP', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** Return True if have Add to Blacklist Permission else return false */ 
    private function canAddToBlacklist(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('ADD_TO_BLACKLIST', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * @return bool 
     */ 
    private function canMultiGroup(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('GROUPING', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * @return bool 
     */ 
    private function canMultiUnBlacklist(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('ADD_TO_BLACKLIST', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * @return bool 
     */ 
    private function canMultiBlacklist(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('ADD_TO_BLACKLIST', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
 
    /** 
     * @return bool 
     */ 
    private function canMultiUnGroup(): bool 
    { 
        if ($this->security->isGranted('ROLE_ADMIN')) { 
            return true; 
        } 
        if (array_key_exists('device', $this->permissions)) { 
            if (in_array('UNGROUP', $this->permissions['device'])) { 
                return true; 
            } 
        } 
 
        return false; 
    } 
}