src/Security/Voter/BackendNewRegistrationVoter.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 BackendNewRegistrationVoter extends Voter
  11. {
  12.     const NEW_REGISTRATION_VIEW 'NEW_REGISTRATION_VIEW';
  13.     const NEW_REGISTRATION_EDIT 'NEW_REGISTRATION_EDIT';
  14.     const NEW_REGISTRATION_ASSIGN 'NEW_REGISTRATION_ASSIGN';
  15.     const NEW_REGISTRATION_MESSAGE 'NEW_REGISTRATION_MESSAGE';
  16.     const NEW_REGISTRATION_DELETE 'NEW_REGISTRATION_DELETE';
  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.             [self::NEW_REGISTRATION_VIEWself::NEW_REGISTRATION_VIEW,  self::NEW_REGISTRATION_ASSIGNself::NEW_REGISTRATION_MESSAGEself::NEW_REGISTRATION_DELETE]
  49.         )) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54.     /**
  55.      * @param string $attribute
  56.      * @param mixed $subject
  57.      * @param TokenInterface $token
  58.      * @return bool
  59.      */
  60.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  61.     {
  62.         /** @var User $user */
  63.         $user $token->getUser();
  64.         // if the user is anonymous, do not grant access
  65.         if (!$user instanceof UserInterface) {
  66.             return false;
  67.         }
  68.         $this->permissions $this->commons->getBackendPermissions($user);
  69.         if ($this->hasSuperUser() === true) {
  70.             return true;
  71.         }
  72.         switch ($attribute) {
  73.             case self::NEW_REGISTRATION_VIEW:
  74.                 return $this->canView();
  75.             case self::NEW_REGISTRATION_EDIT:
  76.                 return $this->canEdit();
  77.             case self::NEW_REGISTRATION_ASSIGN:
  78.                 return $this->canAssign();
  79.             case self::NEW_REGISTRATION_MESSAGE:
  80.                 return $this->canMessage();
  81.             case self::NEW_REGISTRATION_DELETE:
  82.                 return $this->canDelete();
  83.         }
  84.         throw new LogicException('Invalid attribute: ' $attribute);
  85.     }
  86.     /**
  87.      * Return True if have Super View Permission else return false
  88.      * @return bool
  89.      */
  90.     private function hasSuperUser(): bool
  91.     {
  92.         if (array_key_exists('superUser'$this->permissions)) {
  93.             if (in_array('Yes'$this->permissions['superUser'])) {
  94.                 return true;
  95.             }
  96.         }
  97.         return false;
  98.     }
  99.     /**
  100.      * Return True if have View Permission else return false
  101.      * @return bool
  102.      */
  103.     private function canView(): bool
  104.     {
  105.         if (array_key_exists('newRegistrations'$this->permissions)) {
  106.             if (in_array('View'$this->permissions['newRegistrations'])) {
  107.                 return true;
  108.             }
  109.         }
  110.         return false;
  111.     }
  112.     /**
  113.      * Return True if have Edit Permission else return false
  114.      * @return bool
  115.      */
  116.     private function canEdit(): bool
  117.     {
  118.         if (array_key_exists('newRegistrations'$this->permissions)) {
  119.             if (in_array('Edit'$this->permissions['newRegistrations'])) {
  120.                 return true;
  121.             }
  122.         }
  123.         return false;
  124.     }
  125.     /**
  126.      * Return True if have Assign Team Permission else return false
  127.      * @return bool
  128.      */
  129.     private function canAssign(): bool
  130.     {
  131.         if (array_key_exists('newRegistrations'$this->permissions)) {
  132.             if (in_array('Assign'$this->permissions['newRegistrations'])) {
  133.                 return true;
  134.             }
  135.         }
  136.         return false;
  137.     }
  138.     /**
  139.      * Return True if have Message Permission else return false
  140.      * @return bool
  141.      */
  142.     private function canMessage(): bool
  143.     {
  144.         if (array_key_exists('newRegistrations'$this->permissions)) {
  145.             if (in_array('Message'$this->permissions['newRegistrations'])) {
  146.                 return true;
  147.             }
  148.         }
  149.         return false;
  150.     }
  151.     /**
  152.      * Return True if have Delete Permission else return false
  153.      * @return bool
  154.      */
  155.     private function canDelete(): bool
  156.     {
  157.         if (array_key_exists('newRegistrations'$this->permissions)) {
  158.             if (in_array('Delete'$this->permissions['newRegistrations'])) {
  159.                 return true;
  160.             }
  161.         }
  162.         return false;
  163.     }
  164. }