示例#1
0
 public function onAuthSuccess($client)
 {
     /* @var $client \yii\authclient\OAuth2*/
     /* @var $user \common\models\User */
     $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) {
             // вход
             $user = $auth->user;
             if ($user->status == User::STATUS_NOT_ACTIVE && $user->email == '') {
                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter the phone number and confirm the e-mail address.")]);
                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
             } elseif ($user->status == User::STATUS_NOT_ACTIVE && $user->email != '') {
                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter a phone number.")]);
                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
             } elseif ($user->status == User::STATUS_DELETED) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "This user is blocked.")]);
                 return $this->redirectUser($url = Url::to(['/ad/view/all']));
             }
             Yii::$app->user->login($user);
         } else {
             // регистрация
             if (isset($attributes['email']) && ($user = User::findOne(['email' => $attributes['email']]))) {
                 // Если пользователь регитрировался ранее через форму регистации.
                 if ($user) {
                     if ($user->status == User::STATUS_DELETED) {
                         Yii::$app->getSession()->setFlash('error', Yii::t('app', "User <strong> {email} </strong> blocked.", ['email' => $user->email]));
                     } elseif ($user->auths->source) {
                         Yii::$app->getSession()->setFlash('error', [Yii::t('app', "Authorization using the email address <strong> {email} </strong> is already happening through the account <strong> {auths} </strong>.\n                            Log on using the account <strong> {auths} </strong> or use the link <strong> Forgot your password? </strong> for email <strong> {email} </strong> to restore the password..", ['email' => $user->email, 'auths' => $user->auths->source])]);
                     } else {
                         Yii::$app->getSession()->setFlash('error', Yii::t('app', "Authorization using the email address <strong> {email} </strong> has successfully passed through the registration form. Click on the link <strong> Forgot your password? </strong> to restore the password.", ['email' => $user->email]));
                     }
                 }
                 return $this->redirectUser($url = Url::to(['/main/login']));
             } else {
                 // Полученные данные заносим в переменные
                 /* @var $email string */
                 /* @var $first_name string */
                 /* @var $last_name string */
                 if (Yii::$app->request->get('authclient') == 'google') {
                     $first_name = $attributes['name']['givenName'];
                     $last_name = $attributes['name']['familyName'];
                     $email = $attributes['emails'][0]['value'];
                 } elseif (Yii::$app->request->get('authclient') == 'yandex') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = $attributes['default_email'];
                 } elseif (Yii::$app->request->get('authclient') == 'facebook') {
                     $names = explode(' ', $attributes['name']);
                     $first_name = $names[0];
                     $last_name = $names[1];
                     $email = $attributes['email'];
                 } elseif (Yii::$app->request->get('authclient') == 'vkontakte') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = false;
                 } elseif (Yii::$app->request->get('authclient') == 'twitter') {
                     $names = $attributes['name'];
                     $names = explode(' ', $names);
                     $first_name = $names[0];
                     $last_name = $names[1];
                     $email = false;
                 } elseif (Yii::$app->request->get('authclient') == 'linkedin') {
                     $first_name = $attributes['first_name'];
                     $last_name = $attributes['last_name'];
                     $email = $attributes['email'];
                 }
                 $password = Yii::$app->security->generateRandomString(6);
                 if ($email == false) {
                     $email = '';
                 }
                 $user = new User(['email' => $email, 'password' => $password, 'status' => User::STATUS_NOT_ACTIVE, 'country_id' => 182]);
                 $user->generateAuthKey();
                 $user->generateSecretKey();
                 $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()) {
                         /* @var $modelProfile /common/models/UserProfile */
                         $modelProfile = new UserProfile();
                         $modelProfile->user_id = $user->id;
                         $modelProfile->first_name = $first_name;
                         $modelProfile->last_name = $last_name;
                         if ($modelProfile->save()) {
                             if (RbacHelper::assignRole($user->id)) {
                                 $modelUserPrivilege = new UserPrivilege();
                                 $modelUserPrivilege->link('user', $user);
                                 $transaction->commit();
                             }
                             // если нет емайл, делаем перенаправление на main/finish-reg
                             if ($email == false) {
                                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter the phone number and confirm the e-mail address.")]);
                                 return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
                             }
                             Yii::$app->getSession()->setFlash('success', [Yii::t('app', "To complete registration, enter a phone number.")]);
                             return $this->redirectUser($url = Url::to(['/main/finish-reg', 'id' => $user->id]));
                         }
                     } else {
                         d($auth->getErrors());
                     }
                 } else {
                     /* @var $user \common\models\User */
                     $user = User::findOne(['email' => $user->email]);
                     // Если пользователь регитрировался ранее через форму регистации.
                     if ($user) {
                         if ($user->status == User::STATUS_DELETED) {
                             Yii::$app->getSession()->setFlash('error', Yii::t('app', "User <strong> {email} </strong> blocked.", ['email' => $user->email]));
                         } elseif ($user->auths->source) {
                             Yii::$app->getSession()->setFlash('error', [Yii::t('app', "Authorization using the email address <strong> {email} </strong> is already happening through the account <strong> {auths} </strong>.\n                            Log on using the account <strong> {auths} </strong> or use the link <strong> Forgot your password? </strong> for email <strong> {email} </strong> to restore the password..", ['email' => $user->email, 'auths' => $user->auths->source])]);
                         } else {
                             Yii::$app->getSession()->setFlash('error', Yii::t('app', "Authorization using the email address <strong> {email} </strong> has successfully passed through the registration form. Click on the link <strong> Forgot your password? </strong> to restore the password.", ['email' => $user->email]));
                         }
                     }
                     return $this->redirectUser($url = Url::to(['/main/login']));
                 }
             }
         }
     } 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();
         }
     }
     return true;
 }
示例#2
0
 public function reg()
 {
     if ($this->validate()) {
         $modelUser = new User();
         $modelUser->phone = $this->getPhoneNumber();
         $modelUser->email = $this->email;
         $modelUser->status = $this->status;
         $modelUser->country_id = $this->country_id;
         $modelUser->setPassword($this->password);
         $modelUser->generateAuthKey();
         if ($this->scenario === 'emailActivation') {
             $modelUser->generateSecretKey();
         }
         if ($modelUser->save()) {
             $modelUserProfile = new UserProfile();
             $modelUserProfile->link('user', $modelUser);
             $modelUserPrivilege = new UserPrivilege();
             $modelUserPrivilege->link('user', $modelUser);
             return RbacHelper::assignRole($modelUser->getId()) ? $modelUser : null;
         }
     }
     return false;
 }