1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/kanboard_ynh.git synced 2024-09-03 19:36:17 +02:00
kanboard_ynh/sources/app/Auth/Google.php

124 lines
2.8 KiB
PHP
Raw Normal View History

2014-07-20 12:26:15 +02:00
<?php
2014-10-22 19:59:09 +02:00
namespace Auth;
2014-07-20 12:26:15 +02:00
2015-01-16 14:23:05 +01:00
use Event\AuthEvent;
2014-07-20 12:26:15 +02:00
/**
2014-10-22 19:59:09 +02:00
* Google backend
2014-07-20 12:26:15 +02:00
*
2014-10-22 19:59:09 +02:00
* @package auth
2014-07-20 12:26:15 +02:00
* @author Frederic Guillot
*/
class Google extends Base
{
2014-10-22 19:59:09 +02:00
/**
* Backend name
*
* @var string
*/
const AUTH_NAME = 'Google';
2015-08-16 17:04:56 +02:00
/**
* OAuth2 instance
*
* @access private
* @var \Core\OAuth2
*/
private $service;
2014-07-20 12:26:15 +02:00
/**
* Authenticate a Google user
*
* @access public
* @param string $google_id Google unique id
* @return boolean
*/
public function authenticate($google_id)
{
2014-10-22 19:59:09 +02:00
$user = $this->user->getByGoogleId($google_id);
2014-07-20 12:26:15 +02:00
2015-04-21 17:56:16 +02:00
if (! empty($user)) {
2015-01-16 14:23:05 +01:00
$this->userSession->refresh($user);
$this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id']));
2014-07-20 12:26:15 +02:00
return true;
}
return false;
}
/**
* Unlink a Google account for a given user
*
* @access public
* @param integer $user_id User id
* @return boolean
*/
public function unlink($user_id)
{
2014-10-22 19:59:09 +02:00
return $this->user->update(array(
2014-07-20 12:26:15 +02:00
'id' => $user_id,
'google_id' => '',
));
}
/**
* Update the user table based on the Google profile information
*
* @access public
* @param integer $user_id User id
* @param array $profile Google profile
* @return boolean
*/
public function updateUser($user_id, array $profile)
{
2015-08-16 17:04:56 +02:00
$user = $this->user->getById($user_id);
2014-10-22 19:59:09 +02:00
return $this->user->update(array(
2014-07-20 12:26:15 +02:00
'id' => $user_id,
'google_id' => $profile['id'],
2015-08-16 17:04:56 +02:00
'email' => $profile['email'] ?: $user['email'],
'name' => $profile['name'] ?: $user['name'],
2014-07-20 12:26:15 +02:00
));
}
/**
2015-08-16 17:04:56 +02:00
* Get OAuth2 configured service
2014-07-20 12:26:15 +02:00
*
* @access public
2015-08-16 17:04:56 +02:00
* @return \Core\OAuth2
2014-07-20 12:26:15 +02:00
*/
public function getService()
{
2015-08-16 17:04:56 +02:00
if (empty($this->service)) {
$this->service = $this->oauth->createService(
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
$this->helper->url->to('oauth', 'google', array(), '', 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')
);
}
2014-07-20 12:26:15 +02:00
2015-08-16 17:04:56 +02:00
return $this->service;
2014-07-20 12:26:15 +02:00
}
/**
2015-08-16 17:04:56 +02:00
* Get Google profile
2014-07-20 12:26:15 +02:00
*
* @access public
2015-08-16 17:04:56 +02:00
* @param string $code
* @return array
2014-07-20 12:26:15 +02:00
*/
2015-08-16 17:04:56 +02:00
public function getProfile($code)
2014-07-20 12:26:15 +02:00
{
2015-08-16 17:04:56 +02:00
$this->getService()->getAccessToken($code);
2014-07-20 12:26:15 +02:00
2015-08-16 17:04:56 +02:00
return $this->httpClient->getJson(
'https://www.googleapis.com/oauth2/v1/userinfo',
array($this->getService()->getAuthorizationHeader())
);
2014-07-20 12:26:15 +02:00
}
}