Symfony Action Security - How to forward after successful authentication?

When using Symfony Action Security, if the user is not authenticated, they will be redirected to the default login action as defined in the applications settings.yml file. How would I redirect the user to the originally requested action after successfully authenticating the user?

+1


source to share


3 answers


The first time you access your login action, store the referent in the user session:

if(!$this->getUser()->hasParameter('referer'))
{
  $this->getUser()->setParameter('referer',$this->getRequest()->getReferer());
}

      

and then on successful login, redirects the user to the saved referent with:



$this->redirect($this->getUser()->getParameter('referer'));

      

You have a complete example in sfGuardPlugin:

http://www.symfony-project.org/plugins/sfGuardPlugin

+8


source


Simply put...

$this->getUser()->setReferer($this->getRequest()->getReferer());

      



as

setReferer($referer)
{
  if (!$this->hasAttribute('referer'))
    $this->setAttribute('referer', $referer);
}

      

+1


source


Related issue, but instead trying to forward from another action:

If you have an action protected by sfGuard that tries to redirect to a referrer, you will get a redirect loop after login. This is because the sfGuard login page wil becomes the referee. A parameter or attribute can be saved across multiple requests if it is stored in the login action as above, which means the action might be redirected to an invalid page if it is already enabled. The solution is to use a flash to be forgotten. This can be accomplished with the following code in the executeSignin method for sfGuardAuthActions:

if ($this->getUser()->hasFlash('referer'))
{
  $this->getUser()->setFlash('referer', $this->getUser()->getFlash('referer'));
}
else
{   
  $this->getUser()->setFlash('referer', $this->getRequest()->getReferer());
}

      

By dumping the flash in the first block, it will not be forgotten between login attempts, and with flash, login from other pages cannot interfere with your action.

0


source







All Articles