src/Security/Voter/Customer/DownloadVoter.php line 12

  1. <?php
  2. namespace App\Security\Voter\Customer;
  3. use App\Entity\Customer\File;
  4. use App\Entity\Admin\CustomerUser;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. use Symfony\Bundle\SecurityBundle\Security;
  9. class DownloadVoter extends Voter
  10. {
  11.     final public const VIEW   'DOWNLOAD_ENTITY_VIEW';
  12.     
  13.     public function __construct(
  14.             private readonly Security $security)
  15.     {
  16.     }
  17.     
  18.     protected function supports(string $attributemixed $subject): bool
  19.     {
  20.         if(!in_array($attribute, [self::VIEW])){
  21.             return false;
  22.         }
  23.         return $subject instanceof File;
  24.     }
  25.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  26.     {
  27.         $user $token->getUser();
  28.         // if the user is anonymous, do not grant access
  29.         if (!$user instanceof UserInterface) {
  30.             return false;
  31.         }
  32.         
  33.         if (!$this->security->isGranted('ROLE_STAFF')){
  34.             return false;
  35.         }
  36.         
  37.         return match($attribute) {
  38.             self::VIEW => $this->canView($subject$user),
  39.             default => throw new \LogicException('This code should not be reached!')
  40.         };
  41.     }
  42.     
  43.     private function canView(File $entityCustomerUser $user): bool
  44.     {
  45.         if ($entity->getRights() == $user->getStaff() or $entity->getType() == 1) {
  46.             return true;
  47.         }
  48.         return false;
  49.     }
  50. }