src/Contact/Security/ContactAccess/Voter.php line 26

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2022 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 2022 TECLA Consulting Group oü
  16.  */
  17. namespace App\Contact\Security\ContactAccess;
  18. use LogicException;
  19. use Symfony\Component\DependencyInjection\ServiceLocator;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authorization\Voter\Voter as BaseVoter;
  22. final class Voter extends BaseVoter
  23. {
  24.     private ServiceLocator $permissions;
  25.     public function __construct(ServiceLocator $permissions)
  26.     {
  27.         $this->permissions $permissions;
  28.     }
  29.     public function supportsAttribute(string $attribute): bool
  30.     {
  31.         return $this->permissions->has($attribute);
  32.     }
  33.     protected function supports(string $attribute$subject): bool
  34.     {
  35.         return $this->supportsAttribute($attribute);
  36.     }
  37.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  38.     {
  39.         /** @var PermissionInterface $permission */
  40.         $permission = ($this->permissions)($attribute);
  41.         if (null !== $permission) {
  42.             return $permission->isGranted($subject);
  43.         }
  44.         throw new LogicException('This code should not be reached!');
  45.     }
  46. }