Great extension. My site already has a registration system with User table and UserProfile table so I changed it slightly to call a custom Registration page so you can implement your own registration. Might be worth including something like this in future releases?
Extract Register (createUser) from _doLogin into its own method with protected access. Also make _linkProvider protected
DefaultController.php
private function _doLogin() {
....
} else if ($identity->errorCode == RemoteUserIdentity::ERROR_USERNAME_INVALID) {
// They have authenticated to their provider but we don't have a matching HaLogin entry
if (Yii::app()->user->isGuest) {
$this->registerUser($identity);
} else {
// They are already logged in, link their user account with new provider
...
}
}
}
/**
* Overridable method to display a custom Registration page if desired
* Overrides should make sure to call the following method to link the user to the provider
* $this->_linkProvider($identity);
*
* @param RemoteUserIdentity $identity
*/
protected function registerUser($identity) {
// They aren't logged in => display a form to choose their username & email
// (we might not get it from the provider)
if ($this->module->withYiiUser == true) {
Yii::import('application.modules.user.models.*');
} else {
Yii::import('application.models.*');
}
$user = new User;
if (isset($_POST['User'])) {
//Save the form
$user->attributes = $_POST['User'];
if ($user->validate() && $user->save()) {
if ($this->module->withYiiUser == true) {
$profile = new Profile();
$profile->first_name='firstname';
$profile->last_name='lastname';
$profile->user_id=$user->id;
$profile->save();
}
$identity->id = $user->id;
$identity->username = $user->username;
$this->_linkProvider($identity);
$this->_loginUser($identity);
} // } else { do nothing } => the form will get redisplayed
} else {
//Display the form with some entries prefilled if we have the info.
if (isset($identity->userData->email)) {
$user->email = $identity->userData->email;
$email = explode('@', $user->email);
$user->username = $email[0];
}
}
$this->render('createUser', array(
'user' => $user,
));
}
Then create a new Controller that extends DefaultController and override the method to display your onw custom Registration page
CustomController.php
Yii::import('application.modules.hybridauth.controllers.DefaultController');
class CustomController extends DefaultController {
protected function registerUser($identity) {
// Your custom Code here
}
}
Then set the default controller in the Module config
main.php
'hybridauth' => array(
.....
'defaultController'=>'custom',
....
),
And lastly i had to update render providers widget to use the defaultController as specified in the module rather than the currently hardcoded default controller
providers.php
<?php $controller = Yii::app()->getModule('hybridauth')->defaultController; ?>
....
<form action="<?php echo $this->config['baseUrl'];?>/<?php echo $controller; ?>/login/" method="get" id="hybridauth-openid-form" >
....
<form action="<?php echo $this->config['baseUrl'];?>/<?php echo $controller; ?>/unlink" method="post" id="hybridauth-unlink-form" >
....
<a id="hybridauth-<?php echo $provider ?>" href="<?php echo $baseUrl?>/<?php echo $controller; ?>/login/?provider=<?php echo $provider ?>" >
....
Any suggestions/comments welcome!
Thanks
Ross