src/Security/Voter/FileGroupVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\FileGroup;
  4. use App\Entity\User;
  5. use App\Repository\FileGroupRepository;
  6. use App\Utils\Commons;
  7. use LogicException;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class FileGroupVoter extends Voter
  13. {
  14.     const FILE_GROUP_VIEW 'FILE_GROUP_VIEW';
  15.     const FILE_GROUP_SHOW 'FILE_GROUP_SHOW';
  16.     const FILE_GROUP_CREATE 'FILE_GROUP_CREATE';
  17.     const FILE_GROUP_EDIT 'FILE_GROUP_EDIT';
  18.     const FILE_GROUP_DELETE 'FILE_GROUP_DELETE';
  19.     const FILE_GROUP_MULTI_DELETE 'FILE_GROUP_MULTI_DELETE';
  20.     /**
  21.      * @var Security
  22.      */
  23.     private $security;
  24.     /**
  25.      * @var Commons
  26.      */
  27.     private $commons;
  28.     /**
  29.      * @var array
  30.      */
  31.     private $permissions;
  32.     /**
  33.      * @var FileGroupRepository
  34.      */
  35.     private $fileGroupRepository;
  36.     /**
  37.      * FileGroupVoter constructor.
  38.      * @param Security $security
  39.      * @param Commons $commons
  40.      * @param FileGroupRepository $fileGroupRepository
  41.      */
  42.     public function __construct(Security $securityCommons $commonsFileGroupRepository $fileGroupRepository)
  43.     {
  44.         $this->security $security;
  45.         $this->commons $commons;
  46.         $this->fileGroupRepository $fileGroupRepository;
  47.         $this->permissions = [];
  48.     }
  49.     /**
  50.      * @param string $attribute
  51.      * @param mixed $subject
  52.      * @return bool
  53.      */
  54.     protected function supports($attribute$subject): bool
  55.     {
  56.         // if the attribute isn't one we support, return false
  57.         if (!in_array($attribute,
  58.             [self::FILE_GROUP_VIEWself::FILE_GROUP_EDITself::FILE_GROUP_CREATEself::FILE_GROUP_DELETEself::FILE_GROUP_MULTI_DELETEself::FILE_GROUP_SHOW]
  59.         )) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64.     /**
  65.      * @param string $attribute
  66.      * @param mixed $subject
  67.      * @param TokenInterface $token
  68.      * @return bool
  69.      */
  70.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  71.     {
  72.         /** @var User $user */
  73.         $user $token->getUser();
  74.         // if the user is anonymous, do not grant access
  75.         if (!$user instanceof UserInterface) {
  76.             return false;
  77.         }
  78.         $this->permissions $this->commons->getUserPermissions($user);
  79.         // ... (check conditions and return true to grant permission) ...
  80.         switch ($attribute) {
  81.             case self::FILE_GROUP_VIEW:
  82.                 return $this->canView();
  83.             case self::FILE_GROUP_CREATE:
  84.                 return $this->canCreate();
  85.             case self::FILE_GROUP_EDIT:
  86.                 return ($this->canEdit() && $this->isOwner($user$subject));
  87.             case self::FILE_GROUP_SHOW:
  88.                 return ($this->canShow() && $this->isOwner($user$subject));
  89.             case self::FILE_GROUP_DELETE:
  90.                 return ($this->canDelete() && $this->isOwner($user$subject));
  91.             case self::FILE_GROUP_MULTI_DELETE:
  92.                 return $this->canMultiDelete();
  93.         }
  94.         throw new LogicException('Invalid attribute: ' $attribute);
  95.     }
  96.     /**
  97.      * @param User|null $user
  98.      * @param $subject
  99.      * @return bool
  100.      */
  101.     private function isOwner(User $user$subject): bool
  102.     {
  103.         if (!is_object($subject)) {
  104.             $subject $this->fileGroupRepository->findOneBy(["id" => intval($subject)]);
  105.         }
  106.         if (!$subject instanceof FileGroup) {
  107.             return false;
  108.         }
  109.         return $user->getWorkspace()->getId() === $subject->getWorkspace()->getId();
  110.     }
  111.     /**
  112.      * Return True if have View Permission else return false
  113.      */
  114.     private function canView(): bool
  115.     {
  116.         if ($this->security->isGranted('ROLE_ADMIN')) {
  117.             return true;
  118.         }
  119.         if (array_key_exists('fileGroup'$this->permissions)) {
  120.             if (in_array('View'$this->permissions['fileGroup'])) {
  121.                 return true;
  122.             }
  123.         }
  124.         return false;
  125.     }
  126.     /**
  127.      * Return True if have Show 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('fileGroup'$this->permissions)) {
  136.             if (in_array('Show'$this->permissions['fileGroup'])) {
  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('fileGroup'$this->permissions)) {
  152.             if (in_array('Edit'$this->permissions['fileGroup'])) {
  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('fileGroup'$this->permissions)) {
  168.             if (in_array('Create'$this->permissions['fileGroup'])) {
  169.                 return true;
  170.             }
  171.         }
  172.         return false;
  173.     }
  174.     /** Return True if have Delete Permission else return false
  175.      * @return bool
  176.      */
  177.     private function canDelete(): bool
  178.     {
  179.         if ($this->security->isGranted('ROLE_ADMIN')) {
  180.             return true;
  181.         }
  182.         if (array_key_exists('fileGroup'$this->permissions)) {
  183.             if (in_array('Delete'$this->permissions['fileGroup'])) {
  184.                 return true;
  185.             }
  186.         }
  187.         return false;
  188.     }
  189.     /**
  190.      * @return bool
  191.      */
  192.     private function canMultiDelete(): bool
  193.     {
  194.         if ($this->security->isGranted('ROLE_ADMIN')) {
  195.             return true;
  196.         }
  197.         if (array_key_exists('fileGroup'$this->permissions)) {
  198.             if (in_array('Delete'$this->permissions['fileGroup'])) {
  199.                 return true;
  200.             }
  201.         }
  202.         return false;
  203.     }
  204. }