src/Offer/Security/OfferDraftEmailDraftAttachment/Voter.php line 27

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Offer\Security\OfferDraftEmailDraftAttachment;
  18. use App\Offer\Model\OfferDraftEmailDraftAttachmentInterface;
  19. use LogicException;
  20. use Symfony\Component\DependencyInjection\ServiceLocator;
  21. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  22. use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
  23. final class Voter extends BaseVoter
  24. {
  25.     private ServiceLocator $permissions;
  26.     public function __construct(ServiceLocator $permissions)
  27.     {
  28.         $this->permissions $permissions;
  29.     }
  30.     protected function supports(string $attribute$subject): bool
  31.     {
  32.         return $this->permissions->has($attribute) && (null === $subject || (true === $subject instanceof OfferDraftEmailDraftAttachmentInterface));
  33.     }
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         /** @var ?OfferDraftEmailDraftAttachmentInterface $attachment */
  37.         $attachment $subject;
  38.         /** @var PermissionInterface $permission */
  39.         $permission = ($this->permissions)($attribute);
  40.         if (null !== $permission) {
  41.             return $permission->isGranted($attachment);
  42.         }
  43.         throw new LogicException('This code should not be reached!');
  44.     }
  45. }