src/Security/Voter/BackendUserVoter.php line 12

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