<?php 
 
namespace App\Security\Voter; 
 
use App\Entity\User; 
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 BackendCustomerUserVoter extends Voter 
{ 
 
    const BACKEND_CUSTOMER_USERS_SHOW = 'BACKEND_CUSTOMER_USERS_SHOW'; 
    const BACKEND_CUSTOMER_USERS_EDIT = 'BACKEND_CUSTOMER_USERS_EDIT'; 
    const BACKEND_CUSTOMER_USERS_CLONE = 'BACKEND_CUSTOMER_USERS_CLONE'; 
    const BACKEND_CUSTOMER_USERS_DELETE = 'BACKEND_CUSTOMER_USERS_DELETE'; 
    const BACKEND_CUSTOMER_USERS_CHANGE = 'BACKEND_CUSTOMER_USERS_CHANGE'; 
 
    /** 
     * @var Security 
     */ 
    private $security; 
    /** 
     * @var Commons 
     */ 
    private $commons; 
    /** 
     * @var array 
     */ 
    private $permissions; 
 
    /** 
     * SupportVoter constructor. 
     * @param Security $security 
     * @param Commons $commons 
     */ 
    public function __construct(Security $security, Commons $commons) 
    { 
        $this->security = $security; 
        $this->commons = $commons; 
        $this->permissions = []; 
    } 
 
    /** 
     * @param string $attribute 
     * @param mixed $subject 
     * @return bool 
     */ 
    protected function supports($attribute, $subject): bool 
    { 
        if (!in_array($attribute, 
            [ 
                self::BACKEND_CUSTOMER_USERS_SHOW, self::BACKEND_CUSTOMER_USERS_EDIT, self::BACKEND_CUSTOMER_USERS_CHANGE, 
                self::BACKEND_CUSTOMER_USERS_CLONE, self::BACKEND_CUSTOMER_USERS_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->getBackendPermissions($user); 
 
        if ($this->hasSuperUser() === true) { 
            return true; 
        } 
 
        switch ($attribute) { 
            case self::BACKEND_CUSTOMER_USERS_SHOW: 
                return $this->canShow(); 
            case self::BACKEND_CUSTOMER_USERS_EDIT: 
                return $this->canEdit(); 
            case self::BACKEND_CUSTOMER_USERS_CLONE: 
                return $this->canClone(); 
            case self::BACKEND_CUSTOMER_USERS_DELETE: 
                return $this->canDelete(); 
            case self::BACKEND_CUSTOMER_USERS_CHANGE: 
                return $this->canChange(); 
 
        } 
 
        throw new LogicException('Invalid attribute: ' . $attribute); 
    } 
 
    /** 
     * Return True if have Super View Permission else return false 
     * @return bool 
     */ 
    private function hasSuperUser(): bool 
    { 
        if (array_key_exists('superUser', $this->permissions)) { 
            if (in_array('Yes', $this->permissions['superUser'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
 
 
    /** 
     * Return True if have Show Permission else return false 
     * @return bool 
     */ 
    private function canShow(): bool 
    { 
        if (array_key_exists('customerUsers', $this->permissions)) { 
            if (in_array('Show', $this->permissions['customerUsers'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Edit Permission else return false 
     * @return bool 
     */ 
    private function canEdit(): bool 
    { 
        if (array_key_exists('customerUsers', $this->permissions)) { 
            if (in_array('Edit', $this->permissions['customerUsers'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Change Permission else return false 
     * @return bool 
     */ 
    private function canChange(): bool 
    { 
        if (array_key_exists('customerUsers', $this->permissions)) { 
            if (in_array('Change', $this->permissions['customerUsers'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
    /** 
     * Return True if have Delete Permission else return false 
     * @return bool 
     */ 
    private function canDelete(): bool 
    { 
        if (array_key_exists('customerUsers', $this->permissions)) { 
            if (in_array('Delete', $this->permissions['customerUsers'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
   /** 
     * Return True if have Clone Permission else return false 
     * @return bool 
     */ 
    private function canClone(): bool 
    { 
        if (array_key_exists('customerUsers', $this->permissions)) { 
            if (in_array('Clone', $this->permissions['customerUsers'])) { 
                return true; 
            } 
        } 
        return false; 
    } 
 
 
}