src/Security/UrlVoters.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Service\Access;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Routing\RouterInterface;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  9. class UrlVoters extends Voter implements VoterInterface
  10. {
  11.     private RouterInterface $routerService;
  12.     private ?array $route null;
  13.     private ?string $url null;
  14.     private RequestStack $requestStack;
  15.     private Access $access;
  16.     public function __construct(RouterInterface $routerAccess $accessRequestStack $requestStack)
  17.     {
  18.         $this->access $access;
  19.         $this->routerService $router;
  20.         $this->requestStack $requestStack;
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         $urlArray explode('?', (string) $this->requestStack->getMainRequest()->server->get('REQUEST_URI'));
  25.         $anonListUrls $this->access->getAnonListUrls();
  26.         $this->url $urlArray[0];
  27.         if (preg_match('![\W]-!'preg_replace('!/!'''$this->url))) {
  28.             return true;
  29.         }
  30.         $this->route $this->routerService->match($this->url);
  31.         foreach ($anonListUrls as $url) {
  32.             if (isset($this->route['_route'])) {
  33.                 if (preg_match('!' $url '!', (string) $this->route['_route'])) {
  34.                     return false;
  35.                 }
  36.             }
  37.         }
  38.         return true;
  39.     }
  40.     /**
  41.      * @param string             $attribute
  42.      * @param object|string|null $subject
  43.      */
  44.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool|string
  45.     {
  46.         if (preg_match('![\W]-!'preg_replace('!/!'''$this->url))) {
  47.             return false;
  48.         }
  49.         return $this->access->checkUrl($this->url);
  50.     }
  51. }