From 068a3995b0d7289785c679a7676a3691e9846087 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Thu, 24 Sep 2020 10:26:29 +0300 Subject: [PATCH] Further fixes on the google synyc activation (#890). --- application/controllers/Google.php | 42 +++++++++++++--------- application/libraries/Google_sync.php | 24 ++++++++++--- assets/js/backend_calendar_default_view.js | 8 ++--- assets/js/backend_calendar_google_sync.js | 15 ++++---- 4 files changed, 56 insertions(+), 33 deletions(-) diff --git a/application/controllers/Google.php b/application/controllers/Google.php index 016f462a..455d8908 100644 --- a/application/controllers/Google.php +++ b/application/controllers/Google.php @@ -82,29 +82,37 @@ class Google extends CI_Controller { */ public function oauth_callback() { - if ($this->input->get('code')) + $code = $this->input->get('code'); + + if (empty($code)) { - $this->load->library('Google_sync'); - $token = $this->google_sync->authenticate($this->input->get('code')); + $this->output->set_output('Code authorization failed.'); + return; + } - // Store the token into the database for future reference. - $oauth_provider_id = $this->session->userdata('oauth_provider_id'); + $this->load->library('Google_sync'); - 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', $token, $oauth_provider_id); - $this->providers_model->set_setting('google_calendar', 'primary', $oauth_provider_id); - } - else - { - $this->output->set_output('

Sync provider id not specified!

'); - } + $token = $this->google_sync->authenticate($code); + + if (empty($token)) + { + $this->output->set_output('Token authorization failed.'); + return; + } + + // Store the token into the database for future reference. + $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 { - $this->output->set_output('

Authorization Failed!

'); + $this->output->set_output('Sync provider id not specified.'); } } diff --git a/application/libraries/Google_sync.php b/application/libraries/Google_sync.php index 07388b7d..00cfaa13 100644 --- a/application/libraries/Google_sync.php +++ b/application/libraries/Google_sync.php @@ -62,7 +62,11 @@ class Google_Sync { $this->client->setClientSecret($this->framework->config->item('google_client_secret')); $this->client->setDeveloperKey($this->framework->config->item('google_api_key')); $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); } @@ -88,12 +92,22 @@ class Google_Sync { * provided. Using this code, we can authenticate the API usage and store the * 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); - return $this->client->getAccessToken(); + $response = $this->client->fetchAccessTokenWithAuthCode($code); + + if (isset($response['error'])) + { + throw new Exception('Google Authentication Error (' . $response['error'] . '): ' . $response['error_description']); + } + + return $response; } /** diff --git a/assets/js/backend_calendar_default_view.js b/assets/js/backend_calendar_default_view.js index 26d753ac..b4cfbe97 100755 --- a/assets/js/backend_calendar_default_view.js +++ b/assets/js/backend_calendar_default_view.js @@ -250,12 +250,12 @@ window.BackendCalendarDefaultView = window.BackendCalendarDefaultView || {}; // If the user has already the sync enabled then apply the proper style changes. if ($('#select-filter-item option:selected').attr('google-sync') === 'true') { - $('#enable-sync').addClass('btn-danger enabled'); - $('#enable-sync span:eq(1)').text(EALang.disable_sync); + $('#enable-sync').removeClass('btn-light').addClass('btn-danger enabled'); + $('#enable-sync span').text(EALang.disable_sync); $('#google-sync').prop('disabled', false); } else { - $('#enable-sync').removeClass('btn-danger enabled'); - $('#enable-sync span:eq(1)').text(EALang.enable_sync); + $('#enable-sync').removeClass('btn-danger enabled').addClass('btn-light'); + $('#enable-sync span').text(EALang.enable_sync); $('#google-sync').prop('disabled', true); } } diff --git a/assets/js/backend_calendar_google_sync.js b/assets/js/backend_calendar_google_sync.js index 6f3a85c7..19268859 100644 --- a/assets/js/backend_calendar_google_sync.js +++ b/assets/js/backend_calendar_google_sync.js @@ -53,8 +53,8 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; // The user has granted access to his data. windowHandle.close(); window.clearInterval(authInterval); - $('#enable-sync').addClass('btn-danger enabled'); - $('#enable-sync span:eq(1)').text(EALang.disable_sync); + $('#enable-sync').addClass('btn-danger enabled').removeClass('btn-light'); + $('#enable-sync span').text(EALang.disable_sync); $('#google-sync').prop('disabled', false); $('#select-filter-item option:selected').attr('google-sync', 'true'); @@ -71,8 +71,9 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; $.post(url, data) .done(function (response) { $('#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'); @@ -94,7 +95,7 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; var providerId = $('#select-filter-item').val(); var provider = GlobalVariables.availableProviders.find(function (availableProvider) { - return Number(provider.id) === Number(providerId); + return Number(availableProvider.id) === Number(providerId); }); provider.settings.google_sync = '0'; @@ -102,8 +103,8 @@ window.BackendCalendarGoogleSync = window.BackendCalendarGoogleSync || {}; disableProviderSync(provider.id); - $('#enable-sync').removeClass('btn-danger enabled'); - $('#enable-sync span:eq(1)').text(EALang.enable_sync); + $('#enable-sync').removeClass('btn-danger enabled').addClass('btn-light'); + $('#enable-sync span').text(EALang.enable_sync); $('#google-sync').prop('disabled', true); $('#select-filter-item option:selected').attr('google-sync', 'false'); }