Esempio n. 1
0
 /**
  * Logs a user in.
  *
  * @param   string   $user : user email
  * @param   string   password
  * @param   boolean  enable autologin
  * @return  boolean
  */
 protected function _login($user, $password, $remember)
 {
     if (!is_object($user)) {
         $q = Doctrine_Query::create()->from('User u')->innerJoin('u.Roles r')->addWhere('u.email=?', $user)->addWhere('u.password=?', $password);
     }
     //die($q->getSqlQuery());
     $user = $q->fetchOne();
     //die(print_r($user->toArray()));
     if ($user && $this->_is_in_db('login', $user->Roles, 'name')) {
         if ($remember === TRUE) {
             // Create a new autologin token
             //$token = ORM::factory('user_token');
             $token = new UserToken();
             // Set token data
             $token->user_id = $user->id;
             $token->expires = time() + $this->_config['lifetime'];
             $token->token = Text::random('alnum', 32);
             $token->created = time();
             $token->user_agent = sha1(Request::$user_agent);
             $token->save();
             $user->UserToken[] = $token;
             // Set the autologin cookie
             Cookie::set('authautologin', $token->token, $this->_config['lifetime']);
         }
         //update lastlogin
         $user->logins++;
         $user->last_login = time();
         $user->save();
         // Finish the login
         $this->complete_login($user);
         return TRUE;
     }
     // Login failed
     return FALSE;
 }
 public static function setCookieToken(User $user, $salt)
 {
     $instance = new UserToken();
     $instance->sid = $salt;
     $instance->uid = $user->id;
     $instance->save(true);
     $cookie_val = sprintf('%s_%s_%s', $instance->uid, $instance->sid, $instance->token);
     $context = Context::getInstance();
     $context->session->set_cookie('frmauth', $cookie_val, time() + 60 * 60 * 24 * 30);
 }
Esempio n. 3
0
 public function activate(UserToken $token, $invalidate = true)
 {
     $token->status = UserToken::STATUS_ACTIVATE;
     if ($token->save()) {
         if ($invalidate) {
             UserToken::model()->deleteAll('id != :id AND user_id = :user_id AND type = :type', array(':user_id' => $token->user_id, ':type' => $token->type, ':id' => $token->id));
         }
         return true;
     }
     throw new CDbException(Yii::t('UserModule.user', 'Error activate token!'));
 }
Esempio n. 4
0
 public function actionCreate()
 {
     $model = new UserToken();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (($data = Yii::app()->getRequest()->getPost('UserToken')) !== null) {
         $model->setAttributes($data);
         if ($model->save()) {
             Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'New record was created!'));
             $this->redirect((array) Yii::app()->getRequest()->getPost('submit-type', array('create')));
         }
     }
     $this->render('create', array('model' => $model));
 }