1

When I secure a certain area of my Symfony2 project by the firewall, people are being redirected to the login page. How can I display a dynamic message on that login page, that tells the user about the required user role?

E.g. there are four different pages on my project

  • / is not restricted,
  • /admin requires ROLE_ADMIN,
  • /user requires ROLE_USER,
  • /beta requires ROLE_BETA_USER.

If the user tries to access /beta, above the login page there shall be the information

This area is restricted to beta users.

... and accordingly for /admin and /user. Is there a way to set this message globally (without having to set it on each controller action separately)?

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116

1 Answers1

1

You could do it by implementing AccessDeniedHandlerInterface.

In handle method you can check request path and add appropriate flash message.

You will have to register your AccessDeniedHandler service in security.yml:

security:
    firewalls:
        main:
            access_denied_handler: my.access_denied_handler_service_name
Igor Pantović
  • 9,107
  • 2
  • 30
  • 43