コード例 #1
0
ファイル: AuthChoice.php プロジェクト: lowbase/yii2-user
 /**
  * Добавляем обрамление div
  * @param ClientInterface $client
  * @param null $text
  * @param array $htmlOptions
  * @throws InvalidConfigException
  */
 public function clientLink($client, $text = null, array $htmlOptions = [])
 {
     echo Html::beginTag('div', ['class' => $this->clientCssClass]);
     $text = Html::tag('span', $text, ['class' => 'auth-icon ' . $client->getName()]);
     if (!array_key_exists('class', $htmlOptions)) {
         $htmlOptions['class'] = 'auth-link ' . $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) . '<br>';
     } 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);
     }
     echo Html::endTag('div');
 }
コード例 #2
0
ファイル: SiteController.php プロジェクト: iw-reload/iw
 /**
  * 
  * @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));
     }
 }
コード例 #3
0
ファイル: AuthRedSocial.php プロジェクト: alejandrososa/AB
 /**
  * Red Social
  */
 public function autorizar()
 {
     $attributes = $this->client->getUserAttributes();
     $model = new LoginRedSocialForm();
     $model->red_social = $this->client->getName();
     $model->perfil_id = $attributes['id'];
     $model->correo = isset($attributes['email']) ? $attributes['email'] : null;
     $model->nombre = $attributes['first_name'];
     $model->apellido = $attributes['last_name'];
     $model->genero = $attributes['gender'];
     $model->url_perfil = $attributes['link'];
     $model->localidad = $attributes['locale'];
     $model->estado = $attributes['verified'];
     $model->access_token = $this->token_acces;
     $model->imagen = $this->getImagenPerfilFacebook($attributes['id']);
     //Set return url if the user is authenticated. AuthAction will handle the redirect
     if ($returnUrl = $model->autenticarUsuario()) {
         \Yii::$app->user->setReturnUrl($returnUrl);
     }
 }
コード例 #4
0
 public function social(ClientInterface $client)
 {
     $user_data = new UserData($client->getName(), $client->getUserAttributes());
     if (Yii::$app->user->isGuest) {
         $user = User::findSocial($user_data->getObjectName(), $user_data->getObjectName());
         if ($user) {
             Login::login($user);
         } else {
             $user_data->save();
         }
     } else {
         User::saveSocial(Yii::$app->user->identity, $user_data);
     }
     Yii::$app->session->set('social', $client->getName());
 }
コード例 #5
0
 /**
  * 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);
 }
コード例 #6
0
ファイル: AuthChoice.php プロジェクト: cakpep/spk-tht
 /**
  * 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);
 }
コード例 #7
0
ファイル: AuthChoice.php プロジェクト: bessonov87/bw
 /**
  * Composes client auth URL.
  * @param \yii\authclient\ClientInterface $client external auth client instance.
  * @return string auth URL.
  */
 public function createClientUrl($client)
 {
     return '/site/auth?authclient=' . $client->getName();
 }
コード例 #8
0
 /**
  * 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()])]);
         }
     }
 }