Is it possible to perform an automatic redirect to the some route (i.e. /) for the specific route /login
only for users that are AUTHENTICATED
? and
As you are using FOSUserBundle the rendering of the login form takes place in SecurityController::renderLogin().
The solution is bascially:
IS_AUTHENTICATD_ANONYMOUSLY
I assume you have already created a bundle extending FOSUserBundle which holds your User
Entity.
I assume this bundle is called YourUserBundle
and is located at src/Your/Bundle/UserBundle
.
Now copy (not cut) the SecurityController
vendor/friendsofsymfony/user-bundle/src/FOS/UserBundle/Controller/SecurityController.php
to (in order to override the one provided by FOSUserBundle)
src/Your/Bundle/UserBundle/Controller/SecurityController.php
add the use-statement for RedirectResponse
and edit the renderLogin()
method like this:
use Symfony\Component\HttpFoundation\RedirectResponse;
// ...
protected function renderLogin(array $data)
{
if (false === $this->container->get('security.context')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
return new RedirectResponse('/', 403);
}
$template = sprintf('FOSUserBundle:Security:login.html.%s', $this->container->getParameter('fos_user.template.engine'));
return $this->container->get('templating')->renderResponse($template, $data);
}
Now instead of security.context
use security.authorization_checker
.
http://symfony.com/blog/new-in-symfony-2-6-security-component-improvements