src/Security/RevueManagement/NumeroEditVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Security\RevueManagement;
  3. use App\Entity\RevueManagement\Numero;
  4. use Symfony\Contracts\Translation\TranslatorInterface;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  8. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  9. use Symfony\Component\HttpKernel\KernelEvents;
  10. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. class NumeroEditVoter extends Voter
  16. {
  17.     private Security $security;
  18.     private SessionInterface $session;
  19.     private TranslatorInterface $translator;
  20.     private EventDispatcherInterface $dispatcher;
  21.     private $url null;
  22.     private UrlGeneratorInterface $router;
  23.     public function __construct(Security $securitySessionInterface $sessionTranslatorInterface $translatorUrlGeneratorInterface $routerEventDispatcherInterface $dispatcher)
  24.     {
  25.         $this->security $security;
  26.         $this->session $session;
  27.         $this->translator $translator;
  28.         $this->dispatcher $dispatcher;
  29.         $this->router $router;
  30.     }
  31.     protected function supports($attribute$subject)
  32.     {
  33.         return \in_array($attribute, ['NUMERO_EDIT'], true)
  34.             && $subject instanceof Numero;
  35.     }
  36.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  37.     {
  38.         switch ($attribute) {
  39.             case 'NUMERO_EDIT':
  40.                 return $this->canEdit($subject$token);
  41.         }
  42.         throw new \LogicException('This code should not be reached!');
  43.     }
  44.     private function canEdit(Numero $numeroTokenInterface $token): bool
  45.     {
  46.         $user $token->getUser();
  47.         // if the user is anonymous, do not grant access
  48.         if (!$user instanceof UserInterface) {
  49.             return false;
  50.         } elseif ($this->security->isGranted('ROLE_GESTION_NUMEROS')) {
  51.             if ($numero->getState() == Numero::PUBLISHED || $numero->getState() == Numero::CONTROLLED ) {
  52.                 $this->session->getFlashBag()->add('error'$this->translator->trans('numero.have_published'));
  53.                 $this->url $this->router->generate('admin_numero_show', ['id'=> $numero->getId()]);                
  54.                 $this->dispatcher->addListener(KernelEvents::RESPONSE, [$this'onKernelResponse']);                 
  55.                 return false;
  56.             }            
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61.    
  62.     public function onKernelResponse(ResponseEvent $event)
  63.     {
  64.         $response = new RedirectResponse($this->url);
  65.         $event->setResponse($response);
  66.     }     
  67. }