示例#1
0
 /**
  * Test action
  */
 public function actionlogin()
 {
     $identity = new InternalIdentity('admin', '');
     $identity->authenticate();
     Yii::app()->user->login($identity, time() + 100000);
     $this->render('test');
 }
 /**
  * @return null on success error on failure
  */
 public function authenticate()
 {
     $identity = new InternalIdentity($this->email, $this->password);
     if ($identity->authenticate()) {
         // Member authenticated
         return true;
     } else {
         $this->addError('password', $identity->errorMessage);
     }
 }
 /**
  * Facebook login page
  */
 public function actionFacebookLogin()
 {
     // Load facebook
     Yii::import('ext.facebook.facebookLib');
     $facebook = new facebookLib(array('appId' => Yii::app()->params['facebookappid'], 'secret' => Yii::app()->params['facebookapisecret'], 'cookie' => true, 'disableSSLCheck' => false));
     facebookLib::$CURL_OPTS[CURLOPT_CAINFO] = Yii::getPathOfAlias('ext.facebook') . '/ca-bundle.crt';
     // Do we have an access token?
     if (($session = $facebook->getSession()) || isset($_GET['session']) && $_GET['session']) {
         $info = array('id' => 0, 'email' => '');
         $info = $facebook->getInfo(null, array('access_token' => $session['access_token']));
         // Did we submit the authenticate form?
         $facebookForm = new facebookForm();
         if (isset($_POST['facebookForm'])) {
             $facebookForm->attributes = $_POST['facebookForm'];
             if ($facebookForm->validate()) {
                 // Member authenticated
                 $identity = new InternalIdentity($facebookForm->email, $facebookForm->password);
                 if ($identity->authenticate()) {
                     // Member authenticated, Login
                     Yii::app()->user->login($identity, Yii::app()->params['loggedInDays'] * 60 * 60 * 24);
                 } else {
                     Yii::app()->user->setFlash('success', $identity->errorMessage);
                 }
                 // Update the fbuid and update the token
                 // We got through save the a new token
                 Members::model()->updateByPk($identity->getId(), array('fbuid' => $info['id'], 'fbtoken' => $session['access_token']));
                 // Login & redirect
                 Yii::app()->user->setFlash('success', Yii::t('login', 'Thank You. You are now logged in.'));
                 //$this->render('facebookdone', array( 'link' => $this->createUrl('/index', array( 'lang' => false ) ) ) );
                 $this->redirect('/index');
             }
         }
         // Did we submit the signup form?
         $facebookSignForm = new Members();
         if (isset($_POST['Members'])) {
             $facebookSignForm->attributes = $_POST['Members'];
             $facebookSignForm->role = 'member';
             $facebookSignForm->scenario = 'register';
             if ($facebookSignForm->save()) {
                 $identity = new InternalIdentity($facebookSignForm->email, $_POST['Members']['password']);
                 if ($identity->authenticate()) {
                     // Member authenticated, Login
                     Yii::app()->user->login($identity, Yii::app()->params['loggedInDays'] * 60 * 60 * 24);
                 } else {
                     Yii::app()->user->setFlash('success', $identity->errorMessage);
                 }
                 // Update the fbuid and update the token
                 // We got through save the a new token
                 Members::model()->updateByPk($facebookSignForm->id, array('fbuid' => $info['id'], 'fbtoken' => $session['access_token']));
                 // Login & redirect
                 Yii::app()->user->setFlash('success', Yii::t('login', 'Thank You. You are now logged in.'));
                 //$this->render('facebookdone', array( 'link' => $this->createUrl('/index', array( 'lang' => false ) ) ) );
                 $this->redirect('/index');
             }
         }
         // Authenticate
         $identity = new facebookIdentity($info['id'], $info['email']);
         $auth = $identity->authenticate();
         // What did we discover?
         if ($identity->errorCode == facebookIdentity::ERROR_UNKNOWN_IDENTITY) {
             // fbuid was not found in the DB
             Yii::app()->user->setFlash('attention', Yii::t('login', 'We could not find any user associated with that facebook account in our records.'));
         } else {
             if ($identity->errorCode == facebookIdentity::ERROR_USERNAME_INVALID) {
                 // Email addresses did not match
                 Yii::app()->user->setFlash('attention', Yii::t('login', 'We found a user account associated with your facebook account, But the email used there is different, Please complete the form below to login as that user.'));
             } else {
                 // We got through save the a new token
                 Yii::app()->user->login($identity, Yii::app()->params['loggedInDays'] * 60 * 60 * 24);
                 Members::model()->updateByPk($identity->getId(), array('fbtoken' => $session['access_token']));
                 Yii::app()->user->setFlash('success', Yii::t('login', 'Thank You. You are now logged in.'));
                 $this->render('facebookdone', array('link' => $this->createUrl('/index', array('lang' => false))));
                 //$this->redirect('/index');
             }
         }
         // Redirect if haven't done so
         if (!isset($_GET['facebookRedirected'])) {
             $_GET['facebookRedirected'] = 'true';
             $this->render('facebookdone', array('link' => $this->createUrl('/login/facebooklogin', array_merge($_GET, array('lang' => false)))));
         }
         // Default values
         $facebookForm->email = $facebookForm->email ? $facebookForm->email : $info['email'];
         $facebookSignForm->email = $facebookSignForm->email ? $facebookSignForm->email : $info['email'];
         $facebookSignForm->username = $facebookSignForm->username ? $facebookSignForm->username : $info['name'];
         $this->render('facebook_login', array('facebookSignForm' => $facebookSignForm, 'facebookForm' => $facebookForm, 'info' => $info));
     } else {
         $this->redirect('/login');
     }
 }