src/Security/Api/Authorization/Voter/ActiveModuleVoter.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Security\Api\Authorization\Voter;
  3. use App\Entity\Api\CoreModule\User;
  4. use App\Helper\Api\Constants\Entity\Properties\Entity as PropertyNamesEntity;
  5. use App\Helper\Api\Entity\RequestDataContainer;
  6. use App\Security\Api\Authorization\VoterAttribute\AbstractVoterAttribute;
  7. use App\Validator\Api\Data\CoreModule\ModuleDataValidator;
  8. use LogicException;
  9. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Validator\Exception\ValidationFailedException;
  12. /**
  13.  * API active module voter.
  14.  *
  15.  * @package API
  16.  * @internal
  17.  */
  18. class ActiveModuleVoter extends AbstractApiBaseVoter
  19. {
  20.     /**
  21.      * Validator interface.
  22.      *
  23.      * @var ModuleDataValidator
  24.      */
  25.     protected ModuleDataValidator $validator;
  26.     /**
  27.      * Constructor.
  28.      *
  29.      * @param ModuleDataValidator $validator
  30.      */
  31.     public function __construct(ModuleDataValidator $validator)
  32.     {
  33.         $this->validator $validator;
  34.     }
  35.     /**
  36.      * Determines if the attribute and subject are supported by this voter.
  37.      *
  38.      * @param string $attribute An attribute.
  39.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type.
  40.      *
  41.      * @return bool
  42.      */
  43.     protected function supports(string $attribute$subject): bool
  44.     {
  45.         // requires data container
  46.         if (!$subject instanceof RequestDataContainer) {
  47.             return false;
  48.         }
  49.         // votes then on every API call when attribute format is valid
  50.         return AbstractVoterAttribute::isValidAttribute($attribute);
  51.     }
  52.     /**
  53.      * Perform a single access check operation on a given attribute, subject and token.
  54.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  55.      *
  56.      * @param string $attribute An attribute.
  57.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type.
  58.      * @param TokenInterface $token The token interface.
  59.      * @return bool
  60.      */
  61.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  62.     {
  63.         $user $token->getUser();
  64.         if (!$user instanceof User) {
  65.             // the user must be logged in, otherwise deny access
  66.             return false;
  67.         }
  68.         if (!$this->supports($attribute$subject)) {
  69.             throw new LogicException('This code should not be reached!');
  70.         }
  71.         if (!$subject instanceof RequestDataContainer) {
  72.             throw new LogicException('This voter requires a RequestDataContainer object passed as subject!');
  73.         }
  74.         try {
  75.             $this->validator->validateIsModuleActive([
  76.                 PropertyNamesEntity::ID => AbstractVoterAttribute::getModuleIdFromAttribute($attribute),
  77.             ]);
  78.         } catch (ValidationFailedException $exception) {
  79.             // get message of first violation and issue an access violation when module is inactive
  80.             $violations $exception->getViolations();
  81.             $subject->setException(new AccessDeniedHttpException($violations->get(0)->getMessage(), $exception));
  82.             return false;
  83.         }
  84.         return true;
  85.     }
  86. }