<?php
/**
* Created by PhpStorm.
* User: ricar
* Date: 19/11/2018
* Time: 11:48
*/
namespace App\EventListener;
use App\Repository\MatriculaRepository;
use App\Repository\UsuarioRepository;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
class ResponseListener
{
private Security $security;
private MatriculaRepository $matriculaRepository;
private UsuarioRepository $usuarioRepository;
private Session $session;
private RouterInterface $router;
public function __construct(
Security $security,
MatriculaRepository $matriculaRepository,
UsuarioRepository $usuarioRepository,
RequestStack $requestStack,
RouterInterface $router
)
{
$this->security = $security;
$this->matriculaRepository = $matriculaRepository;
$this->usuarioRepository = $usuarioRepository;
$this->session = $requestStack->getSession();
$this->router = $router;
}
public function onKernelResponse(ResponseEvent $event): void
{
if ($this->security->getUser()) {
$usuario = $this->usuarioRepository->findOneBy(['email' => $this->security->getUser()->getUserIdentifier()]);
if ($usuario && $usuario->isAluno() && !$usuario->isProfessor() && !$usuario->isFuncionario()) {
if (is_null($this->matriculaRepository->getMatriculaCorrente($usuario->getId()))) {
$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.');
$allowed = array_filter([
'homepage',
'logout',
'change_pass',
$usuario->getCpf() ? 'perfil' : false,
'protocolos',
'protocolos_concluidos',
'view_protocolo',
'view_protocolo_number',
]);
if (!in_array($event->getRequest()->attributes->get('_route'), $allowed)) {
$event->setResponse(new RedirectResponse($this->router->generate('homepage')));
}
}
}
}
}
}