Skip to content

Commit

Permalink
Change OAuth endpoints and add Google avatar support
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Mar 27, 2016
1 parent c3138c7 commit 8165c1e
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 9 deletions.
11 changes: 7 additions & 4 deletions Auth/GoogleAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function authenticate()
*
* @access public
* @param string $code
* @return GoogleAuth
* @return GoogleAuthProvider
*/
public function setCode($code)
{
Expand Down Expand Up @@ -104,9 +104,12 @@ public function getService()
$this->getGoogleClientId(),
$this->getGoogleClientSecret(),
$this->helper->url->to('OAuth', 'handler', array('plugin' => 'GoogleAuth'), '', true),
'https://accounts.google.com/o/oauth2/auth',
'https://accounts.google.com/o/oauth2/token',
array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile')
'https://accounts.google.com/o/oauth2/v2/auth',
'https://www.googleapis.com/oauth2/v4/token',
array(
'email',
'profile',
)
);
}

Expand Down
1 change: 1 addition & 0 deletions Locale/fr_FR/translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
'Google Authentication' => 'Authentification Google',
'Help on Google authentication' => 'Aide sur l\'authentification Google',
'Google Id' => 'Identifiant Google',
'Show Google Avatar' => 'Afficher l\'avatar Google',
);
36 changes: 32 additions & 4 deletions Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,58 @@
namespace Kanboard\Plugin\GoogleAuth;

use Kanboard\Core\Plugin\Base;
use Kanboard\Core\Security\AuthenticationManager;
use Kanboard\Core\Translator;
use Kanboard\Core\Security\Role;
use Kanboard\Event\AuthSuccessEvent;
use Kanboard\Plugin\GoogleAuth\Auth\GoogleAuthProvider;
use Kanboard\Plugin\GoogleAuth\User\Avatar\GoogleAvatarProvider;

class Plugin extends Base
{
public function initialize()
{
$this->on('app.bootstrap', function ($container) {
Translator::load($container['config']->getCurrentLanguage(), __DIR__.'/Locale');
});
$this->dispatcher->addListener('app.bootstrap', array($this, 'onBootstrap'));
$this->dispatcher->addListener(AuthenticationManager::EVENT_SUCCESS, array($this, 'onLoginSuccess'));

$this->authenticationManager->register(new GoogleAuthProvider($this->container));
$this->applicationAccessMap->add('OAuth', 'handler', Role::APP_PUBLIC);
$this->avatarManager->register(new GoogleAvatarProvider($this->container));

$this->route->addRoute('/oauth/google', 'OAuth', 'handler', 'GoogleAuth');

$this->template->hook->attach('template:auth:login-form:after', 'GoogleAuth:auth/login');
$this->template->hook->attach('template:config:integrations', 'GoogleAuth:config/integration');
$this->template->hook->attach('template:user:external', 'GoogleAuth:user/external');
$this->template->hook->attach('template:user:integrations', 'GoogleAuth:user/integrations');
$this->template->hook->attach('template:user:authentication:form', 'GoogleAuth:user/authentication');
$this->template->hook->attach('template:user:create-remote:form', 'GoogleAuth:user/create_remote');
}

public function onBootstrap()
{
Translator::load($this->config->getCurrentLanguage(), __DIR__.'/Locale');
}

public function onLoginSuccess(AuthSuccessEvent $event)
{
if ($event->getAuthType() === 'Google') {
$provider = $this->authenticationManager->getProvider($event->getAuthType());
$avatar_url = $provider->getUser()->getAvatarUrl();
$user_id = $this->userSession->getId();

if (! empty($avatar_url)) {
$options = array('google_avatar_url' => $avatar_url);

if (! $this->userMetadata->exists($user_id, 'google_show_avatar')) {
$options['google_show_avatar'] = 1;
}

$this->userMetadata->save($user_id, $options);
}
}
}

public function getPluginName()
{
return 'Google Authentication';
Expand All @@ -44,7 +72,7 @@ public function getPluginAuthor()

public function getPluginVersion()
{
return '1.0.0';
return '1.0.1';
}

public function getPluginHomepage()
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Google Authentication

[![Build Status](https://travis-ci.org/kanboard/plugin-google-auth.svg?branch=master)](https://travis-ci.org/kanboard/plugin-google-auth)

Link a Google account to a Kanboard user profile.
- Link a Google account to a Kanboard user profile.
- Show Google Avatar image.

Author
------
Expand Down Expand Up @@ -84,3 +85,5 @@ Kanboard use these information from your Google profile:
- Google unique id

The Google unique id is used to link together the local user account and the Google account.

To disable the Google Avatar go to your user profile > integrations > and change the value of the checkbox.
10 changes: 10 additions & 0 deletions Template/user/integrations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h3><i class="fa fa-google fa-fw"></i> <?= t('Google Account') ?></h3>

<p class="listing">
<?= $this->form->hidden('google_show_avatar', array('google_show_avatar' => 0)) ?>
<?= $this->form->checkbox('google_show_avatar', t('Show Google Avatar'), 1, isset($values['google_show_avatar']) && $values['google_show_avatar'] == 1) ?>
</p>

<div class="form-actions">
<button type="submit" class="btn btn-blue"><?= t('Save') ?></button>
</div>
54 changes: 54 additions & 0 deletions User/Avatar/GoogleAvatarProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Kanboard\Plugin\GoogleAuth\User\Avatar;

use Kanboard\Core\Base;
use Kanboard\Core\User\Avatar\AvatarProviderInterface;

/**
* Google Avatar Provider
*
* @package avatar
* @author Frederic Guillot
*/
class GoogleAvatarProvider extends Base implements AvatarProviderInterface
{
private $googleAvatarCache = array();

/**
* Render avatar html
*
* @access public
* @param array $user
* @param int $size
* @return string
*/
public function render(array $user, $size)
{
$url = $this->googleAvatarCache[$user['id']].'?sz='.$size;
$title = $this->helper->text->e($user['name'] ?: $user['username']);
return '<img src="'.$url.'" alt="'.$title.'" title="'.$title.'">';
}

/**
* Determine if the provider is active
*
* @access public
* @param array $user
* @return boolean
*/
public function isActive(array $user)
{
if (!isset($this->googleAvatarCache[$user['id']])) {
$metadata = $this->userMetadata->getAll($user['id']);

if (isset($metadata['google_show_avatar']) && $metadata['google_show_avatar'] == 1) {
$this->googleAvatarCache[$user['id']] = $this->userMetadata->get($user['id'], 'google_avatar_url');
} else {
$this->googleAvatarCache[$user['id']] = '';
}
}

return $this->googleAvatarCache[$user['id']] !== '';
}
}
11 changes: 11 additions & 0 deletions User/GoogleUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,15 @@ public function getExternalIdColumn()
{
return 'google_id';
}

/**
* Get Avatar image url from Google profile
*
* @access public
* @return string
*/
public function getAvatarUrl()
{
return !empty($this->user['picture']) ? $this->user['picture'] : '';
}
}

0 comments on commit 8165c1e

Please sign in to comment.