src/Security/Voter/BackendUserGroupVoter.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 BackendUserGroupVoter extends Voter
  11. {
  12.     const BACKEND_GROUP_VIEW 'BACKEND_GROUP_VIEW';
  13.     const BACKEND_GROUP_SHOW 'BACKEND_GROUP_SHOW';
  14.     const BACKEND_GROUP_CREATE 'BACKEND_GROUP_CREATE';
  15.     const BACKEND_GROUP_EDIT 'BACKEND_GROUP_EDIT';
  16.     const BACKEND_GROUP_DELETE 'BACKEND_GROUP_DELETE';
  17.     const BACKEND_GROUP_CLONE 'BACKEND_GROUP_CLONE';
  18.     /**
  19.      * @var Security
  20.      */
  21.     private $security;
  22.     /**
  23.      * @var Commons
  24.      */
  25.     private $commons;
  26.     /**
  27.      * @var array
  28.      */
  29.     private $permissions;
  30.     public function __construct(Security $securityCommons $commons)
  31.     {
  32.         $this->security $security;
  33.         $this->commons $commons;
  34.         $this->permissions = [];
  35.     }
  36.     /**
  37.      * @param string $attribute
  38.      * @param mixed $subject
  39.      * @return bool
  40.      */
  41.     protected function supports($attribute$subject): bool
  42.     {
  43.         if (!in_array($attribute, [
  44.             self::BACKEND_GROUP_VIEWself::BACKEND_GROUP_SHOWself::BACKEND_GROUP_EDITself::BACKEND_GROUP_CREATEself::BACKEND_GROUP_DELETEself::BACKEND_GROUP_CLONE
  45.         ])) {
  46.             return false;
  47.         }
  48.         return true;
  49.     }
  50.     /**
  51.      * @param string $attribute
  52.      * @param mixed $subject
  53.      * @param TokenInterface $token
  54.      * @return bool
  55.      */
  56.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  57.     {
  58.         /** @var User $user */
  59.         $user $token->getUser();
  60.         // if the user is anonymous, do not grant access
  61.         if (!$user instanceof UserInterface) {
  62.             return false;
  63.         }
  64.         $this->permissions $this->commons->getBackendPermissions($user);
  65.         if ($this->hasSuperUser() === true) {
  66.             return true;
  67.         }
  68.         switch ($attribute) {
  69.             case self::BACKEND_GROUP_VIEW:
  70.                 return $this->canView();
  71.             case self::BACKEND_GROUP_SHOW:
  72.                 return $this->canShow();
  73.             case self::BACKEND_GROUP_CREATE:
  74.                 return $this->canCreate();
  75.             case self::BACKEND_GROUP_EDIT:
  76.                 return $this->canEdit();
  77.             case self::BACKEND_GROUP_DELETE:
  78.                 return $this->canDelete();
  79.             case self::BACKEND_GROUP_CLONE:
  80.                 return $this->canClone();
  81.         }
  82.         throw new LogicException('Invalid attribute: ' $attribute);
  83.     }
  84.     /**
  85.      * Return True if have Super View Permission else return false
  86.      * @return bool
  87.      */
  88.     private function hasSuperUser(): bool
  89.     {
  90.         if (array_key_exists('superUser'$this->permissions)) {
  91.             if (in_array('Yes'$this->permissions['superUser'])) {
  92.                 return true;
  93.             }
  94.         }
  95.         return false;
  96.     }
  97.     /**
  98.      * Return True if have View Permission else return false
  99.      * @return bool
  100.      */
  101.     private function canView(): bool
  102.     {
  103.         if (array_key_exists('backendGroup'$this->permissions)) {
  104.             if (in_array('View'$this->permissions['backendGroup'])) {
  105.                 return true;
  106.             }
  107.         }
  108.         return false;
  109.     }
  110.     /**
  111.      * Return True if have Show Permission else return false
  112.      * @return bool
  113.      */
  114.     private function canShow(): bool
  115.     {
  116.         if (array_key_exists('backendGroup'$this->permissions)) {
  117.             if (in_array('Show'$this->permissions['backendGroup'])) {
  118.                 return true;
  119.             }
  120.         }
  121.         return false;
  122.     }
  123.     /**
  124.      * Return True if have Edit Permission else return false
  125.      * @return bool
  126.      */
  127.     private function canEdit(): bool
  128.     {
  129.         if (array_key_exists('backendGroup'$this->permissions)) {
  130.             if (in_array('Edit'$this->permissions['backendGroup'])) {
  131.                 return true;
  132.             }
  133.         }
  134.         return false;
  135.     }
  136.     /**
  137.      * Return True if have Create Permission else return false
  138.      * @return bool
  139.      */
  140.     private function canCreate(): bool
  141.     {
  142.         if (array_key_exists('backendGroup'$this->permissions)) {
  143.             if (in_array('Create'$this->permissions['backendGroup'])) {
  144.                 return true;
  145.             }
  146.         }
  147.         return false;
  148.     }
  149.     /**
  150.      * Return True if have Delete Permission else return false
  151.      * @return bool
  152.      */
  153.     private function canDelete(): bool
  154.     {
  155.         if (array_key_exists('backendGroup'$this->permissions)) {
  156.             if (in_array('Delete'$this->permissions['backendGroup'])) {
  157.                 return true;
  158.             }
  159.         }
  160.         return false;
  161.     }
  162.     /**
  163.      * Return True if have Clone Permission else return false
  164.      * @return bool
  165.      */
  166.     private function canClone(): bool
  167.     {
  168.         if (array_key_exists('backendGroup'$this->permissions)) {
  169.             if (in_array('Clone'$this->permissions['backendGroup'])) {
  170.                 return true;
  171.             }
  172.         }
  173.         return false;
  174.     }
  175. }