src/Security/Voter/UserVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use App\Repository\UserRepository;
  5. use App\Utils\Commons;
  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 UserVoter extends Voter
  11. {
  12.     const USER_VIEW 'USER_VIEW';
  13.     const USER_CREATE 'USER_CREATE';
  14.     const USER_SHOW 'USER_SHOW';
  15.     const USER_EDIT 'USER_EDIT';
  16.     const USER_DELETE 'USER_DELETE';
  17.     const USER_MULTI_DELETE 'USER_MULTI_DELETE';
  18.     const USER_CLONE 'USER_CLONE';
  19.     /**
  20.      * @var Security
  21.      */
  22.     private $security;
  23.     /**
  24.      * @var Commons
  25.      */
  26.     private $commons;
  27.     /**
  28.      * @var array
  29.      */
  30.     private $permissions;
  31.     /**
  32.      * @var UserRepository
  33.      */
  34.     private $userRepository;
  35.     /**
  36.      * DeviceGroupVoter constructor.
  37.      * @param Security $security
  38.      * @param Commons $commons
  39.      * @param UserRepository $userRepository
  40.      */
  41.     public function __construct(Security $securityCommons $commonsUserRepository $userRepository)
  42.     {
  43.         $this->security $security;
  44.         $this->commons $commons;
  45.         $this->userRepository $userRepository;
  46.         $this->permissions = [];
  47.     }
  48.     /**
  49.      * @param string $attribute
  50.      * @param mixed $subject
  51.      * @return bool
  52.      */
  53.     protected function supports($attribute$subject): bool
  54.     {
  55.         if (!in_array($attribute,
  56.             [self::USER_VIEWself::USER_SHOWself::USER_EDITself::USER_CREATEself::USER_DELETEself::USER_CLONEself::USER_MULTI_DELETE]
  57.         )) {
  58.             return false;
  59.         }
  60.         return true;
  61.     }
  62.     /**
  63.      * @param string $attribute
  64.      * @param mixed $subject
  65.      * @param TokenInterface $token
  66.      * @return bool
  67.      */
  68.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  69.     {
  70.         /** @var User $user */
  71.         $user $token->getUser();
  72.         // if the user is anonymous, do not grant access
  73.         if (!$user instanceof UserInterface) {
  74.             return false;
  75.         }
  76.         $this->permissions $this->commons->getUserPermissions($user);
  77.         switch ($attribute) {
  78.             case self::USER_VIEW:
  79.                 return $this->canView();
  80.             case self::USER_SHOW:
  81.                 return ($this->canShow() && $this->isOwner($user$subject));
  82.             case self::USER_CREATE:
  83.                 return $this->canCreate();
  84.             case self::USER_EDIT:
  85.                 return ($this->canEdit() && $this->isOwner($user$subject));
  86.             case self::USER_DELETE:
  87.                 return ($this->canDelete() && $this->isOwner($user$subject));
  88.             case self::USER_MULTI_DELETE:
  89.                 return $this->canMultiDelete();
  90.             case self::USER_CLONE:
  91.                 return ($this->canClone() && $this->isOwner($user$subject));
  92.         }
  93.         return false;
  94.     }
  95.     /**
  96.      * @param User|null $user
  97.      * @param $subject
  98.      * @return bool
  99.      */
  100.     private function isOwner(User $user$subject): bool
  101.     {
  102.         if (!is_object($subject)) {
  103.             $subject $this->userRepository->findOneBy(["id" => intval($subject)]);
  104.         }
  105.         if (!$subject instanceof User) {
  106.             return false;
  107.         }
  108.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  109.     }
  110.     /**
  111.      * Return True if have View Permission else return false
  112.      * @return bool
  113.      */
  114.     private function canView(): bool
  115.     {
  116.         if ($this->security->isGranted('ROLE_ADMIN')) {
  117.             return true;
  118.         }
  119.         if (array_key_exists('users'$this->permissions)) {
  120.             if (in_array('View'$this->permissions['users'])) {
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     /**
  127.      * Return True if have View Permission else return false
  128.      * @return bool
  129.      */
  130.     private function canShow(): bool
  131.     {
  132.         if ($this->security->isGranted('ROLE_ADMIN')) {
  133.             return true;
  134.         }
  135.         if (array_key_exists('users'$this->permissions)) {
  136.             if (in_array('Show'$this->permissions['users'])) {
  137.                 return true;
  138.             }
  139.         }
  140.         return false;
  141.     }
  142.     /**
  143.      * Return True if have Edit Permission else return false
  144.      * @return bool
  145.      */
  146.     private function canEdit(): bool
  147.     {
  148.         if ($this->security->isGranted('ROLE_ADMIN')) {
  149.             return true;
  150.         }
  151.         if (array_key_exists('users'$this->permissions)) {
  152.             if (in_array('Edit'$this->permissions['users'])) {
  153.                 return true;
  154.             }
  155.         }
  156.         return false;
  157.     }
  158.     /**
  159.      * Return True if have Create Permission else return false
  160.      * @return bool
  161.      */
  162.     private function canCreate(): bool
  163.     {
  164.         if ($this->security->isGranted('ROLE_ADMIN')) {
  165.             return true;
  166.         }
  167.         if (array_key_exists('users'$this->permissions)) {
  168.             if (in_array('Create'$this->permissions['users'])) {
  169.                 return true;
  170.             }
  171.         }
  172.         return false;
  173.     }
  174.     /**
  175.      * Return True if have Delete Permission else return false
  176.      * @return bool
  177.      */
  178.     private function canDelete(): bool
  179.     {
  180.         if ($this->security->isGranted('ROLE_ADMIN')) {
  181.             return true;
  182.         }
  183.         if (array_key_exists('users'$this->permissions)) {
  184.             if (in_array('Delete'$this->permissions['users'])) {
  185.                 return true;
  186.             }
  187.         }
  188.         return false;
  189.     }
  190.     /**
  191.      * Return True if have Delete Permission else return false
  192.      * @return bool
  193.      */
  194.     private function canMultiDelete(): bool
  195.     {
  196.         if ($this->security->isGranted('ROLE_ADMIN')) {
  197.             return true;
  198.         }
  199.         if (array_key_exists('users'$this->permissions)) {
  200.             if (in_array('Delete'$this->permissions['users'])) {
  201.                 return true;
  202.             }
  203.         }
  204.         return false;
  205.     }
  206.     /**
  207.      * Return True if have Clone Permission else return false
  208.      * @return bool
  209.      */
  210.     private function canClone(): bool
  211.     {
  212.         if ($this->security->isGranted('ROLE_ADMIN')) {
  213.             return true;
  214.         }
  215.         if (array_key_exists('users'$this->permissions)) {
  216.             if (in_array('Clone'$this->permissions['users'])) {
  217.                 return true;
  218.             }
  219.         }
  220.         return false;
  221.     }
  222. }