Add RememberMe feature and authentications history

This commit is contained in:
Frédéric Guillot
2014-04-19 22:12:12 -04:00
parent 5aacb6a763
commit a04ecbde77
24 changed files with 949 additions and 55 deletions

View File

@@ -7,7 +7,7 @@ require_once __DIR__.'/base.php';
/**
* Automatic actions management
*
* @package controllers
* @package controller
* @author Frederic Guillot
*/
class Action extends Base

View File

@@ -4,8 +4,19 @@ namespace Controller;
require_once __DIR__.'/base.php';
/**
* Application controller
*
* @package controller
* @author Frederic Guillot
*/
class App extends Base
{
/**
* Redirect to the project creation page or the board controller
*
* @access public
*/
public function index()
{
if ($this->project->countByStatus(\Model\Project::ACTIVE)) {

View File

@@ -26,6 +26,8 @@ abstract class Base
$this->task = $registry->task;
$this->user = $registry->user;
$this->comment = $registry->comment;
$this->rememberMe = $registry->rememberMe;
$this->lastLogin = $registry->lastLogin;
$this->event = $registry->shared('event');
}
@@ -37,7 +39,7 @@ abstract class Base
public function beforeAction($controller, $action)
{
// Start the session
$this->session->open(dirname($_SERVER['PHP_SELF']), SESSION_SAVE_PATH);
$this->session->open(BASE_URL_DIRECTORY, SESSION_SAVE_PATH);
// HTTP secure headers
$this->response->csp();
@@ -53,9 +55,27 @@ abstract class Base
// Set timezone
date_default_timezone_set($this->config->get('timezone', 'UTC'));
// If the user is not authenticated redirect to the login form, if the action is public continue
if (! isset($_SESSION['user']) && ! $this->acl->isPublicAction($controller, $action)) {
$this->response->redirect('?controller=user&action=login');
// Authentication
if (! $this->acl->isLogged() && ! $this->acl->isPublicAction($controller, $action)) {
// Try the remember me authentication first
if (! $this->rememberMe->authenticate()) {
// Redirect to the login form if not authenticated
$this->response->redirect('?controller=user&action=login');
}
else {
$this->lastLogin->create(
\Model\LastLogin::AUTH_REMEMBER_ME,
$this->acl->getUserId(),
$this->user->getIpAddress(),
$this->user->getUserAgent()
);
}
}
else if ($this->rememberMe->hasCookie()) {
$this->rememberMe->refresh();
}
// Check if the user is allowed to see this page

View File

@@ -28,7 +28,9 @@ class Config extends Base
'errors' => array(),
'menu' => 'config',
'title' => t('Settings'),
'timezones' => $this->config->getTimezones()
'timezones' => $this->config->getTimezones(),
'remember_me_sessions' => $this->rememberMe->getAll($this->acl->getUserId()),
'last_logins' => $this->lastLogin->getAll($this->acl->getUserId()),
)));
}
@@ -63,7 +65,9 @@ class Config extends Base
'errors' => $errors,
'menu' => 'config',
'title' => t('Settings'),
'timezones' => $this->config->getTimezones()
'timezones' => $this->config->getTimezones(),
'remember_me_sessions' => $this->rememberMe->getAll($this->acl->getUserId()),
'last_logins' => $this->lastLogin->getAll($this->acl->getUserId()),
)));
}
@@ -101,4 +105,15 @@ class Config extends Base
$this->session->flash(t('All tokens have been regenerated.'));
$this->response->redirect('?controller=config');
}
/**
* Remove a "RememberMe" token
*
* @access public
*/
public function removeRememberMeToken()
{
$this->rememberMe->remove($this->request->getIntegerParam('id'));
$this->response->redirect('?controller=config&action=index#remember-me');
}
}

View File

