public function actionLoginHandler()
 {
     $model = new LoginForm();
     if (isPostOrAjaxRequest()) {
         if (isset($_POST['LoginForm'])) {
             $model->attributes = $_POST['LoginForm'];
             if ($model->validate()) {
                 if (!$model->isUserBanned()) {
                     $model->login();
                 } else {
                     $response['redirect'] = $this->createUrl('banned');
                     $this->successfulAjaxResponse($response);
                 }
             }
             if (isAjax()) {
                 if ($model->hasErrors()) {
                     $this->validationErrorsAjaxResponse($model, FALSE);
                 } else {
                     $response['redirect'] = Yii::app()->getRequest()->getUrlReferrer();
                     $this->successfulAjaxResponse($response);
                 }
             }
         }
     }
 }
 public function login(LoginForm $form, IWebUser $user, CHttpRequest $request = null)
 {
     if ($form->hasErrors()) {
         return false;
     }
     $identity = new UserIdentity($form->email, $form->password);
     $duration = 0;
     //if ($form->remember_me) {
     $sessionTimeInWeeks = (int) Yii::app()->getModule('user')->sessionLifeTime;
     $duration = $sessionTimeInWeeks * 24 * 60 * 60;
     //}
     if ($identity->authenticate()) {
         $user->login($identity, $duration);
         Yii::log(Yii::t('UserModule.user', 'User with {email} was logined with IP-address {ip}!', array('{email}' => $form->email, '{ip}' => $request->getUserHostAddress())), CLogger::LEVEL_INFO, UserModule::$logCategory);
         return true;
     }
     Yii::log(Yii::t('UserModule.user', 'Authorization error with IP-address {ip}! email => {email}, Password => {password}!', array('{email}' => $form->email, '{password}' => $form->password, '{ip}' => $request->getUserHostAddress())), CLogger::LEVEL_ERROR, UserModule::$logCategory);
     return false;
 }
 public function login(LoginForm $form, IWebUser $user, CHttpRequest $request = null)
 {
     if ($form->hasErrors()) {
         Yii::app()->eventManager->fire(UserEvents::FAILURE_LOGIN, new UserLoginEvent($form, $user));
         return false;
     }
     $identity = new UserIdentity($form->email, $form->password);
     //        CVarDumper::dump($identity->authenticate(),10,10);exit;
     $duration = 0;
     if ($form->remember_me) {
         $sessionTimeInWeeks = (int) Yii::app()->getModule('user')->sessionLifeTime;
         $duration = $sessionTimeInWeeks * 24 * 60 * 60;
     }
     if ($identity->authenticate()) {
         Yii::app()->eventManager->fire(UserEvents::BEFORE_LOGIN, new UserLoginEvent($form, $user, $identity));
         $user->login($identity, $duration);
         Yii::log(Yii::t('UserModule.user', 'User with {email} was logined with IP-address {ip}!', ['{email}' => $form->email, '{ip}' => $request->getUserHostAddress()]), CLogger::LEVEL_INFO, UserModule::$logCategory);
         Yii::app()->eventManager->fire(UserEvents::SUCCESS_LOGIN, new UserLoginEvent($form, $user, $identity));
         return true;
     }
     Yii::app()->eventManager->fire(UserEvents::FAILURE_LOGIN, new UserLoginEvent($form, $user, $identity));
     Yii::log(Yii::t('UserModule.user', 'Authorization error with IP-address {ip}! email => {email}, Password => {password}!', ['{email}' => $form->email, '{password}' => $form->password, '{ip}' => $request->getUserHostAddress()]), CLogger::LEVEL_ERROR, UserModule::$logCategory);
     return false;
 }
Пример #4
0
 public function actionLogin()
 {
     $this->layout = '//layouts/login';
     // echo var_dump(Session::getOnlineUsers());
     if (Yii::app()->user->isInitialized && !Yii::app()->user->isGuest) {
         $this->redirect(Yii::app()->homeUrl);
         return;
     }
     $model = new LoginForm();
     $model->useCaptcha = false;
     // collect user input data
     if (isset($_POST['LoginForm'])) {
         $model->attributes = $_POST['LoginForm'];
         $ip = $this->getRealIp();
         x2base::cleanUpSessions();
         $session = CActiveRecord::model('Session')->findByAttributes(array('user' => $model->username, 'IP' => $ip));
         if (isset($session)) {
             $session->lastUpdated = time();
             if ($session->status < 1) {
                 if ($session->status > -3) {
                     $session->status -= 1;
                 }
             } else {
                 $session->status = -1;
             }
             if ($session->status < -1) {
                 $model->useCaptcha = true;
             }
             if ($session->status < -2) {
                 $model->setScenario('loginWithCaptcha');
             }
         } else {
             $session = new Session();
             $session->user = $model->username;
             $session->lastUpdated = time();
             $session->status = 1;
             $session->IP = $ip;
         }
         if ($model->validate() && $model->login()) {
             $user = User::model()->findByPk(Yii::app()->user->getId());
             $user->login = time();
             $user->save();
             if ($user->username == 'admin') {
                 if (ini_get('allow_url_fopen') == 1) {
                     $context = stream_context_create(array('http' => array('timeout' => 2)));
                     $updateSources = array('http://x2planet.com/updates/versionCheck.php', 'http://x2base.com/updates/versionCheck.php');
                     $newVersion = '';
                     foreach ($updateSources as $url) {
                         $sourceVersion = @file_get_contents($url, 0, $context);
                         if ($sourceVersion !== false) {
                             $newVersion = $sourceVersion;
                             break;
                         }
                     }
                     if (empty($newVersion)) {
                         $newVersion = Yii::app()->params->version;
                     }
                     /* 
                     						// check X2Planet for updates
                     						$x2planetVersion = @file_get_contents('http://x2planet.com/updates/versionCheck.php',0,$context);
                     						if($x2planetVersion !== false)
                     							$newVersion = $x2planetVersion;
                     						else {
                     							// try X2Base if that didn't work
                     							$x2baseVersion = @file_get_contents('http://x2base.com/updates/versionCheck.php',0,$context);
                     							if($x2baseVersion !== false)
                     								$newVersion=$x2baseVersion;
                     							else
                     								$newVersion=Yii::app()->params->version;
                     						} */
                     if (version_compare($newVersion, Yii::app()->params->version) > 0) {
                         // if the latest version is newer than our version
                         Yii::app()->session['versionCheck'] = false;
                         Yii::app()->session['newVersion'] = $newVersion;
                     } else {
                         Yii::app()->session['versionCheck'] = true;
                     }
                 } else {
                     Yii::app()->session['versionCheck'] = true;
                 }
             } else {
                 Yii::app()->session['versionCheck'] = true;
             }
             Yii::app()->session['loginTime'] = time();
             $session->status = 1;
             $session->save();
             if (Yii::app()->user->returnUrl == 'site/index') {
                 $this->redirect('index');
             } else {
                 $this->redirect(Yii::app()->user->returnUrl);
             }
         } else {
             $session->save();
             $model->verifyCode = '';
             if ($model->hasErrors()) {
                 $model->addError('username', Yii::t('app', 'Incorrect username or password.'));
             }
             $model->addError('password', Yii::t('app', 'Incorrect username or password.'));
         }
     }
     // display the login form
     $this->render('login', array('model' => $model));
 }