From e88f386410dc6779b7ab2d811fc996e2a294b930 Mon Sep 17 00:00:00 2001 From: "Marco A. Nina Mena" Date: Tue, 17 Nov 2015 14:50:41 -0400 Subject: [PATCH] =?UTF-8?q?GI-143=20Investigar=20si=20la=20integraci=C3=B3?= =?UTF-8?q?n=20con=20Drive...=20SOLVED?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add functionality google web application. --- workflow/engine/classes/class.pmGoogleApi.php | 143 ++++++++---------- workflow/engine/controllers/pmGmail.php | 20 ++- workflow/engine/templates/admin/pmGmail.js | 1 + 3 files changed, 82 insertions(+), 82 deletions(-) diff --git a/workflow/engine/classes/class.pmGoogleApi.php b/workflow/engine/classes/class.pmGoogleApi.php index acfb115e0..373e04914 100755 --- a/workflow/engine/classes/class.pmGoogleApi.php +++ b/workflow/engine/classes/class.pmGoogleApi.php @@ -17,9 +17,6 @@ class PMGoogleApi private $user; private $typeAuthentication; - private $clientId; - private $clientSecret; - private $redirectUrl = 'http://localhost/index.html'; private $accountJson; public function __construct() @@ -100,7 +97,7 @@ class PMGoogleApi $this->serviceAccountP12 = $serviceAccountP12; } - public function getserviceAccountP12() + public function getServiceAccountP12() { return $this->serviceAccountP12; } @@ -150,51 +147,6 @@ class PMGoogleApi return $this->accountJson; } - public function setClientId($clientId) - { - $conf = $this->getConfigGmail(); - - $conf->aConfig['clientId'] = $clientId; - $conf->saveConfig('GOOGLE_API_SETTINGS', '', '', ''); - - $this->clientId = $clientId; - } - - public function getClientId() - { - return $this->clientId; - } - - public function setClientSecret($clientSecret) - { - $conf = $this->getConfigGmail(); - - $conf->aConfig['clientSecret'] = $clientSecret; - $conf->saveConfig('GOOGLE_API_SETTINGS', '', '', ''); - - $this->clientSecret = $clientSecret; - } - - public function getClientSecret() - { - return $this->clientSecret; - } - - public function setRedirectUrl($redirectUrl) - { - $conf = $this->getConfigGmail(); - - $conf->aConfig['redirectUrl'] = $redirectUrl; - $conf->saveConfig('GOOGLE_API_SETTINGS', '', '', ''); - - $this->redirectUrl = $redirectUrl; - } - - public function getRedirectUrl() - { - return $this->redirectUrl; - } - /** * load configuration gmail service account * @@ -204,23 +156,16 @@ class PMGoogleApi $conf = $this->getConfigGmail(); $typeAuthentication = empty($conf->aConfig['typeAuthentication']) ? '' : $conf->aConfig['typeAuthentication']; - $clientId = empty($conf->aConfig['clientId']) ? '' : $conf->aConfig['clientId']; - $clientSecret = empty($conf->aConfig['clientSecret']) ? '' : $conf->aConfig['clientSecret']; - $redirectUrl = empty($conf->aConfig['redirectUrl']) ? 'http://localhost/index.html' : $conf->aConfig['redirectUrl']; - $accountJson = empty($conf->aConfig['accountJson']) ? '' : $conf->aConfig['accountJson']; + $serviceAccountP12 = empty($conf->aConfig['serviceAccountP12']) ? '' : $conf->aConfig['serviceAccountP12']; $serviceAccountEmail = empty($conf->aConfig['serviceAccountEmail']) ? '' : $conf->aConfig['serviceAccountEmail']; $statusService = empty($conf->aConfig['statusService']) ? '' : $conf->aConfig['statusService']; $this->scope = array(); - $this->setRedirectUrl($accountJson); $this->setTypeAuthentication($typeAuthentication); - //$this->setClientId($clientId); - //$this->setClientSecret($clientSecret); $this->setAccountJson($accountJson); - $this->setRedirectUrl($redirectUrl); $this->setServiceAccountEmail($serviceAccountEmail); $this->setServiceAccountP12($serviceAccountP12); @@ -235,23 +180,32 @@ class PMGoogleApi public function serviceClient() { $client = null; - if ($this->getTypeAuthentication == 'webApplication') { + if ($this->typeAuthentication == 'webApplication') { $key = file_get_contents(PATH_DATA_SITE . $this->accountJson); $client = new Google_Client(); $client->setAuthConfig($key); - $client->setRedirectUri($this->redirectUrl); + $client->addScope($this->scope); if (!empty($_SESSION['google_token'])) { $client->setAccessToken($_SESSION['google_token']); if ($client->isAccessTokenExpired()) { + $client->getRefreshToken(); unset($_SESSION['google_token']); + $_SESSION['google_token'] = $client->getAccessToken(); } + } else if (!empty($_SESSION['CODE_GMAIL'])) { + $token = $client->authenticate($_SESSION['CODE_GMAIL']); + $_SESSION['google_token'] = $client->getAccessToken(); } else { $authUrl = $client->createAuthUrl(); - print_r($authUrl); + echo ''; + die; } - } else if ($this->getTypeAuthentication == 'serviceAccount') { + } else if ($this->typeAuthentication == 'serviceAccount') { $key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12); $assertionCredentials = new Google_Auth_AssertionCredentials( @@ -276,28 +230,59 @@ class PMGoogleApi * * @return Google_Service_Client $service API service instance. */ - public function testService($serviceAccountEmail, $pathServiceAccountP12) + public function testService($credentials) { - $key = file_get_contents($pathServiceAccountP12); - $assertionCredentials = new Google_Auth_AssertionCredentials( - $serviceAccountEmail, - array( - 'https://www.googleapis.com/auth/drive', - 'https://www.googleapis.com/auth/drive.file', - 'https://www.googleapis.com/auth/drive.readonly', - 'https://www.googleapis.com/auth/drive.metadata.readonly', - 'https://www.googleapis.com/auth/drive.appdata', - 'https://www.googleapis.com/auth/drive.metadata', - 'https://www.googleapis.com/auth/drive.photos.readonly' - ), - $key + $scope = array( + 'https://www.googleapis.com/auth/drive', + 'https://www.googleapis.com/auth/drive.file', + 'https://www.googleapis.com/auth/drive.readonly', + 'https://www.googleapis.com/auth/drive.metadata.readonly', + 'https://www.googleapis.com/auth/drive.appdata', + 'https://www.googleapis.com/auth/drive.metadata', + 'https://www.googleapis.com/auth/drive.photos.readonly' ); - $assertionCredentials->sub = $this->user; - $client = new Google_Client(); - $client->setApplicationName("PMDrive"); - $client->setAssertionCredentials($assertionCredentials); + if ($credentials->typeAuth == 'webApplication') { + $key = file_get_contents($credentials->pathFileJson); + + $client = new Google_Client(); + $client->setAuthConfig($key); + $client->addScope($scope); + + if (!empty($_SESSION['google_token'])) { + $client->setAccessToken($_SESSION['google_token']); + if ($client->isAccessTokenExpired()) { + unset($_SESSION['google_token']); + } + } else if (!empty($_SESSION['CODE_GMAIL'])) { + $token = $client->authenticate($_SESSION['CODE_GMAIL']); + //$client->setAccessToken($token); + // store in the session also + $_SESSION['google_token'] = $client->getAccessToken(); + } else { + $authUrl = $client->createAuthUrl(); + echo ''; + die; + } + } else { + $key = file_get_contents($credentials->pathServiceAccountP12); + $assertionCredentials = new Google_Auth_AssertionCredentials( + $credentials->emailServiceAccount, + $scope, + $key + ); + $assertionCredentials->sub = $this->user; + + $client = new Google_Client(); + $client->setApplicationName("PMDrive"); + $client->setAssertionCredentials($assertionCredentials); + } + + $service = new Google_Service_Drive($client); diff --git a/workflow/engine/controllers/pmGmail.php b/workflow/engine/controllers/pmGmail.php index 3471c7859..6fabdc108 100644 --- a/workflow/engine/controllers/pmGmail.php +++ b/workflow/engine/controllers/pmGmail.php @@ -87,17 +87,31 @@ class pmGmail extends Controller } } + /** + * @param $httpData + */ public function testConfigPmGmail($httpData) { G::LoadClass( "pmGoogleApi" ); $pmGoogle = new PMGoogleApi(); - $emailServiceAccount = empty($httpData->email_service_account) ? $pmGoogle->getServiceAccountEmail() : $httpData->email_service_account; - $pathServiceAccountP12 = empty($_FILES['file_p12']['tmp_name']) ? PATH_DATA_SITE . $pmGoogle->getserviceAccountP12() : $_FILES['file_p12']['tmp_name']; + $result = new stdClass(); - print(G::json_encode($pmGoogle->testService($emailServiceAccount, $pathServiceAccountP12))); + $result->typeAuth = empty($httpData->typeAuth) ? $pmGoogle->getTypeAuthentication() : $httpData->typeAuth; + if ($result->typeAuth == 'webApplication') { + $result->redirectUrl = $pmGoogle->getRedirectUrl(); + $result->pathFileJson = empty($_FILES['file_json']['tmp_name']) ? PATH_DATA_SITE . $pmGoogle->getAccountJson() : $_FILES['file_json']['tmp_name']; + } else { + $result->emailServiceAccount = empty($httpData->email_service_account) ? $pmGoogle->getServiceAccountEmail() : $httpData->email_service_account; + $result->pathServiceAccountP12 = empty($_FILES['file_p12']['tmp_name']) ? PATH_DATA_SITE . $pmGoogle->getserviceAccountP12() : $_FILES['file_p12']['tmp_name']; + } + + print(G::json_encode($pmGoogle->testService($result))); } + /** + * + */ public function testUserGmail() { $criteria = new Criteria(); diff --git a/workflow/engine/templates/admin/pmGmail.js b/workflow/engine/templates/admin/pmGmail.js index aa1482e68..9230cb42d 100644 --- a/workflow/engine/templates/admin/pmGmail.js +++ b/workflow/engine/templates/admin/pmGmail.js @@ -105,6 +105,7 @@ Ext.onReady(function(){ } }, select: function(combo){ + saveButton.disable(); var value = combo.getValue(); if (value == 'webApplication' ) { Ext.getCmp('email_service_account').hide();