I would like to check from inside a controller if it is a secured page or not.
How to do this ?
My use case is the following :
- User can register and log in
- If he logs in and tries to access a secured page, he will be redirected to a “beta version” page until the end of June.
- If he tries to access a normal page (not secured), he will be able to access it without any redirection.
Thanks for your help !
Aurel
When Symfony2 processes a request it matches the url pattern with each firewall defined in
app/config/security.yml. When url pattern matches with a pattern of the firewall Symfony2 creates some listener objects and callhandlemethod of those objects. If any listener returns aResponseobject then the loop breaks and Symfony2 outputs the response. Authentication part is done in authentication listeners. They are created from config defined in matched firewall e.gform_login,http_basicetc. If user is not authenticated then authenticated listeners create aRedirectResponseobject to redirect user to login page. For your case, you can cheat by creating a custom authentication listener and add it in your secured page firewall. Sample implementation would be following,Create a
Tokenclass,Create a class that implements
AuthenticationProviderInterface. Forform_loginlistener it authenticates with the givenUserProvider. In this case it will do nothing.Create an entry point class. The listener will create a
RedirectResponsefrom this class.Create a listener class. Here you will implement your redirection logic.
Create the services.
Register the listener. Create a folder named
Security/Factoryin your bundlesDependencyInjectionfolder. Then create the factory class.Then in your
NamespaceBundle.phpof your bundle folder add the following code.Authentication listener is created, phew :). Now in your
app/config/security.ymldo following.