Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Setting fields before saving user data #38

Closed
re1naldo opened this issue Feb 9, 2014 · 7 comments
Closed

Setting fields before saving user data #38

re1naldo opened this issue Feb 9, 2014 · 7 comments
Labels

Comments

@re1naldo
Copy link

re1naldo commented Feb 9, 2014

Is it possible to set certain fields (like timezone, country, etc.) to related User model before hoauth saves User data? For example, when user registers using Facebook account, application could retrieve various data from his Facebook account and save it to User model.

If it is possible, where should we place the code? I saw we only have 2 callbacks (i.e. hoauthAfterLogin and hoauthCheckAccess) and User model has been created when they are called.

@SleepWalker
Copy link
Owner

There is one possibility to specify some User model attributes in action property attributes. But this helps only with static value, e.g. status => 1 for all new users.

When you to change some attributes according to the user's social profile, for now, you can only use hoauthProcessUser($user, $userProfile) callback. But this callback is responsible for the whole user registration process. So to use it you should copy some code from original HOAuthAction::processUser() method.

And another variant could be with edition of User model and using beforeSave() method. For example you already have country attribute in your User model, but you don't want to use country name, that is returned by social network. Than you can do the following:
action config:

public function actions()
{
  ...
    'attributes' => array(
      'socialCountry' => 'country',
    ),
 ...
}

Than in your model:

public function setSocialCountry($country)
{
  $mainland = $this->mainlandByCountry($country);
  $this->timezone = $mainland.'/'.$country; // here is timezone setting

  $countryModel = Country::model()->findByAttributes(array('name' => $country));
  $this->country_id = $countryModel ? $countryModel->primaryKey : new CDbExpression('NULL');
}

so you even need no beforeSave() method call.

when you need, I can also add one more callback that will be called before user's model saving.

@re1naldo
Copy link
Author

Sorry for my late response.

Thank you for such detailed explanation. Very helpful indeed, but the country value that was returned from social network was empty. It may be related to this issue.

@re1naldo
Copy link
Author

Is it possible to set country field in User table with certain value when social network provider returns no country data? If I understand your code correctly, setSocialCountry() method won't be called when country data is empty. Is this true?

@re1naldo re1naldo reopened this Feb 28, 2014
@SleepWalker
Copy link
Owner

@re1naldo, it should be actually called. see this function: https://github.com/SleepWalker/hoauth/blob/master/HOAuthAction.php#L440 . So all HOAuthAction::$attributes will be setted during model population, even if in profile data it will be an empty field.

You have some more options:

@re1naldo
Copy link
Author

re1naldo commented Mar 4, 2014

Please correct me if I am wrong, but if I saw this line (https://github.com/SleepWalker/hoauth/blob/master/HOAuthAction.php#L463), user attribute will only be set when profile data is not empty, won't it?

As setSocialCountry() method was placed in User model, then it won't be called when country data from social network is empty, right?

Anyway, thanks for mentioning beforeSave() method. I'm sure it can be used for handling empty fields.

@SleepWalker
Copy link
Owner

Please correct me if I am wrong, but if I saw this line (https://github.com/SleepWalker/hoauth/blob/master/HOAuthAction.php#L463), user attribute will only be set when profile data is not empty, won't it?

oh, yes. You are right, sorry.

As setSocialCountry() method was placed in User model, then it won't be called when country data from social network is empty, right?

yes. that's right too

@re1naldo
Copy link
Author

re1naldo commented Mar 4, 2014

Ok. Thanks for the clarification.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants