Further fixes on the google synyc activation (#890).

This commit is contained in:
Alex Tselegidis 2020-09-24 10:26:29 +03:00
parent 4c10b5a173
commit 068a3995b0
4 changed files with 56 additions and 33 deletions

View file

@ -82,29 +82,37 @@ class Google extends CI_Controller {
*/ */
public function oauth_callback() public function oauth_callback()
{ {
if ($this->input->get('code')) $code = $this->input->get('code');
if (empty($code))
{ {
$this->load->library('Google_sync'); $this->output->set_output('Code authorization failed.');
$token = $this->google_sync->authenticate($this->input->get('code')); return;
}
// Store the token into the database for future reference. $this->load->library('Google_sync');
$oauth_provider_id = $this->session->userdata('oauth_provider_id');
if ($oauth_provider_id) $token = $this->google_sync->authenticate($code);
{
$this->load->model('providers_model'); if (empty($token))
$this->providers_model->set_setting('google_sync', TRUE, $oauth_provider_id); {
$this->providers_model->set_setting('google_token', $token, $oauth_provider_id); $this->output->set_output('Token authorization failed.');
$this->providers_model->set_setting('google_calendar', 'primary', $oauth_provider_id); return;
} }
else
{ // Store the token into the database for future reference.
$this->output->set_output('<h1>Sync provider id not specified!</h1>'); $oauth_provider_id = $this->session->userdata('oauth_provider_id');
}
if ($oauth_provider_id)
{
$this->load->model('providers_model');
$this->providers_model->set_setting('google_sync', TRUE, $oauth_provider_id);
$this->providers_model->set_setting('google_token', json_encode($token), $oauth_provider_id);
$this->providers_model->set_setting('google_calendar', 'primary', $oauth_provider_id);
} }
else else
{ {
$this->output->set_output('<h1>Authorization Failed!</h1>'); $this->output->set_output('Sync provider id not specified.');
} }
} }

View file

@ -62,7 +62,11 @@ class Google_Sync {
$this->client->setClientSecret($this->framework->config->item('google_client_secret')); $this->client->setClientSecret($this->framework->config->item('google_client_secret'));
$this->client->setDeveloperKey($this->framework->config->item('google_api_key')); $this->client->setDeveloperKey($this->framework->config->item('google_api_key'));
$this->client->setRedirectUri(site_url('google/oauth_callback')); $this->client->setRedirectUri(site_url('google/oauth_callback'));
$this->client->setScopes('https://www.googleapis.com/auth/calendar'); $this->client->setAccessType('offline');
$this->client->addScope([
Google_Service_Calendar::CALENDAR,
Google_Service_Calendar::CALENDAR_READONLY
]);
$this->service = new Google_Service_Calendar($this->client); $this->service = new Google_Service_Calendar($this->client);
} }
@ -88,12 +92,22 @@ class Google_Sync {
* provided. Using this code, we can authenticate the API usage and store the * provided. Using this code, we can authenticate the API usage and store the
* token information to the database. * token information to the database.
* *
* @see Google Controller * @param $code
*
* @return array
*
* @throws Exception
*/ */
public function authenticate($auth_code) public function authenticate($code)
{ {
$this->client->authenticate($auth_code); $response = $this->client->fetchAccessTokenWithAuthCode($code);
return $this->client->getAccessToken();
if (isset($response['error']))
{
throw new Exception('Google Authentication Error (' . $response['error'] . '): ' . $response['error_description']);
}
return $response;
} }
/** /**

View file

@ -250,12 +250,12 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {};
// If the user has already the sync enabled then apply the proper style changes. // If the user has already the sync enabled then apply the proper style changes.
if ($('#select-filter-item option:selected').attr('google-sync') === 'true') { if ($('#select-filter-item option:selected').attr('google-sync') === 'true') {
$('#enable-sync').addClass('btn-danger enabled'); $('#enable-sync').removeClass('btn-light').addClass('btn-danger enabled');
$('#enable-sync span:eq(1)').text(EALang.disable_sync); $('#enable-sync span').text(EALang.disable_sync);
$('#google-sync').prop('disabled', false); $('#google-sync').prop('disabled', false);
} else { } else {
$('#enable-sync').removeClass('btn-danger enabled'); $('#enable-sync').removeClass('btn-danger enabled').addClass('btn-light');
$('#enable-sync span:eq(1)').text(EALang.enable_sync); $('#enable-sync span').text(EALang.enable_sync);
$('#google-sync').prop('disabled', true); $('#google-sync').prop('disabled', true);
} }
} }

View file

@ -53,8 +53,8 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
// The user has granted access to his data. // The user has granted access to his data.
windowHandle.close(); windowHandle.close();
window.clearInterval(authInterval); window.clearInterval(authInterval);
$('#enable-sync').addClass('btn-danger enabled'); $('#enable-sync').addClass('btn-danger enabled').removeClass('btn-light');
$('#enable-sync span:eq(1)').text(EALang.disable_sync); $('#enable-sync span').text(EALang.disable_sync);
$('#google-sync').prop('disabled', false); $('#google-sync').prop('disabled', false);
$('#select-filter-item option:selected').attr('google-sync', 'true'); $('#select-filter-item option:selected').attr('google-sync', 'true');
@ -71,8 +71,9 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
$.post(url, data) $.post(url, data)
.done(function (response) { .done(function (response) {
$('#google-calendar').empty(); $('#google-calendar').empty();
response.forEach(response, function (event) {
$('#google-calendar').append(new Option(event.summary, event.id)); response.forEach(function (calendar) {
$('#google-calendar').append(new Option(calendar.summary, calendar.id));
}); });
$('#select-google-calendar').modal('show'); $('#select-google-calendar').modal('show');
@ -94,7 +95,7 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
var providerId = $('#select-filter-item').val(); var providerId = $('#select-filter-item').val();
var provider = GlobalVariables.availableProviders.find(function (availableProvider) { var provider = GlobalVariables.availableProviders.find(function (availableProvider) {
return Number(provider.id) === Number(providerId); return Number(availableProvider.id) === Number(providerId);
}); });
provider.settings.google_sync = '0'; provider.settings.google_sync = '0';
@ -102,8 +103,8 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {};
disableProviderSync(provider.id); disableProviderSync(provider.id);
$('#enable-sync').removeClass('btn-danger enabled'); $('#enable-sync').removeClass('btn-danger enabled').addClass('btn-light');
$('#enable-sync span:eq(1)').text(EALang.enable_sync); $('#enable-sync span').text(EALang.enable_sync);
$('#google-sync').prop('disabled', true); $('#google-sync').prop('disabled', true);
$('#select-filter-item option:selected').attr('google-sync', 'false'); $('#select-filter-item option:selected').attr('google-sync', 'false');
} }