src/Contact/Security/Contact/Voter.php line 29

  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\Contact\Security\Contact;
  18. use App\Contact\Model\ContactInterface;
  19. use App\Core\Security\ContactAwareSubjectInterface;
  20. use InvalidArgumentException;
  21. use LogicException;
  22. use Symfony\Component\DependencyInjection\ServiceLocator;
  23. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  24. use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
  25. final class Voter extends BaseVoter
  26. {
  27.     private ServiceLocator $permissions;
  28.     public function __construct(ServiceLocator $permissions)
  29.     {
  30.         $this->permissions $permissions;
  31.     }
  32.     protected function supports(string $attribute$subject): bool
  33.     {
  34.         return
  35.             $this->permissions->has($attribute) &&
  36.             true === $subject instanceof ContactAwareSubjectInterface &&
  37.             (null === $subject->getSubject() || (true === $subject->getSubject() instanceof ContactInterface))
  38.         ;
  39.     }
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  41.     {
  42.         if (false === $subject instanceof ContactAwareSubjectInterface) {
  43.             throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given'ContactAwareSubjectInterface::class, get_debug_type($subject)));
  44.         }
  45.         $contact $subject->getContact();
  46.         $subjectContact $subject->getSubject();
  47.         if (null !== $subjectContact && false === $subjectContact instanceof ContactInterface) {
  48.             throw new InvalidArgumentException(sprintf('Expected subject of type "%s", "%s" given'ContactInterface::class, get_debug_type($subjectContact)));
  49.         }
  50.         /** @var PermissionInterface $permission */
  51.         $permission = ($this->permissions)($attribute);
  52.         if (null !== $permission) {
  53.             return $permission->isGranted($contact$subjectContact);
  54.         }
  55.         throw new LogicException('This code should not be reached!');
  56.     }
  57. }