src/Platform/SecurityBundle/Security/AliasVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace Platform\SecurityBundle\Security;
  3. use Platform\SecurityBundle\Service\Sentry;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  7. /**
  8.  * AliasVoter votes based on the permissions array extracted from the alias.
  9.  */
  10. class AliasVoter implements VoterInterface
  11. {
  12.     /**
  13.      * @var AuthorizationCheckerInterface
  14.      */
  15.     private AuthorizationCheckerInterface $authorizationChecker;
  16.     /**
  17.      * @var Sentry
  18.      */
  19.     private Sentry $sentry;
  20.     /**
  21.      * @param AuthorizationCheckerInterface $authorizationChecker
  22.      * @param Sentry $sentry
  23.      */
  24.     public function __construct(
  25.         AuthorizationCheckerInterface $authorizationChecker,
  26.         Sentry $sentry
  27.     ) {
  28.         $this->authorizationChecker $authorizationChecker;
  29.         $this->sentry $sentry;
  30.     }
  31.     /**
  32.      * {@inheritdoc}
  33.      */
  34.     public function vote(TokenInterface $token$subject, array $attributes): int
  35.     {
  36.         foreach ($attributes as $attribute) {
  37.             // skip non aliases
  38.             if ( ! $this->sentry->isAlias($attribute)) {
  39.                 continue;
  40.             }
  41.             foreach ($this->sentry->prepareAttributes([$attribute]) as $aliasAttribute) {
  42.                 // there should be no aliases at this stage, if there are, skip them
  43.                 if ($this->sentry->isAlias($aliasAttribute)) {
  44.                     continue;
  45.                 }
  46.                 if ($this->authorizationChecker->isGranted($aliasAttribute$subject)) {
  47.                     return VoterInterface::ACCESS_GRANTED;
  48.                 }
  49.             }
  50.         }
  51.         return VoterInterface::ACCESS_ABSTAIN;
  52.     }
  53. }