protected function registerLocalProfile(ProfileForm $localProfile, HybridauthForm $remoteLogin, $localIdentity = false) { if (!isset($_POST['ProfileForm']) && $localIdentity === false) { $userIdentityClass = $localProfile->userIdentityClass; $remoteProfile = $remoteLogin->getHybridAuthAdapter()->getUserProfile(); $localProfile->setAttributes($userIdentityClass::getRemoteAttributes($remoteProfile)); $localProfile->validate(); return $localProfile; } if ($localIdentity !== false) { $userIdentityClass = $localProfile->userIdentityClass; $remoteProfile = $remoteLogin->getHybridAuthAdapter()->getUserProfile(); $localProfile->setAttributes($userIdentityClass::getRemoteAttributes($remoteProfile)); } if (isset($_POST['ProfileForm']) && is_array($_POST['ProfileForm'])) { $localProfile->setAttributes($_POST['ProfileForm']); } if (!$localProfile->validate()) { return $localProfile; } $trx = Yii::app()->db->beginTransaction(); if (!$localProfile->save($this->module->requireVerifiedEmail)) { $trx->rollback(); Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to register a new user.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.')); return $localProfile; } $trx->commit(); if ($this->module->requireVerifiedEmail) { if ($this->sendEmail($localProfile, 'verify')) { Yii::app()->user->setFlash('success', Yii::t('UsrModule.usr', 'An email containing further instructions has been sent to the provided email address.')); } else { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to send an email.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.')); } } // don't forget to associate the new profile with remote provider if (!$remoteLogin->associate($localProfile->getIdentity()->getId())) { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to associate current user with {provider}.', array('{provider}' => $remoteLogin->provider))); $this->redirect(array('login', 'provider' => $remoteLogin->provider)); } if ($localProfile->getIdentity()->isActive()) { // don't use the $localProfile->login() method because there is no password set so we can't authenticate this identity if (Yii::app()->user->login($localProfile->getIdentity(), 0)) { $this->afterLogin(); } else { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to log in.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.')); } } else { if (!Yii::app()->user->hasFlash('success')) { Yii::app()->user->setFlash('success', Yii::t('UsrModule.usr', 'Please wait for the account to be activated. A notification will be send to provided email address.')); } $this->redirect(array('login', 'provider' => $remoteLogin->provider)); } return $localProfile; }
protected function registerLocalProfile(ProfileForm $localProfile, HybridauthForm $remoteLogin) { if (isset($_POST['ProfileForm'])) { $localProfile->setAttributes($_POST['ProfileForm']); if ($localProfile->register()) { if ($this->module->requireVerifiedEmail) { if ($this->sendEmail($localProfile, 'verify')) { Yii::app()->user->setFlash('success', Yii::t('UsrModule.usr', 'An email containing further instructions has been sent to provided email address.')); } else { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to send an email.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.')); } } // don't forget to associate the new profile with remote provider if (!$remoteLogin->associate($localProfile->getIdentity()->getId())) { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to associate current user with {provider}.', array('{provider}' => $remoteLogin->provider))); $this->redirect('login'); } if ($localProfile->getIdentity()->isActive()) { // don't use the $localProfile->login() method because there is no password set so we can't authenticate this identity if (Yii::app()->user->login($localProfile->getIdentity(), 0)) { $this->afterLogin(); } else { Yii::app()->user->setFlash('error', Yii::t('UsrModule.usr', 'Failed to log in.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.')); } } else { if (!Yii::app()->user->hasFlash('success')) { Yii::app()->user->setFlash('success', Yii::t('UsrModule.usr', 'Please wait for the account to be activated. A notification will be send to provided email address.')); } $this->redirect(array('login')); } } } else { $profile = $remoteLogin->getHybridAuthAdapter()->getUserProfile(); $email = $profile->emailVerifier !== null ? $profile->emailVerifier : $profile->email; $localProfile->setAttributes(array('username' => $email, 'email' => $email, 'firstName' => $profile->firstName, 'lastName' => $profile->lastName)); } return $localProfile; }