/** * * @param \yii\authclient\ClientInterface $client * @return type */ public function successCallback($client) { // TODO: Group FK's to one local user. // Otherwise, if we log in via FB and another time via google, we // end up with two local accounts. if (!$this->action instanceof \yii\authclient\AuthAction) { throw new \yii\base\InvalidCallException("successCallback is only meant to be executed by AuthAction!"); } $attributes = $client->getUserAttributes(); $externalUser = new AuthForm(); $externalUser->authProvider = $client->getName(); $externalUser->externalUserId = array_key_exists('id', $attributes) ? $attributes['id'] : null; if ($externalUser->validate()) { Yii::info('AuthForm validated.'); if ($externalUser->isRegistered()) { Yii::info('ExternalUser is registered. Logging in and redirecting to game/index.'); $externalUser->login(); return $this->action->redirect(Url::to(['game/index'], true)); } else { Yii::info('ExternalUser is not registered. Redirecting to site/signup.'); Yii::$app->session->set('game/register/authProviderName', $client->getName()); Yii::$app->session->set('game/register/authProviderTitle', $client->getTitle()); Yii::$app->session->set('game/register/attributes', $attributes); return $this->action->redirect(Url::to(['site/signup'], true)); } } else { // TODO error. Throw, display actionError? Yii::info('AuthForm couldn\'t be validated. Errors: ' . print_r($externalUser->errors, true)); Yii::info('Client attributes: ' . print_r($attributes, true)); } }
/** * @param $client * * TODO */ public function onAuthSuccess(ClientInterface $client) { $attributes = $client->getUserAttributes(); /* @var $auth UserAuth */ $auth = UserAuth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one(); if (Yii::$app->user->isGuest) { if ($auth) { $user = $auth->user; Yii::$app->user->login($user); } else { if (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) { Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]); } else { if ($client->signIn()) { $this->redirect('/profile'); } } } } else { if (!$auth) { $auth = new UserAuth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]); $auth->save(); } } }
/** * Outputs client auth link. * @param \yii\authclient\ClientInterface $client external auth client instance. * @return string */ public function clientLink($client) { if (!$this->inlineForm) { $text = '<i class="icon-social icon-' . $client->getName() . '"></i> ' . $client->getTitle(); $class = 'auth-btn'; } else { $text = '<i class="icon-social icon-' . $client->getName() . '-small"></i>'; $class = 'auth-btn-small'; } return Html::a($text, $this->createClientUrl($client), ['class' => $class . ' auth-' . $client->getName(), 'title' => $client->getTitle()]); }
/** * @param ClientInterface $client */ public function onAuthSuccess($client) { $attributes = $client->getUserAttributes(); $email = ArrayHelper::getValue($attributes, 'email'); /** @var Auth $auth */ $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one(); if (Yii::$app->user->isGuest) { if ($auth) { // login $user = $auth->user; Yii::$app->user->login($user, 3600 * 24 * 30); } else { // signup if (User::find()->where(['email' => $email])->exists()) { Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]); } else { $password = Yii::$app->security->generateRandomString(6); $user = new User(['username' => $attributes['login'], 'email' => $email, 'password' => $password]); $user->generateAuthKey(); $user->generatePasswordResetToken(); $transaction = $user->getDb()->beginTransaction(); if ($user->save()) { $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]); if ($auth->save()) { $transaction->commit(); Yii::$app->user->login($user, 3600 * 24 * 30); } else { print_r($auth->getErrors()); die; } } else { print_r($user->getErrors()); die; } } } } else { // user already logged in if (!$auth) { // add auth provider $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]); $auth->save(); } } }
/** * Outputs client auth link. * @param ClientInterface $client external auth client instance. * @param string $text link text, if not set - default value will be generated. * @param array $htmlOptions link HTML options. * @throws InvalidConfigException on wrong configuration. */ public function clientLink($client, $text = null, array $htmlOptions = []) { if ($this->shortView) { $text = ''; } elseif ($text === null) { $text = Html::tag('span', $client->getTitle(), ['class' => 'auth-title']); } if (!array_key_exists('class', $htmlOptions)) { $htmlOptions['class'] = $client->getName(); } $viewOptions = $client->getViewOptions(); if (empty($viewOptions['widget'])) { if ($this->popupMode) { if (isset($viewOptions['popupWidth'])) { $htmlOptions['data-popup-width'] = $viewOptions['popupWidth']; } if (isset($viewOptions['popupHeight'])) { $htmlOptions['data-popup-height'] = $viewOptions['popupHeight']; } } echo Html::a($text, $this->createClientUrl($client), $htmlOptions); } else { $widgetConfig = $viewOptions['widget']; if (!isset($widgetConfig['class'])) { throw new InvalidConfigException('Widget config "class" parameter is missing'); } /* @var $widgetClass Widget */ $widgetClass = $widgetConfig['class']; if (!is_subclass_of($widgetClass, AuthChoiceItem::className())) { throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::className() . '"'); } unset($widgetConfig['class']); $widgetConfig['client'] = $client; $widgetConfig['authChoice'] = $this; echo $widgetClass::widget($widgetConfig); } }
/** * Outputs client auth link. * @param ClientInterface $client external auth client instance. * @param string $text link text, if not set - default value will be generated. * @param array $htmlOptions link HTML options. * @return string generated HTML. * @throws InvalidConfigException on wrong configuration. */ public function clientLink($client, $text = null, array $htmlOptions = []) { $viewOptions = $client->getViewOptions(); if (empty($viewOptions['widget'])) { if ($text === null) { $text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]); } if (!isset($htmlOptions['class'])) { $htmlOptions['class'] = $client->getName(); } if (!isset($htmlOptions['title'])) { $htmlOptions['title'] = $client->getTitle(); } Html::addCssClass($htmlOptions, ['widget' => 'auth-link']); if ($this->popupMode) { if (isset($viewOptions['popupWidth'])) { $htmlOptions['data-popup-width'] = $viewOptions['popupWidth']; } if (isset($viewOptions['popupHeight'])) { $htmlOptions['data-popup-height'] = $viewOptions['popupHeight']; } } return Html::a($text, $this->createClientUrl($client), $htmlOptions); } $widgetConfig = $viewOptions['widget']; if (!isset($widgetConfig['class'])) { throw new InvalidConfigException('Widget config "class" parameter is missing'); } /* @var $widgetClass Widget */ $widgetClass = $widgetConfig['class']; if (!is_subclass_of($widgetClass, AuthChoiceItem::className())) { throw new InvalidConfigException('Item widget class must be subclass of "' . AuthChoiceItem::className() . '"'); } unset($widgetConfig['class']); $widgetConfig['client'] = $client; $widgetConfig['authChoice'] = $this; return $widgetClass::widget($widgetConfig); }
/** * Outputs client auth link. * @param ClientInterface $client external auth client instance. * @param string $text link text, if not set - default value will be generated. * @param array $htmlOptions link HTML options. */ public function clientLink($client, $text = null, array $htmlOptions = []) { if ($text === null) { $text = Html::tag('span', '', ['class' => 'auth-icon ' . $client->getName()]); $text .= Html::tag('span', $client->getTitle(), ['class' => 'auth-title']); } if (!array_key_exists('class', $htmlOptions)) { $htmlOptions['class'] = 'auth-link ' . $client->getName(); } if ($this->popupMode) { $viewOptions = $client->getViewOptions(); if (isset($viewOptions['popupWidth'])) { $htmlOptions['data-popup-width'] = $viewOptions['popupWidth']; } if (isset($viewOptions['popupHeight'])) { $htmlOptions['data-popup-height'] = $viewOptions['popupHeight']; } } echo Html::a($text, $this->createClientUrl($client), $htmlOptions); }
/** * handle */ public function handle() { $attributes = $this->client->getUserAttributes(); // common $continue = false; $id = ArrayHelper::getValue($attributes, 'id'); $fullname = ''; $email = ''; // google if ($this->client->getName() == 'google') { $fullname = ArrayHelper::getValue($attributes, 'displayName'); $emails = ArrayHelper::getValue($attributes, 'emails'); $email = $emails[0]['value']; $continue = true; } // facebook if ($this->client->getName() == 'facebook') { $fullname = ArrayHelper::getValue($attributes, 'name'); $email = ArrayHelper::getValue($attributes, 'email'); $continue = true; } if (!$continue) { // Yii::$app->getSession()->setFlash('info', [ // Yii::t('app', 'Flickr'), // ]); //Yii::$app->user->setReturnUrl(Yii::$app->request->referrer); return; } /* @var Auth $auth */ $auth = Auth::find()->where(['source' => $this->client->getId(), 'source_id' => $id])->one(); if (Yii::$app->user->isGuest) { if ($auth) { // login /* @var Account $user */ $user = $auth->user; $this->updateUserInfo($user); Yii::$app->user->login($user, Setting::getValue('rememberMeDuration')); } else { // signup if ($email !== null && Account::find()->where(['email' => $email])->exists()) { Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $this->client->getTitle()])]); } else { $password = Yii::$app->security->generateRandomString(6); $user = new Account(['fullname' => $fullname, 'email' => $email, 'password' => $password]); $user->generateAuthKey(); $user->generatePasswordResetToken(); $transaction = Account::getDb()->beginTransaction(); //file_put_contents('D:\log', json_encode($transaction)); if ($user->save()) { $auth = new Auth(['user_id' => $user->id, 'source' => $this->client->getId(), 'source_id' => (string) $id]); if ($auth->save()) { $transaction->commit(); Yii::$app->user->login($user, Setting::getValue('rememberMeDuration')); } else { $transaction->rollBack(); Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to save {client} account: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($auth->getErrors())])]); } } else { $transaction->rollBack(); Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to save user: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($user->getErrors())])]); } } } } else { // user already logged in Yii::$app->user->setReturnUrl(Yii::$app->request->referrer); if (!$auth) { // add auth provider $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $this->client->getId(), 'source_id' => (string) $attributes['id']]); if ($auth->save()) { /** @var Account $user */ $user = $auth->user; $this->updateUserInfo($user); Yii::$app->getSession()->setFlash('success', [Yii::t('app', 'Linked {client} account.', ['client' => $this->client->getTitle()])]); } else { Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to link {client} account: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($auth->getErrors())])]); } } else { // there's existing auth Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to link {client} account. There is another user using it.', ['client' => $this->client->getTitle()])]); } } }