@@ -4,9 +4,19 @@ namespace Controller;
require_once __DIR__.'/base.php';
/**
* Project controller
*
* @package controller
* @author Frederic Guillot
*/
class Project extends Base
{
// Display access forbidden page
/**
* Display access forbidden page
*
* @access public
*/
public function forbidden()
{
$this->response->html($this->template->layout('project_forbidden', array(
@@ -15,7 +25,11 @@ class Project extends Base
)));
}
// List of completed tasks for a given project
/**
* List of completed tasks for a given project
*
* @access public
*/
public function tasks()
{
$project_id = $this->request->getIntegerParam('project_id');
@@ -40,7 +54,11 @@ class Project extends Base
)));
}
// List of projects
/**
* List of projects
*
* @access public
*/
public function index()
{
$projects = $this->project->getAll(true, $this->acl->isRegularUser());
@@ -54,7 +72,11 @@ class Project extends Base
)));
}
// Display a form to create a new project
/**
* Display a form to create a new project
*
* @access public
*/
public function create()
{
$this->response->html($this->template->layout('project_new', array(
@@ -65,7 +87,11 @@ class Project extends Base
)));
}
// Validate and save a new project
/**
* Validate and save a new project
*
* @access public
*/
public function save()
{
$values = $this->request->getValues();
@@ -90,7 +116,11 @@ class Project extends Base
)));
}
// Display a form to edit a project
/**
* Display a form to edit a project
*
* @access public
*/
public function edit()
{
$project = $this->project->getById($this->request->getIntegerParam('project_id'));
@@ -108,7 +138,11 @@ class Project extends Base
)));
}
// Validate and update a project
/**
* Validate and update a project
*
* @access public
*/
public function update()
{
$values = $this->request->getValues() + array('is_active' => 0);
@@ -133,7 +167,11 @@ class Project extends Base
)));
}
// Confirmation dialog before to remove a project
/**
* Confirmation dialog before to remove a project
*
* @access public
*/
public function confirm()
{
$project = $this->project->getById($this->request->getIntegerParam('project_id'));
@@ -150,7 +188,11 @@ class Project extends Base
)));
}
// Remove a project
/**
* Remove a project
*
* @access public
*/
public function remove()
{
$project_id = $this->request->getIntegerParam('project_id');
@@ -164,7 +206,11 @@ class Project extends Base
$this->response->redirect('?controller=project');
}
// Enable a project
/**
* Enable a project
*
* @access public
*/
public function enable()
{
$project_id = $this->request->getIntegerParam('project_id');
@@ -178,7 +224,11 @@ class Project extends Base
$this->response->redirect('?controller=project');
}
// Disable a project
/**
* Disable a project
*
* @access public
*/
public function disable()
{
$project_id = $this->request->getIntegerParam('project_id');
@@ -192,7 +242,11 @@ class Project extends Base
$this->response->redirect('?controller=project');
}
// Users list for the selected project
/**
* Users list for the selected project
*
* @access public
*/
public function users()
{
$project = $this->project->getById($this->request->getIntegerParam('project_id'));
@@ -210,7 +264,11 @@ class Project extends Base
)));
}
// Allow a specific user for the selected project
/**
* Allow a specific user for the selected project
*
* @access public
*/
public function allow()
{
$values = $this->request->getValues();
@@ -229,7 +287,11 @@ class Project extends Base
$this->response->redirect('?controller=project&action=users&project_id='.$values['project_id']);
}
// Revoke user access
/**
* Revoke user access
*
* @access public
*/
public function revoke()
{
$values = array(

View File

@@ -32,6 +32,7 @@ class User extends Base
*/
public function logout()
{
$this->rememberMe->destroy($this->acl->getUserId());
$this->session->close();
$this->response->redirect('?controller=user&action=login');
}
@@ -63,7 +64,17 @@ class User extends Base
$values = $this->request->getValues();
list($valid, $errors) = $this->user->validateLogin($values);
if ($valid) $this->response->redirect('?controller=app');
if ($valid) {
$this->lastLogin->create(
\Model\LastLogin::AUTH_DATABASE,
$this->acl->getUserId(),
$this->user->getIpAddress(),
$this->user->getUserAgent()
);
$this->response->redirect('?controller=app');
}
$this->response->html($this->template->layout('user_login', array(
'errors' => $errors,