src/Security/Voter/BackendDeletedCustomerVoter.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 BackendDeletedCustomerVoter extends Voter
  11. {
  12.     const BACKEND_DELETED_CUSTOMER_VIEW 'BACKEND_DELETED_CUSTOMER_VIEW';
  13.     const BACKEND_UNCONFIRMED_CUSTOMER_VIEW 'BACKEND_UNCONFIRMED_CUSTOMER_VIEW';
  14.     const BACKEND_DATA_EXPORT_VIEW 'BACKEND_DATA_EXPORT_VIEW';
  15.     /**
  16.      * @var Security
  17.      */
  18.     private $security;
  19.     /**
  20.      * @var Commons
  21.      */
  22.     private $commons;
  23.     /**
  24.      * @var array
  25.      */
  26.     private $permissions;
  27.     /**
  28.      * SupportVoter constructor.
  29.      * @param Security $security
  30.      * @param Commons $commons
  31.      */
  32.     public function __construct(Security $securityCommons $commons)
  33.     {
  34.         $this->security $security;
  35.         $this->commons $commons;
  36.         $this->permissions = [];
  37.     }
  38.     /**
  39.      * @param string $attribute
  40.      * @param mixed $subject
  41.      * @return bool
  42.      */
  43.     protected function supports($attribute$subject): bool
  44.     {
  45.         if (!in_array($attribute,
  46.             [self::BACKEND_DELETED_CUSTOMER_VIEWself::BACKEND_UNCONFIRMED_CUSTOMER_VIEWself::BACKEND_DATA_EXPORT_VIEW]
  47.         )) {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.     /**
  53.      * @param string $attribute
  54.      * @param mixed $subject
  55.      * @param TokenInterface $token
  56.      * @return bool
  57.      */
  58.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  59.     {
  60.         /** @var User $user */
  61.         $user $token->getUser();
  62.         // if the user is anonymous, do not grant access
  63.         if (!$user instanceof UserInterface) {
  64.             return false;
  65.         }
  66.         $this->permissions $this->commons->getBackendPermissions($user);
  67.         if ($this->hasSuperUser() === true) {
  68.             return true;
  69.         }
  70.         switch ($attribute) {
  71.             case self::BACKEND_DELETED_CUSTOMER_VIEW:
  72.                 return $this->canViewDeleted();
  73.             case self::BACKEND_UNCONFIRMED_CUSTOMER_VIEW:
  74.                 return $this->canViewUnconfirmed();
  75.             case self::BACKEND_DATA_EXPORT_VIEW:
  76.                 return $this->canDataExportView();
  77.         }
  78.         throw new LogicException('Invalid attribute: ' $attribute);
  79.     }
  80.     /**
  81.      * Return True if have Super View Permission else return false
  82.      * @return bool
  83.      */
  84.     private function hasSuperUser(): bool
  85.     {
  86.         if (array_key_exists('superUser'$this->permissions)) {
  87.             if (in_array('Yes'$this->permissions['superUser'])) {
  88.                 return true;
  89.             }
  90.         }
  91.         return false;
  92.     }
  93.     /**
  94.      * Return True if have View Permission else return false
  95.      * @return bool
  96.      */
  97.     private function canViewDeleted(): bool
  98.     {
  99.         if (array_key_exists('deletedCustomers'$this->permissions)) {
  100.             if (in_array('View'$this->permissions['deletedCustomers'])) {
  101.                 return true;
  102.             }
  103.         }
  104.         return false;
  105.     }
  106.     /**
  107.      * Return True if have View Permission else return false
  108.      * @return bool
  109.      */
  110.     private function canViewUnconfirmed(): bool
  111.     {
  112.         if (array_key_exists('unconfirmedCustomers'$this->permissions)) {
  113.             if (in_array('View'$this->permissions['unconfirmedCustomers'])) {
  114.                 return true;
  115.             }
  116.         }
  117.         return false;
  118.     }
  119.     /**
  120.      * Return True if have dataExport View Permission else return false
  121.      * @return bool
  122.      */
  123.     private function canDataExportView(): bool
  124.     {
  125.         if (array_key_exists('dataExport'$this->permissions)) {
  126.             if (in_array('View'$this->permissions['dataExport'])) {
  127.                 return true;
  128.             }
  129.         }
  130.         return false;
  131.     }
  132. }