Skip to content

Commit

Permalink
Add auth controller
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Apr 11, 2015
1 parent ea9d402 commit 7df055a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 62 deletions.
67 changes: 67 additions & 0 deletions app/Controller/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Controller;

/**
* Authentication controller
*
* @package controller
* @author Frederic Guillot
*/
class Auth extends Base
{
/**
* Display the form login
*
* @access public
*/
public function login(array $values = array(), array $errors = array())
{
if ($this->userSession->isLogged()) {
$this->response->redirect($this->helper->url('app', 'index'));
}

$this->response->html($this->template->layout('auth/index', array(
'errors' => $errors,
'values' => $values,
'no_layout' => true,
'redirect_query' => $this->request->getStringParam('redirect_query'),
'title' => t('Login')
)));
}

/**
* Check credentials
*
* @access public
*/
public function check()
{
$redirect_query = $this->request->getStringParam('redirect_query');
$values = $this->request->getValues();
list($valid, $errors) = $this->authentication->validateForm($values);

if ($valid) {

if ($redirect_query !== '') {
$this->response->redirect('?'.urldecode($redirect_query));
}

$this->response->redirect($this->helper->url('app', 'index'));
}

$this->login($values, $errors);
}

/**
* Logout and destroy session
*
* @access public
*/
public function logout()
{
$this->authentication->backend('rememberMe')->destroy($this->userSession->getId());
$this->session->close();
$this->response->redirect($this->helper->url('auth', 'login'));
}
}
2 changes: 1 addition & 1 deletion app/Controller/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function handleAuthentication()
$this->response->text('Not Authorized', 401);
}

$this->response->redirect('?controller=user&action=login&redirect_query='.urlencode($this->request->getQueryString()));
$this->response->redirect($this->helper->url('auth', 'login', array('redirect_query' => urlencode($this->request->getQueryString()))));
}
}

Expand Down
60 changes: 2 additions & 58 deletions app/Controller/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,6 @@
*/
class User extends Base
{
/**
* Logout and destroy session
*
* @access public
*/
public function logout()
{
$this->checkCSRFParam();
$this->authentication->backend('rememberMe')->destroy($this->userSession->getId());
$this->session->close();
$this->response->redirect('?controller=user&action=login');
}

/**
* Display the form login
*
* @access public
*/
public function login(array $values = array(), array $errors = array())
{
if ($this->userSession->isLogged()) {
$this->response->redirect('?controller=app');
}

$this->response->html($this->template->layout('user/login', array(
'errors' => $errors,
'values' => $values,
'no_layout' => true,
'redirect_query' => $this->request->getStringParam('redirect_query'),
'title' => t('Login')
)));
}

/**
* Check credentials
*
* @access public
*/
public function check()
{
$redirect_query = $this->request->getStringParam('redirect_query');
$values = $this->request->getValues();
list($valid, $errors) = $this->authentication->validateForm($values);

if ($valid) {
if ($redirect_query !== '') {
$this->response->redirect('?'.urldecode($redirect_query));
}
else {
$this->response->redirect('?controller=app');
}
}

$this->login($values, $errors);
}

/**
* Common layout for user views
*
Expand Down Expand Up @@ -450,7 +394,7 @@ public function google()
$this->response->redirect('?controller=app');
}
else {
$this->response->html($this->template->layout('user/login', array(
$this->response->html($this->template->layout('auth/index', array(
'errors' => array('login' => t('Google authentication failed')),
'values' => array(),
'no_layout' => true,
Expand Down Expand Up @@ -512,7 +456,7 @@ public function github()
$this->response->redirect('?controller=app');
}
else {
$this->response->html($this->template->layout('user/login', array(
$this->response->html($this->template->layout('auth/index', array(
'errors' => array('login' => t('GitHub authentication failed')),
'values' => array(),
'no_layout' => true,
Expand Down
3 changes: 2 additions & 1 deletion app/Model/Acl.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Acl extends Base
* @var array
*/
private $public_acl = array(
'user' => array('login', 'check', 'google', 'github'),
'auth' => array('login', 'check'),
'user' => array('google', 'github'),
'task' => array('readonly'),
'board' => array('readonly'),
'project' => array('feed'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p class="alert alert-error"><?= $this->e($errors['login']) ?></p>
<?php endif ?>

<form method="post" action="<?= $this->u('user', 'check', array('redirect_query' => urlencode($redirect_query))) ?>">
<form method="post" action="<?= $this->u('auth', 'check', array('redirect_query' => $redirect_query)) ?>">

<?= $this->formCsrf() ?>

Expand Down
2 changes: 1 addition & 1 deletion app/Template/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
</li>
<?php endif ?>
<li>
<?= $this->a(t('Logout'), 'user', 'logout', array(), true) ?>
<?= $this->a(t('Logout'), 'auth', 'logout') ?>
<span class="username hide-tablet">(<?= $this->a($this->e($this->getFullname()), 'user', 'show', array('user_id' => $this->userSession->getId())) ?>)</span>
</li>
</ul>
Expand Down

0 comments on commit 7df055a

Please sign in to comment.