Add Google authentication

This commit is contained in:
Frédéric Guillot
2014-05-03 22:24:03 -04:00
parent 9531e439cd
commit 560a12f0bd
96 changed files with 7751 additions and 11 deletions

View File

@@ -10,6 +10,102 @@ namespace Controller;
*/
abstract class Base
{
/**
* Acl model
*
* @accesss protected
* @var \Model\Acl
*/
protected $acl;
/**
* Action model
*
* @accesss protected
* @var \Model\Action
*/
protected $action;
/**
* Board model
*
* @accesss protected
* @var \Model\Board
*/
protected $board;
/**
* Config model
*
* @accesss protected
* @var \Model\Config
*/
protected $config;
/**
* Project model
*
* @accesss protected
* @var \Model\Project
*/
protected $project;
/**
* Task model
*
* @accesss protected
* @var \Model\Task
*/
protected $task;
/**
* User model
*
* @accesss protected
* @var \Model\User
*/
protected $user;
/**
* Comment model
*
* @accesss protected
* @var \Model\Comment
*/
protected $comment;
/**
* RememberMe model
*
* @accesss protected
* @var \Model\RememberMe
*/
protected $rememberMe;
/**
* LastLogin model
*
* @accesss protected
* @var \Model\LastLogin
*/
protected $lastLogin;
/**
* Google model
*
* @accesss protected
* @var \Model\Google
*/
protected $google;
/**
* Event instance
*
* @accesss protected
* @var \Model\Event
*/
protected $event;
/**
* Constructor
*
@@ -28,6 +124,7 @@ abstract class Base
$this->comment = $registry->comment;
$this->rememberMe = $registry->rememberMe;
$this->lastLogin = $registry->lastLogin;
$this->google = $registry->google;
$this->event = $registry->shared('event');
}

View File

@@ -248,4 +248,65 @@ class User extends Base
$this->response->redirect('?controller=user');
}
/**
* Google authentication
*
* @access public
*/
public function google()
{
$code = $this->request->getStringParam('code');
if ($code) {
$profile = $this->google->getGoogleProfile($code);
if (is_array($profile)) {
// If the user is already logged, link the account otherwise authenticate
if ($this->acl->isLogged()) {
if ($this->google->updateUser($this->acl->getUserId(), $profile)) {
$this->session->flash(t('Your Google Account is linked to your profile successfully.'));
}
else {
$this->session->flashError(t('Unable to link your Google Account.'));
}
$this->response->redirect('?controller=user');
}
else if ($this->google->authenticate($profile['id'])) {
$this->response->redirect('?controller=app');
}
else {
$this->response->html($this->template->layout('user_login', array(
'errors' => array('login' => t('Google authentication failed')),
'values' => array(),
'no_layout' => true,
'title' => t('Login')
)));
}
}
}
$this->response->redirect($this->google->getAuthorizationUrl());
}
/**
* Unlink a Google account
*
* @access public
*/
public function unlinkGoogle()
{
if ($this->google->unlink($this->acl->getUserId())) {
$this->session->flash(t('Your Google Account is not linked anymore to your profile.'));
}
else {
$this->session->flashError(t('Unable to unlink your Google Account.'));
}
$this->response->redirect('?controller=user');
}
}