src/Security/Voter/BackendCustomerUserVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Utils\Commons;
  5. use LogicException;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. use Symfony\Component\Security\Core\User\UserInterface;
  10. class BackendCustomerUserVoter extends Voter
  11. {
  12.     const BACKEND_CUSTOMER_USERS_SHOW 'BACKEND_CUSTOMER_USERS_SHOW';
  13.     const BACKEND_CUSTOMER_USERS_EDIT 'BACKEND_CUSTOMER_USERS_EDIT';
  14.     const BACKEND_CUSTOMER_USERS_CLONE 'BACKEND_CUSTOMER_USERS_CLONE';
  15.     const BACKEND_CUSTOMER_USERS_DELETE 'BACKEND_CUSTOMER_USERS_DELETE';
  16.     const BACKEND_CUSTOMER_USERS_CHANGE 'BACKEND_CUSTOMER_USERS_CHANGE';
  17.     /**
  18.      * @var Security
  19.      */
  20.     private $security;
  21.     /**
  22.      * @var Commons
  23.      */
  24.     private $commons;
  25.     /**
  26.      * @var array
  27.      */
  28.     private $permissions;
  29.     /**
  30.      * SupportVoter constructor.
  31.      * @param Security $security
  32.      * @param Commons $commons
  33.      */
  34.     public function __construct(Security $securityCommons $commons)
  35.     {
  36.         $this->security $security;
  37.         $this->commons $commons;
  38.         $this->permissions = [];
  39.     }
  40.     /**
  41.      * @param string $attribute
  42.      * @param mixed $subject
  43.      * @return bool
  44.      */
  45.     protected function supports($attribute$subject): bool
  46.     {
  47.         if (!in_array($attribute,
  48.             [
  49.                 self::BACKEND_CUSTOMER_USERS_SHOWself::BACKEND_CUSTOMER_USERS_EDITself::BACKEND_CUSTOMER_USERS_CHANGE,
  50.                 self::BACKEND_CUSTOMER_USERS_CLONEself::BACKEND_CUSTOMER_USERS_DELETE
  51.             ]
  52.         )) {
  53.             return false;
  54.         }
  55.         return true;
  56.     }
  57.     /**
  58.      * @param string $attribute
  59.      * @param mixed $subject
  60.      * @param TokenInterface $token
  61.      * @return bool
  62.      */
  63.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  64.     {
  65.         /** @var User $user */
  66.         $user $token->getUser();
  67.         // if the user is anonymous, do not grant access
  68.         if (!$user instanceof UserInterface) {
  69.             return false;
  70.         }
  71.         $this->permissions $this->commons->getBackendPermissions($user);
  72.         if ($this->hasSuperUser() === true) {
  73.             return true;
  74.         }
  75.         switch ($attribute) {
  76.             case self::BACKEND_CUSTOMER_USERS_SHOW:
  77.                 return $this->canShow();
  78.             case self::BACKEND_CUSTOMER_USERS_EDIT:
  79.                 return $this->canEdit();
  80.             case self::BACKEND_CUSTOMER_USERS_CLONE:
  81.                 return $this->canClone();
  82.             case self::BACKEND_CUSTOMER_USERS_DELETE:
  83.                 return $this->canDelete();
  84.             case self::BACKEND_CUSTOMER_USERS_CHANGE:
  85.                 return $this->canChange();
  86.         }
  87.         throw new LogicException('Invalid attribute: ' $attribute);
  88.     }
  89.     /**
  90.      * Return True if have Super View Permission else return false
  91.      * @return bool
  92.      */
  93.     private function hasSuperUser(): bool
  94.     {
  95.         if (array_key_exists('superUser'$this->permissions)) {
  96.             if (in_array('Yes'$this->permissions['superUser'])) {
  97.                 return true;
  98.             }
  99.         }
  100.         return false;
  101.     }
  102.     /**
  103.      * Return True if have Show Permission else return false
  104.      * @return bool
  105.      */
  106.     private function canShow(): bool
  107.     {
  108.         if (array_key_exists('customerUsers'$this->permissions)) {
  109.             if (in_array('Show'$this->permissions['customerUsers'])) {
  110.                 return true;
  111.             }
  112.         }
  113.         return false;
  114.     }
  115.     /**
  116.      * Return True if have Edit Permission else return false
  117.      * @return bool
  118.      */
  119.     private function canEdit(): bool
  120.     {
  121.         if (array_key_exists('customerUsers'$this->permissions)) {
  122.             if (in_array('Edit'$this->permissions['customerUsers'])) {
  123.                 return true;
  124.             }
  125.         }
  126.         return false;
  127.     }
  128.     /**
  129.      * Return True if have Change Permission else return false
  130.      * @return bool
  131.      */
  132.     private function canChange(): bool
  133.     {
  134.         if (array_key_exists('customerUsers'$this->permissions)) {
  135.             if (in_array('Change'$this->permissions['customerUsers'])) {
  136.                 return true;
  137.             }
  138.         }
  139.         return false;
  140.     }
  141.     /**
  142.      * Return True if have Delete Permission else return false
  143.      * @return bool
  144.      */
  145.     private function canDelete(): bool
  146.     {
  147.         if (array_key_exists('customerUsers'$this->permissions)) {
  148.             if (in_array('Delete'$this->permissions['customerUsers'])) {
  149.                 return true;
  150.             }
  151.         }
  152.         return false;
  153.     }
  154.    /**
  155.      * Return True if have Clone Permission else return false
  156.      * @return bool
  157.      */
  158.     private function canClone(): bool
  159.     {
  160.         if (array_key_exists('customerUsers'$this->permissions)) {
  161.             if (in_array('Clone'$this->permissions['customerUsers'])) {
  162.                 return true;
  163.             }
  164.         }
  165.         return false;
  166.     }
  167. }