Advertisement
Guest User

Untitled

a guest
Feb 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.37 KB | None | 0 0
  1.     public function onAuthSuccess(ClientInterface $client)
  2.     {
  3.         return;
  4.  
  5.         $attributes = $client->getUserAttributes();
  6.         $email = ArrayHelper::getValue($attributes, 'email');
  7.         $id = ArrayHelper::getValue($attributes, 'id');
  8.         #$nickname = ArrayHelper::getValue($attributes, 'login');
  9.  
  10.         #Y::dump($_REQUEST, false);
  11.        #Y::dump($attributes, true);
  12.  
  13.         $email = null;
  14.  
  15.         /* @var Auth $auth */
  16.         $auth = Auth::find()->where([
  17.             'source' => $client->getId(),
  18.             'source_id' => $id,
  19.         ])->one();
  20.  
  21.         if (Yii::$app->user->isGuest) {
  22.             if ($auth) { // login
  23.                 /* @var User $user */
  24.                 $user = $auth->user;
  25.                 #$this->updateUserInfo($user);
  26.                Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  27.             } else { // signup
  28.  
  29.                 if ($email === null) {
  30.                     Y::app()->getSession()->set('emptyEmail', ArrayHelper::getValue($attributes, 'User'));
  31.                     Yii::$app->getSession()->setFlash('error', 'User with the same email already exists but isn\'t linked to it. Login using email first to link it.');
  32.                 } elseif ($user = User::findOne(['email' => $email])) {
  33.                     $transaction = User::getDb()->beginTransaction();
  34.  
  35.                     $auth = new Auth([
  36.                         'user_id' => $user->id,
  37.                         'source' => $client->getId(),
  38.                         'source_id' => (string)$id,
  39.                     ]);
  40.                     if ($auth->save()) {
  41.                         $transaction->commit();
  42.                         Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  43.                     } else {
  44.                         Yii::$app->getSession()->setFlash('error', 'Unable to save, errors: ' . json_encode($auth->getErrors()));
  45.                     }
  46.                 } else {
  47.                     #Y::dump(Yii::$app->params, true);
  48.                    $password = Yii::$app->security->generateRandomString(6);
  49.                     $user = new User([
  50.                         #'username' => $nickname,
  51.                        #'github' => $nickname,
  52.                        'email' => $email,
  53.                         'password' => $password,
  54.                     ]);
  55.                     $user->generateAuthKey();
  56.                     $user->generatePasswordResetToken();
  57.  
  58.                     $transaction = User::getDb()->beginTransaction();
  59.  
  60.                     if ($user->save()) {
  61.                         $auth = new Auth([
  62.                             'user_id' => $user->id,
  63.                             'source' => $client->getId(),
  64.                             'source_id' => (string)$id,
  65.                         ]);
  66.                         if ($auth->save()) {
  67.                             $transaction->commit();
  68.                             Yii::$app->user->login($user, Yii::$app->params['user.rememberMeDuration']);
  69.                         } else {
  70.                             Yii::$app->getSession()->setFlash('error', [
  71.                                 Yii::t('app', 'Unable to save {client} account: {errors}', [
  72.                                     'client' => $client->getTitle(),
  73.                                     'errors' => json_encode($auth->getErrors()),
  74.                                 ]),
  75.                             ]);
  76.                         }
  77.                     } else {
  78.                         Yii::$app->getSession()->setFlash('error', [
  79.                             Yii::t('app', 'Unable to save user: {errors}', [
  80.                                 'client' => $client->getTitle(),
  81.                                 'errors' => json_encode($user->getErrors()),
  82.                             ]),
  83.                         ]);
  84.                     }
  85.                 }
  86.             }
  87.         } else { // user already logged in
  88.             if (!$auth) { // add auth provider
  89.                 Y::dump($attributes, true);
  90.                 $auth = new Auth([
  91.                     'user_id' => Yii::$app->user->id,
  92.                     'source' => $client->getId(),
  93.                     'source_id' => (string)$attributes['id'],
  94.                 ]);
  95.                 if ($auth->save()) {
  96.                     /** @var User $user */
  97.                     $user = $auth->user;
  98.                     #$this->updateUserInfo($user);
  99.                    Yii::$app->getSession()->setFlash('success', [
  100.                         Yii::t('app', 'Linked {client} account.', [
  101.                             'client' => $client->getTitle()
  102.                         ]),
  103.                     ]);
  104.                 } else {
  105.                     Yii::$app->getSession()->setFlash('error', [
  106.                         Yii::t('app', 'Unable to link {client} account: {errors}', [
  107.                             'client' => $client->getTitle(),
  108.                             'errors' => json_encode($auth->getErrors()),
  109.                         ]),
  110.                     ]);
  111.                 }
  112.             } else { // there's existing auth
  113.                 Yii::$app->getSession()->setFlash('error', [
  114.                     Yii::t('app',
  115.                         'Unable to link {client} account. There is another user using it.',
  116.                         ['client' => $client->getTitle()]),
  117.                 ]);
  118.             }
  119.         }
  120.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement