src/Security/Voter/BackendPopUpMessageVoter.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 BackendPopUpMessageVoter extends Voter
  11. {
  12.     const BACKEND_POP_UP_VIEW 'BACKEND_POP_UP_VIEW';
  13.     const BACKEND_POP_UP_CREATE 'BACKEND_POP_UP_CREATE';
  14.     const BACKEND_POP_UP_EDIT 'BACKEND_POP_UP_EDIT';
  15.     const BACKEND_POP_UP_DELETE 'BACKEND_POP_UP_DELETE';
  16.     const BACKEND_POP_UP_STATUS_CHANGE 'BACKEND_POP_UP_STATUS_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.      * BackendPlanVoter 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 mixed $attribute
  42.      * @param mixed $subject
  43.      * @return bool
  44.      */
  45.     protected function supports($attribute$subject): bool
  46.     {
  47.         if (!in_array($attribute, [self::BACKEND_POP_UP_VIEWself::BACKEND_POP_UP_STATUS_CHANGEself::BACKEND_POP_UP_EDITself::BACKEND_POP_UP_CREATEself::BACKEND_POP_UP_DELETE])) {
  48.             return false;
  49.         }
  50.         return true;
  51.     }
  52.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  53.     {
  54.         /** @var User $user */
  55.         $user $token->getUser();
  56.         // if the user is anonymous, do not grant access
  57.         if (!$user instanceof UserInterface) {
  58.             return false;
  59.         }
  60.         $this->permissions $this->commons->getBackendPermissions($user);
  61.         if ($this->hasSuperUser() === true) {
  62.             return true;
  63.         }
  64.         switch ($attribute) {
  65.             case self::BACKEND_POP_UP_VIEW:
  66.                 return $this->canView();
  67.             case self::BACKEND_POP_UP_CREATE:
  68.                 return $this->canCreate();
  69.             case self::BACKEND_POP_UP_EDIT:
  70.                 return $this->canEdit();
  71.             case self::BACKEND_POP_UP_DELETE:
  72.                 return $this->canDelete();
  73.             case self::BACKEND_POP_UP_STATUS_CHANGE:
  74.                 return $this->canChangeStatus();
  75.         }
  76.         throw new LogicException('Invalid attribute: ' $attribute);
  77.     }
  78.     /**
  79.      * Return True if have Super View Permission else return false
  80.      * @return bool
  81.      */
  82.     private function hasSuperUser(): bool
  83.     {
  84.         if (array_key_exists('superUser'$this->permissions)) {
  85.             if (in_array('Yes'$this->permissions['superUser'])) {
  86.                 return true;
  87.             }
  88.         }
  89.         return false;
  90.     }
  91.     /**
  92.      * Return True if have View Permission else return false
  93.      * @return bool
  94.      */
  95.     private function canView(): bool
  96.     {
  97.         if (array_key_exists('backendPopUp'$this->permissions)) {
  98.             if (in_array('View'$this->permissions['backendPopUp'])) {
  99.                 return true;
  100.             }
  101.         }
  102.         return false;
  103.     }
  104.     /**
  105.      * Return True if have Edit Permission else return false
  106.      * @return bool
  107.      */
  108.     private function canEdit(): bool
  109.     {
  110.         if (array_key_exists('backendPopUp'$this->permissions)) {
  111.             if (in_array('Edit'$this->permissions['backendPopUp'])) {
  112.                 return true;
  113.             }
  114.         }
  115.         return false;
  116.     }
  117.     /**
  118.      * Return True if have Create Permission else return false
  119.      * @return bool
  120.      */
  121.     private function canCreate(): bool
  122.     {
  123.         if (array_key_exists('backendPopUp'$this->permissions)) {
  124.             if (in_array('Create'$this->permissions['backendPopUp'])) {
  125.                 return true;
  126.             }
  127.         }
  128.         return false;
  129.     }
  130.     /**
  131.      * Return True if have Delete Permission else return false
  132.      * @return bool
  133.      */
  134.     private function canDelete(): bool
  135.     {
  136.         if (array_key_exists('backendPopUp'$this->permissions)) {
  137.             if (in_array('Delete'$this->permissions['backendPopUp'])) {
  138.                 return true;
  139.             }
  140.         }
  141.         return false;
  142.     }
  143.     /**
  144.      * Return True if have Change Status Permission else return false
  145.      * @return bool
  146.      */
  147.     private function canChangeStatus(): bool
  148.     {
  149.         if (array_key_exists('backendPopUp'$this->permissions)) {
  150.             if (in_array('Change'$this->permissions['backendPopUp'])) {
  151.                 return true;
  152.             }
  153.         }
  154.         return false;
  155.     }
  156. }