Example #1
0
 public function actionLogin()
 {
     $this->layout = '//layouts/inner';
     $this->showSearchForm = false;
     $model = new LoginForm();
     if (Yii::app()->request->getQuery('soc_error_save')) {
         Yii::app()->user->setFlash('error', tt('Error saving data. Please try again later.', 'socialauth'));
     }
     if (Yii::app()->request->getQuery('deactivate')) {
         showMessage(tc('Login'), tt('Your account not active. Administrator deactivate your account.', 'socialauth'), null, true);
     }
     $service = Yii::app()->request->getQuery('service');
     if (isset($service)) {
         $authIdentity = Yii::app()->eauth->getIdentity($service);
         $authIdentity->redirectUrl = Yii::app()->user->returnUrl;
         $authIdentity->cancelUrl = $this->createAbsoluteUrl('site/login');
         if ($authIdentity->authenticate()) {
             $identity = new EAuthUserIdentity($authIdentity);
             // успешная авторизация
             if ($identity->authenticate()) {
                 //Yii::app()->user->login($identity);
                 $uid = $identity->id;
                 $firstName = $identity->firstName;
                 $email = $identity->email;
                 $service = $identity->serviceName;
                 $mobilePhone = $identity->mobilePhone;
                 $homePhone = $identity->homePhone;
                 $isNewUser = false;
                 $existId = User::getIdByUid($uid, $service);
                 if (!$existId) {
                     $isNewUser = true;
                     $email = !$email ? User::getRandomEmail() : $email;
                     $phone = '';
                     if ($mobilePhone) {
                         $phone = $mobilePhone;
                     } elseif ($homePhone) {
                         $phone = $homePhone;
                     }
                     $user = User::createUser(array('email' => $email, 'phone' => $phone, 'username' => $firstName), true);
                     if (!$user && isset($user['id'])) {
                         $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?soc_error_save=1');
                     }
                     $success = User::setSocialUid($user['id'], $uid, $service);
                     if (!$success) {
                         User::model()->findByPk($user['id'])->delete();
                         $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?soc_error_save=1');
                     }
                     $existId = User::getIdByUid($uid, $service);
                 }
                 if ($existId) {
                     $result = $model->loginSocial($existId);
                     User::updateUserSession();
                     User::updateLatestInfo(Yii::app()->user->id, Yii::app()->controller->currentUserIp);
                     if ($result) {
                         //						Yii::app()->user->clearState('id');
                         //						Yii::app()->user->clearState('first_name');
                         //						Yii::app()->user->clearState('nickname');
                         if ($result === 'deactivate') {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/site/login') . '?deactivate=1');
                         }
                         if ($isNewUser) {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/usercpanel/main/index') . '?soc_success=1');
                         } else {
                             $authIdentity->redirect(Yii::app()->createAbsoluteUrl('/usercpanel/main/index'));
                         }
                     }
                 }
                 // специальное перенаправления для корректного закрытия всплывающего окна
                 $authIdentity->redirect();
             } else {
                 // закрытие всплывающего окна и перенаправление на cancelUrl
                 $authIdentity->cancel();
             }
         }
         // авторизация не удалась, перенаправляем на страницу входа
         $this->redirect(array('site/login'));
     }
     if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
         $model->scenario = 'withCaptcha';
     }
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
         echo CActiveForm::validate($model);
         Yii::app()->end();
     }
     if (isset($_POST['LoginForm'])) {
         $model->attributes = $_POST['LoginForm'];
         if ($model->validate() && $model->login()) {
             Yii::app()->user->setState('attempts-login', 0);
             User::updateUserSession();
             User::updateLatestInfo(Yii::app()->user->id, Yii::app()->controller->currentUserIp);
             if (Yii::app()->user->checkAccess('apartments_admin')) {
                 NewsProduct::getProductNews();
                 $this->redirect(array('/apartments/backend/main/admin'));
                 Yii::app()->end();
             }
             /*if (Yii::app()->user->isGuest) {
             			$this->redirect(Yii::app()->user->returnUrl);
             		} else {
             			if (!Yii::app()->user->getState('returnedUrl')) {
             				$this->redirect(array('/usercpanel/main/index'));
             			} else {
             				$this->redirect(Yii::app()->user->getState('returnedUrl'));
             			}
             		}*/
             if (!Yii::app()->user->returnUrl) {
                 $this->redirect(array('/usercpanel/main/index'));
             } else {
                 $this->redirect(Yii::app()->user->returnUrl);
             }
         } else {
             Yii::app()->user->setState('attempts-login', Yii::app()->user->getState('attempts-login', 0) + 1);
             if (Yii::app()->user->getState('attempts-login') >= LoginForm::ATTEMPTSLOGIN) {
                 $model->scenario = 'withCaptcha';
             }
         }
     }
     $this->render('login', array('model' => $model));
 }
Example #2
0
 public function login($model)
 {
     //проверяем вход через социальные сервисы
     $service = Yii::app()->request->getQuery('service');
     if (isset($service) && $service) {
         $authIdentity = Yii::app()->eauth->getIdentity($service);
         $authIdentity->redirectUrl = user()->getReturnUrl(bu());
         $authIdentity->cancelUrl = $this->createAbsoluteUrl('/auth/login');
         if ($authIdentity->authenticate()) {
             $identity = new EAuthUserIdentity($authIdentity);
             // successful authentication
             if ($identity->authenticate()) {
                 Yii::app()->user->login($identity);
                 app()->shoppingCart->saveToCart();
                 app()->shoppingCart->clear();
                 app()->shoppingCart->getFromDb();
                 // cookie save db and remove cookie
                 $this->deleteCompareCookiesAfterLogin();
                 $this->deleteCookieFavoriteProducts();
                 // special redirect with closing popup window
                 $authIdentity->redirect();
                 return true;
             } else {
                 // close popup window and redirect to cancelUrl
                 $authIdentity->cancel();
                 return false;
             }
         }
     } else {
         $model->attributes = $_POST['LoginForm'];
         // Проверяем правильность данных
         $ui = user()->frontUserIdentity;
         $identity = new $ui($model->username, $model->password);
         $identity->authenticate();
         if ($identity->errorCode == CBaseUserIdentity::ERROR_NONE) {
             // Данная строчка говорит что надо выдать пользователю
             // соответствующие куки о том что он зарегистрирован, срок действий
             // у которых указан вторым параметром.
             $duration = $model->rememberMe ? 3600 * 24 * 30 : 0;
             // 30 days
             user()->login($identity, $duration);
             app()->shoppingCart->saveToCart();
             app()->shoppingCart->clear();
             app()->shoppingCart->getFromDb();
             // dump(user()->isGuest);
             // cookie save db and remove cookie
             $this->deleteCompareCookiesAfterLogin();
             $this->deleteCookieFavoriteProducts();
             return true;
         }
     }
 }