Apr 04
Digg
Stumbleupon
Technorati
Delicious

How to customize login action for sfGuardPlugin

Actually, we are going to raise here 2 topics:

  • how to customize login action
  • how to fix referer problem for sfGuardPlugin

Both are quite often using in real life projects. For instance, on logging in user stage you may want to store some extra information which is not provided by sfGuardPlugin by default. Lets say you want to store referer information.

So first of all you would need to override login handler for default sfGuard plugin. For this create a sfGuardAuth module in your application (don’t use the init-module task, just create a sfGuardAuth directory). And copy into lib folder BasesfGuardAuthActions.class.php and into actions directory actions.class.php (take these files from sfGuardPlugin original folder).

Now we are going to edit BasesfGuardAuthActions.class.php, it contains public function executeSignin which is responsible for actual signing in mechanism. Most functionality for actual logging was actual performed so this is the only lastest steps before redirecting user to tha login success/failure steps.

So lets add into this function some code (we assume using of sfGuardUserProfile class along with sfGuardUser so we actually work with extended version of guard plugin. We reffer you to our previous articles “Extending sfGuardPlugin part 1 part 2):

if ($profile = $user->getProfile()) {
$profile->setReferer($referer);
$profile->save();
}

The same way we may handle here the places where should be redirected user after success logging in (as you know sfGuardUser provides singin url success parameter but sometimes we need more than one place to redirect after login, e.g. based on referer we may login user to different locations of dashboard or so).

Next interesting thing is about redirecting user to the page from where he tried to login. Basically, sfGuardPlugin supports this and provides referer parameter for it. Originally this piece of code looks like:

$referer = $user->getAttribute(‘referer’, $this->getRequest()->getReferer());

But it never worked for me. There is a ticket opened currently for guard plugin where is proposed to use:

$referer = $user->getAttribute(‘referer’) ? $user->getAttribute(‘referer’) : $this->getRequest()->getReferer();

This approach looks better and the other guys confirmed it works but I could nto figure out why it does not work for me.
So finally we came to decision that the following code should be added into the template from where is requesting login mechanism. Lets say you have the site where visitors are browsing some data and you have vote link per each data item. And when user clicks on vote you want to popup login form and when user enters in required information you want him to be logged in and stay on the same page. So for it we add the following code to this “browse data” template:

$sf_user->setAttribute(‘referer’, $sf_request->getUri());

which basically setup referer in forced way. In our understanding this stuff had to be built into guard plugin but somehow it does not work or works wrong way. Hopefully this is going to be resolved or documented on official sfGuardPlugin page).

We are really sure there must be the other workarounds for it and if you know them share with us so the other guys know how this can be done in easier maner.

Guys, have a great weekend!


Author: symfonian

1 Comment(s)

Gijs Nelissen
June 16, 2008

Dear,

Read my comment in ticket #3212

I fixed it changing one line in the signin method.

Kind regards,

Comments RSS TrackBack Identifier URI

Leave a comment