src/App/EventSubscriber/Framework/AccessDeniedSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Framework;
  3. use App\Entity\Profile;
  4. use App\Entity\Profile\JoboffererProfile;
  5. use App\Entity\Profile\JobseekerProfile;
  6. use App\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Exception;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  17. use Twig\Environment;
  18. class AccessDeniedSubscriber implements EventSubscriberInterface
  19. {
  20. private $router;
  21. private $entityManager;
  22. private $tokenStorage;
  23. private $template;
  24. public function __construct(RouterInterface $router, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage, Environment $template)
  25. {
  26. $this->router = $router;
  27. $this->entityManager = $entityManager;
  28. $this->tokenStorage = $tokenStorage;
  29. $this->template = $template;
  30. }
  31. public static function getSubscribedEvents()
  32. {
  33. return [
  34. KernelEvents::EXCEPTION => ['onKernelException', 1],
  35. ];
  36. }
  37. /** @throws Exception */
  38. public function onKernelException(ExceptionEvent $event): void
  39. {
  40. $token = $this->tokenStorage->getToken();
  41. /** @var User $user */
  42. $user = null;
  43. if (!is_null($token)) {
  44. $user = $token->getUser();
  45. }
  46. if (is_object($user) && $event->getThrowable() instanceof AccessDeniedException) {
  47. $request = $event->getRequest();
  48. $matchedRoute = $request->attributes->get('_route');
  49. if (in_array($matchedRoute, [
  50. 'account.conversations.show_jobseeker',
  51. 'account.conversations.show_jobofferer'
  52. ])) {
  53. $username = $this->getLoginUsername($request, $matchedRoute);
  54. $response = new Response($this->template->render('errors/access_denied.html.twig', [
  55. 'username' => $username,
  56. 'targetPath' => $request->getRequestUri()
  57. ]), Response::HTTP_FORBIDDEN);
  58. $response->prepare($request);
  59. $event->setResponse($response);
  60. }
  61. }
  62. }
  63. protected function getLoginUsername(Request $request, string $route): string
  64. {
  65. $profileId = $route === 'account.conversations.show_jobseeker' ? $request->attributes->get('jobseekerProfileId') : $request->attributes->get('joboffererProfileId');
  66. $class = $route === 'account.conversations.show_jobseeker' ? JobseekerProfile::class : JoboffererProfile::class;
  67. /** @var Profile $profile */
  68. $profile = $this->entityManager->getRepository($class)->find($profileId);
  69. return !is_null($profile) ? $profile->getUser()->getUsername() : '';
  70. }
  71. }