Question

How do you set the user culture during the signin process which is handled by the sfDoctrineGuardPlugin? Each user can set their preferred language which is stored in the sfGuardUserProfile table.

Originally I had thought about overriding the executeSignin() function by doing something similar to this:

public function executeSignin(sfWebRequest $request)
{
  $this->getUser()->setCulture($this->getUser()->getGuardUser()->getProfile()->getLanguage());
  parent::executeSignin($request);
}

but obviously I can't do that since I don't have access to the GuardUser object before the parent function is executed.

Then I thought about creating a postExectute() function similar to this:

public function postExecute()
{
  if($this->getUser()->isAuthenticated()){
    //SET USER CULTURE
  }
}

but again this can't work because the signin function does a redirect so postExecute() will never run.

I cannot put my logic to set the culture on the default page after login, because the user is not always redirected to the same page or even the same module.

Was it helpful?

Solution

I will go on something easier than re-create the action.

In your file /apps/[your app]/lib/user/myUser.class.php which extends sfGuardSecurityUser:

You can override the signIn function like that :

public function signIn($user, $remember = false, $con = null)
{
  parent::signin($user, $remember, $con);

  $this->setCulture($user->getProfile()->getLanguage());
}

So every time a user is log in, it will have its culture setted.

OTHER TIPS

Your first guess is a right approach, just need a change ;)

Instead of calling the parent executeSignin() write your own signin action (e.g. copy the one used by the plugin). Then add the line you need

$this->getUser()->setCulture($this->getUser()->getGuardUser()->getProfile()->getLanguage());

just before the redirection is made - so when you already have the Profile assigned to your user session.

You can change the setting in settings.yml to have sfDoctrineGuard use the new action:

all:
  .settings:
    login_module:          myCleverModule
    login_action:          signinMyUser

You can also create a folder in your app/appName/modules/sfGuardAuth/actions, create an actions.class.php there and put your executeSignin() function in this file. It will automatically override the original from the plugin.

You don't have access to the sfGuardUser object before authentication. This doesn't mean you don't have access to an sfUser from an action instance which is what you need.

Does the line

$this->getUser()->isAuthenticated()

not suggest to you that a user is not necessarily authenticated when you call it.

So if that line doesn't set the culture, look somewhere else. I'd start with checking if the culture is present in the request in some form that might override your previous line so try swapping those.

In Symfony, the sfUser represents a PHP session which is started before the user signs in and not a user. You try to manipulate the session data via sfUser::setAttribute so that should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top