Beispiel #1
0
 public function testGetSessionUsername()
 {
     // Must be username
     $lf = new LoginForm();
     $lf->username = $this->user('testUser')->userAlias;
     $lf->password = '******';
     $this->assertEquals($this->user('testUser')->username, $lf->getSessionUsername());
 }
Beispiel #2
0
 /**
  * Log in using a Google account.
  */
 public function actionGoogleLogin()
 {
     $this->layout = '//layouts/login';
     $model = new LoginForm();
     $model->useCaptcha = false;
     // echo var_dump(Session::getOnlineUsers());
     if (Yii::app()->user->isInitialized && !Yii::app()->user->isGuest) {
         $this->redirect(Yii::app()->homeUrl);
         return;
     }
     require_once 'protected/components/GoogleAuthenticator.php';
     $auth = new GoogleAuthenticator();
     if (Yii::app()->settings->googleIntegration && ($token = $auth->getAccessToken())) {
         try {
             $user = $auth->getUserInfo($token);
             $email = filter_var($user->email, FILTER_SANITIZE_EMAIL);
             $profileRecord = X2Model::model('Profile')->findByAttributes(array('googleId' => $email));
             if (!isset($profileRecord)) {
                 $userRecord = X2Model::model('User')->findByAttributes(array('emailAddress' => $email));
                 $profileRecord = X2Model::model('Profile')->findByAttributes(array(), "emailAddress=:email OR googleId=:email", array(':email' => $email));
             }
             if (isset($userRecord) || isset($profileRecord)) {
                 if (!isset($profileRecord)) {
                     $profileRecord = X2Model::model('Profile')->findByPk($userRecord->id);
                 }
                 $auth->storeCredentials($profileRecord->id, $_SESSION['access_token']);
             }
             if (isset($userRecord) || isset($profileRecord)) {
                 if (!isset($userRecord)) {
                     $userRecord = User::model()->findByPk($profileRecord->id);
                 }
                 $username = $userRecord->username;
                 $password = $userRecord->password;
                 $model->username = $username;
                 $model->password = $password;
                 if ($model->login(true)) {
                     $ip = $this->getRealIp();
                     Session::cleanUpSessions();
                     if (isset($_SESSION['sessionId'])) {
                         $sessionId = $_SESSION['sessionId'];
                     } else {
                         $sessionId = $_SESSION['sessionId'] = session_id();
                     }
                     $session = X2Model::model('Session')->findByPk($sessionId);
                     // if this client has already tried to log in, increment their attempt count
                     if ($session === null) {
                         $session = new Session();
                         $session->id = $sessionId;
                         $session->user = $model->getSessionUsername();
                         $session->lastUpdated = time();
                         $session->status = 1;
                         $session->IP = $ip;
                     } else {
                         $session->lastUpdated = time();
                     }
                     // x2base::cleanUpSessions();
                     // $session = X2Model::model('Session')->findByAttributes(array('user'=>$userRecord->username,'IP'=>$ip));
                     // if(isset($session)) {
                     // $session->lastUpdated = time();
                     // } else {
                     // $session = new Session;
                     // $session->user = $model->username;
                     // $session->lastUpdated = time();
                     // $session->status = 1;
                     // $session->IP = $ip;
                     // }
                     $session->save();
                     SessionLog::logSession($userRecord->username, $sessionId, 'googleLogin');
                     $userRecord->login = time();
                     $userRecord->save();
                     Yii::app()->session['versionCheck'] = true;
                     Yii::app()->session['loginTime'] = time();
                     $session->status = 1;
                     if (Yii::app()->user->returnUrl == 'site/index') {
                         $this->redirect(array('/site/index'));
                     } else {
                         $this->redirect(Yii::app()->user->returnUrl);
                     }
                 }
             } else {
                 $this->render('googleLogin', array('failure' => 'email', 'email' => $email));
             }
         } catch (Google_AuthException $e) {
             $auth->flushCredentials();
             $auth->setErrors($e->getMessage());
             $this->render('googleLogin', array('failure' => $auth->getErrors()));
         } catch (NoUserIdException $e) {
             $auth->flushCredentials();
             $auth->setErrors($e->getMessage());
             $this->render('googleLogin', array('failure' => $auth->getErrors()));
         }
     } else {
         $this->render('googleLogin');
     }
 }