Alex
Alex

Reputation: 51

Symfony Action Security - How to forward after successful authentication?

With Symfony's Action Security if a user has not been identified he will be forwarded to the default login action as defined in the applications settings.yml file. How would I forward the user to the originally requested action after the user is successfully authenticated?

Upvotes: 1

Views: 5924

Answers (3)

Druckles
Druckles

Reputation: 3782

A related problem, but instead trying to perform the forward from a different action:

If you have an action protected by sfGuard which is attempting to redirect to the referrer, you will get a redirect loop after signing in. This is because the sign-in page of sfGuard wil become the referrer. A parameter or attribute can be saved over multiple requests if stored in the sign-in action as above, meaning the action might be redirecting to an incorrect page if already signed in. The solution is to use a flash which will be forgotten. This can be accomplished with the following code in the executeSignin method of sfGuardAuthActions:

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

By resetting the flash in the first block, it won't be forgotten between login attempts, and by using a flash, signing in from other pages can't interfere with your action.

Upvotes: 0

deresh
deresh

Reputation: 510

On first hit to your login action, store referer to the user session:

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

and then when login succeeds, redirect user to stored referer with:

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

You have complete example in sfGuardPlugin:

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

Upvotes: 8

comster
comster

Reputation:

More simply...

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

like

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

Upvotes: 1

Related Questions