Exemplo n.º 1
0
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     /* @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);
         } else {
             // signup
             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 {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['screen_name'], 'password' => $password, 'role' => 'user', 'email' => $attributes['email']]);
                 if (!empty($attributes['photo'])) {
                     $file = file_get_contents($attributes['photo']);
                     $avatarPath = \app\modules\user\Module::$avatarPath;
                     file_put_contents($avatarPath . '/1.jpg', $file);
                 }
                 $profile = new Profile(['firstname' => $attributes['first_name'], 'lastname' => $attributes['last_name'], 'country' => $attributes['country']]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     // Сохраняем профиль
                     $profile->user_id = $user->id;
                     if (!$profile->save()) {
                         print_r($profile->getErrors());
                         die;
                     }
                     $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);
                     } else {
                         print_r($auth->getErrors());
                     }
                 } else {
                     print_r($user->getErrors());
                 }
             }
         }
     } 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();
         }
     }
 }
Exemplo n.º 2
0
 public function actionMassdelete()
 {
     if (($ids = Yii::$app->request->post('selection')) !== null) {
         $models = $this->findModel(Yii::$app->request->post('selection'));
         foreach ($models as $model) {
             $model->delete();
         }
         $models = Profile::findAll(Yii::$app->request->post('selection'));
         foreach ($models as $model) {
             $model->delete();
         }
         $models = Auth::findAll(Yii::$app->request->post('selection'));
         foreach ($models as $model) {
             $model->delete();
         }
         return $this->redirect(['index']);
     } else {
         throw new HttpException(400);
     }
 }