/**
  * Authenticates a user.
  * The example implementation makes sure if the username and password
  * are both 'demo'.
  * In practical applications, this should be changed to authenticate
  * against some persistent user identity storage (e.g. database).
  * @return boolean whether authentication succeeds.
  */
 public function authenticate()
 {
     $user = User::model()->with(array('userGroup'))->find('username = ? AND password = ?', array($this->username, $this->password));
     if ($user === null) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
     } else {
         $this->user_id = $user->user_id;
         $this->username = $user->username;
         //save date time when login
         $user->last_login = date('Y-m-d H:i:s');
         $user->save();
         $auth = Yii::app()->authManager;
         if (!$auth->isAssigned($user->userGroup->role, $this->user_id)) {
             if ($auth->assign($user->userGroup->role, $this->user_id)) {
                 Yii::app()->authManager->save();
             }
         }
         // Save log for login
         $log = new LogLogin();
         $log->user_id = $user->user_id;
         $log->login_time = $user->last_login;
         $log->ip = $_SERVER['REMOTE_ADDR'];
         $log->browser = $_SERVER['HTTP_USER_AGENT'];
         $log->save();
         //////////////////////
         $this->errorCode = self::ERROR_NONE;
     }
     return $this->errorCode == self::ERROR_NONE;
 }
 /**
  * Logs out the current user and redirect to homepage.
  */
 public function actionLogout()
 {
     //print '<pre>';
     //print_r(Yii::app()->user->getId());exit;
     $current_user_id = Yii::app()->user->getId();
     Yii::app()->user->logout();
     // Save log for login
     $log = LogLogin::model()->find('user_id = ? ORDER BY login_time DESC', array($current_user_id));
     //$log = new LogLogin();
     $log->logout_time = date('Y-m-d H:i:s');
     $log->save();
     //////////////////////
     $this->redirect(Yii::app()->homeUrl);
 }