/**
  * Logs out the current user and redirect to homepage.
  */
 public function actionLogout()
 {
     $UsuarioLog = new UsuarioLog();
     $usuarioLogado = Yii::app()->user->objeto->id;
     $UsuarioLog->usuario_id = $usuarioLogado;
     $UsuarioLog->descricao = "Efetuou logout";
     $UsuarioLog->save(false);
     $online = Usuario::model()->findByPk($usuarioLogado);
     $online->online = 0;
     $online->save();
     Yii::app()->user->logout();
     $this->redirect(Yii::app()->homeUrl);
 }
 /**
  * Displays the login page
  */
 public function actionLogin()
 {
     $model = new LoginForm();
     // if it is ajax validation request
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'login-form') {
         echo CActiveForm::validate($model);
         Yii::app()->end();
     }
     $modal = false;
     // collect user input data
     if (isset($_POST['LoginForm'])) {
         $model->attributes = $_POST['LoginForm'];
         $modal = $model->modal;
         if ($modal) {
             $this->layout = '//layouts/column2';
         }
         // validate user input and redirect to the previous page if valid
         if ($model->validate() && $model->login()) {
             if (!$modal) {
                 $usuario_log = new UsuarioLog();
                 $usuario_log->usuario = $model->username;
                 $usuario_log->ip = $this->getClientIP();
                 $usuario_log->inicio = new CDbExpression('NOW()');
                 $usuario_log->user_agent = $_SERVER['HTTP_USER_AGENT'];
                 $usuario_log->save();
                 $this->redirect(Yii::app()->user->returnUrl);
             } else {
                 echo json_encode(array('id' => 1, 'success' => true, 'msg' => 'Login correcto'));
                 return;
             }
         }
     }
     if (isset($_POST['modal'])) {
         $this->layout = '//layouts/column2';
         $modal = true;
     }
     // display the login form
     $this->render('login', array('model' => $model, 'modal' => $modal));
 }