src/EventListener/ResponseListener.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: ricar
  5.  * Date: 19/11/2018
  6.  * Time: 11:48
  7.  */
  8. namespace App\EventListener;
  9. use App\Repository\MatriculaRepository;
  10. use App\Repository\UsuarioRepository;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  15. use Symfony\Component\Routing\RouterInterface;
  16. use Symfony\Component\Security\Core\Security;
  17. class ResponseListener
  18. {
  19.     private Security $security;
  20.     private MatriculaRepository $matriculaRepository;
  21.     private UsuarioRepository $usuarioRepository;
  22.     private Session $session;
  23.     private RouterInterface $router;
  24.     public function __construct(
  25.         Security            $security,
  26.         MatriculaRepository $matriculaRepository,
  27.         UsuarioRepository   $usuarioRepository,
  28.         RequestStack        $requestStack,
  29.         RouterInterface     $router
  30.     )
  31.     {
  32.         $this->security $security;
  33.         $this->matriculaRepository $matriculaRepository;
  34.         $this->usuarioRepository $usuarioRepository;
  35.         $this->session $requestStack->getSession();
  36.         $this->router $router;
  37.     }
  38.     public function onKernelResponse(ResponseEvent $event): void
  39.     {
  40.         if ($this->security->getUser()) {
  41.             $usuario $this->usuarioRepository->findOneBy(['email' => $this->security->getUser()->getUserIdentifier()]);
  42.             if ($usuario && $usuario->isAluno() && !$usuario->isProfessor() && !$usuario->isFuncionario()) {
  43.                 if (is_null($this->matriculaRepository->getMatriculaCorrente($usuario->getId()))) {
  44.                     $this->session->getFlashBag()->add('warning''Você não está vinculado a nenhuma linha de pesquisa! Favor entrar em contato imediatamente com a secretaria do programa.');
  45.                     $allowed array_filter([
  46.                         'homepage',
  47.                         'logout',
  48.                         'change_pass',
  49.                         $usuario->getCpf() ? 'perfil' false,
  50.                         'protocolos',
  51.                         'protocolos_concluidos',
  52.                         'view_protocolo',
  53.                         'view_protocolo_number',
  54.                     ]);
  55.                     if (!in_array($event->getRequest()->attributes->get('_route'), $allowed)) {
  56.                         $event->setResponse(new RedirectResponse($this->router->generate('homepage')));
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }