diff --git a/workflow/engine/classes/class.pmGoogleApi.php b/workflow/engine/classes/class.pmGoogleApi.php index e55212807..40dd3754d 100755 --- a/workflow/engine/classes/class.pmGoogleApi.php +++ b/workflow/engine/classes/class.pmGoogleApi.php @@ -16,6 +16,9 @@ class PMGoogleApi private $domain; private $user; + private $typeAuthentication; + private $accountJson; + public function __construct() { $licensedFeatures = &PMLicensedFeatures::getSingleton(); @@ -94,7 +97,7 @@ class PMGoogleApi $this->serviceAccountP12 = $serviceAccountP12; } - public function getserviceAccountP12() + public function getServiceAccountP12() { return $this->serviceAccountP12; } @@ -114,6 +117,36 @@ class PMGoogleApi return $this->domain; } + public function setTypeAuthentication($type) + { + $conf = $this->getConfigGmail(); + + $conf->aConfig['typeAuthentication'] = $type; + $conf->saveConfig('GOOGLE_API_SETTINGS', '', '', ''); + + $this->typeAuthentication = $type; + } + + public function getTypeAuthentication() + { + return $this->typeAuthentication; + } + + public function setAccountJson($accountJson) + { + $conf = $this->getConfigGmail(); + + $conf->aConfig['accountJson'] = $accountJson; + $conf->saveConfig('GOOGLE_API_SETTINGS', '', '', ''); + + $this->accountJson = $accountJson; + } + + public function getAccountJson() + { + return $this->accountJson; + } + /** * load configuration gmail service account * @@ -122,11 +155,18 @@ class PMGoogleApi { $conf = $this->getConfigGmail(); - $serviceAccountP12 = empty($conf->aConfig['serviceAccountP12']) ? '' : $conf->aConfig['serviceAccountP12']; - $serviceAccountEmail = empty($conf->aConfig['serviceAccountEmail']) ? '' : $conf->aConfig['serviceAccountEmail']; - $statusService = empty($conf->aConfig['statusService']) ? '' : $conf->aConfig['statusService']; + $typeAuthentication = empty($conf->aConfig['typeAuthentication']) ? '' : $conf->aConfig['typeAuthentication']; + $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->setTypeAuthentication($typeAuthentication); + $this->setAccountJson($accountJson); + $this->setServiceAccountEmail($serviceAccountEmail); $this->setServiceAccountP12($serviceAccountP12); $this->setStatusService($statusService); @@ -139,18 +179,48 @@ class PMGoogleApi */ public function serviceClient() { - $key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12); + $client = null; + if ($this->typeAuthentication == 'webApplication') { + $credential = file_get_contents(PATH_DATA_SITE . $this->accountJson); - $assertionCredentials = new Google_Auth_AssertionCredentials( - $this->serviceAccountEmail, - $this->scope, - $key - ); - $assertionCredentials->sub = $this->user; + $client = new Google_Client(); + $client->setAuthConfig($credential); + $client->addScope($this->scope); - $client = new Google_Client(); - $client->setApplicationName("PMDrive"); - $client->setAssertionCredentials($assertionCredentials); + 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(); + echo ''; + die; + } + } else if ($this->typeAuthentication == 'serviceAccount') { + $key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12); + + $assertionCredentials = new Google_Auth_AssertionCredentials( + $this->serviceAccountEmail, + $this->scope, + $key + ); + $assertionCredentials->sub = $this->user; + + $client = new Google_Client(); + $client->setApplicationName("PMDrive"); + $client->setAssertionCredentials($assertionCredentials); + } else { + throw new Exception(G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR')); + } return $client; } @@ -160,28 +230,57 @@ 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') { + $credential = file_get_contents($credentials->pathFileJson); + + $client = new Google_Client(); + $client->setAuthConfig($credential); + $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']); + $_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 fc4e088d1..6fabdc108 100644 --- a/workflow/engine/controllers/pmGmail.php +++ b/workflow/engine/controllers/pmGmail.php @@ -20,6 +20,9 @@ class pmGmail extends Controller $httpData->status_pmgmail = $httpData->status_pmgmail == 1 ? true : false; $pmGoogle->setStatusService($httpData->status_pmgmail); $message = G::LoadTranslation('ID_ENABLE_PMGMAIL') . ': ' . ($httpData->status_pmgmail ? G::LoadTranslation('ID_ENABLE') : G::LoadTranslation('ID_DISABLE')); + + $pmGoogle->setTypeAuthentication($httpData->typeAuth); + if (!empty($httpData->email_service_account)) { $pmGoogle->setServiceAccountEmail($httpData->email_service_account); $message .= ', ' . G::LoadTranslation('ID_PMG_EMAIL') . ': ' . $httpData->email_service_account; @@ -31,6 +34,12 @@ class pmGmail extends Controller $pmGoogle->setServiceAccountP12($_FILES['file_p12']['name']); $message .= ', ' . G::LoadTranslation('ID_PMG_FILE') . ': ' . $_FILES['file_p12']['name']; } + } if ($_FILES['file_json']['error'] != 1) { + if ($_FILES['file_json']['tmp_name'] != '') { + G::uploadFile($_FILES['file_json']['tmp_name'], PATH_DATA_SITE, $_FILES['file_json']['name']); + $pmGoogle->setAccountJson($_FILES['file_json']['name']); + $message .= ', ' . G::LoadTranslation('ID_PMG_FILE') . ': ' . $_FILES['file_json']['name']; + } } else { $result->success = false; $result->fileError = true; @@ -59,11 +68,16 @@ class pmGmail extends Controller $pmGoogle = new PMGoogleApi(); $accountEmail = $pmGoogle->getServiceAccountEmail(); $fileP12 = $pmGoogle->getServiceAccountP12(); + $fileJson = $pmGoogle->getAccountJson(); + $fileJson = $fileJson == null ? '' : $fileJson; + $type = $pmGoogle->getTypeAuthentication(); $enablePMGmail = $pmGoogle->getStatusService(); $this->setJSVar('accountEmail', $accountEmail); $this->setJSVar('fileP12', $fileP12); $this->setJSVar('enablePMGmail', $enablePMGmail); + $this->setJSVar('fileJson', $fileJson); + $this->setJSVar('typeAuthentication', $type); G::RenderPage('publish', 'extJs'); @@ -73,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 40d900389..b99b88d2c 100644 --- a/workflow/engine/templates/admin/pmGmail.js +++ b/workflow/engine/templates/admin/pmGmail.js @@ -43,11 +43,17 @@ Ext.onReady(function(){ if (btn == "yes") { Ext.getCmp('email_service_account').disable(); Ext.getCmp('file_p12').disable(); + Ext.getCmp('typeAuthentication').disable(); + Ext.getCmp('file_json').disable(); + Ext.getCmp('fileJson').disable(); Ext.getCmp('listUsers').hide(); testButton.disable(); saveButton.disable(); saveSettings(); } else { + Ext.getCmp('typeAuthentication').enable(); + Ext.getCmp('file_json').enable(); + Ext.getCmp('fileJson').enable(); Ext.getCmp('status_pmgmail').enable(); Ext.getCmp('email_service_account').enable(); Ext.getCmp('file_p12').enable(); @@ -60,6 +66,62 @@ Ext.onReady(function(){ } } }, + { + xtype : 'combo', + id : 'typeAuthentication', + name : 'typeAuthentication', + xtype : 'combo', + fieldLabel : _('GMAIL_TYPE_AUTH'), + hiddenName: 'typeAuth', + mode : 'local', + triggerAction : 'all', + forceSelection: true, + store: new Ext.data.SimpleStore({ + fields: ['value','type'], + data: [['webApplication','Web Application'],['serviceAccount', 'Service Account']], + autoLoad: true + }), + submitValue : true, + value: typeAuthentication, + valueField: 'value', + displayField: 'type', + width: 250, + editable: false, + listeners:{ + afterRender: function () { + Ext.getCmp('email_service_account').hide(); + Ext.getCmp('file_p12').hide(); + Ext.getCmp('labelFileP12').hide(); + Ext.getCmp('file_json').hide(); + Ext.getCmp('fileJson').hide(); + if (typeAuthentication == 'webApplication' ) { + Ext.getCmp('file_json').show(); + Ext.getCmp('fileJson').show(); + } else if (typeAuthentication == 'serviceAccount' ) { + Ext.getCmp('email_service_account').show(); + Ext.getCmp('file_p12').show(); + Ext.getCmp('labelFileP12').show(); + } + }, + select: function(combo){ + saveButton.disable(); + var value = combo.getValue(); + if (value == 'webApplication' ) { + Ext.getCmp('email_service_account').hide(); + Ext.getCmp('file_p12').hide(); + Ext.getCmp('labelFileP12').hide(); + Ext.getCmp('file_json').show(); + Ext.getCmp('fileJson').show(); + } else { + Ext.getCmp('email_service_account').show(); + Ext.getCmp('file_p12').show(); + Ext.getCmp('labelFileP12').show(); + Ext.getCmp('file_json').hide(); + Ext.getCmp('fileJson').hide(); + } + } + } + }, { xtype : 'textfield', id : 'email_service_account', @@ -110,11 +172,49 @@ Ext.onReady(function(){ }, { xtype : 'label', + id : 'labelFileP12', + name : 'labelFileP12', labelAlign : 'right', fieldLabel : '', text : fileP12, width : 400, style : "padding-left:180px;" + }, + { + xtype : 'fileuploadfield', + id : 'file_json', + emptyText : _('ID_PMG_SELECT_FILE_JSON'), + fieldLabel : _('ID_PMG_FILE_JSON'), + name : 'file_json', + buttonText : '', + width : 400, + disabled : !enablePMGmail, + buttonCfg : { + iconCls : 'upload-icon' + }, + listeners:{ + change : function(){ + changeSettings(); + }, + afterrender:function(cmp){ + changeSettings(); + cmp.fileInput.set({ + accept:'*/json' + }); + } + }, + regex : /(.)+((\.json)(\w)?)$/i, + regexText : _('ID_PMG_TYPE_ACCEPT') + }, + { + xtype : 'label', + id : 'fileJson', + name : 'fileJson', + labelAlign : 'right', + fieldLabel : '', + text : fileJson, + width : 400, + style : "padding-left:180px;" } ] }); diff --git a/workflow/public_html/gmail.php b/workflow/public_html/gmail.php new file mode 100644 index 000000000..cf866017d --- /dev/null +++ b/workflow/public_html/gmail.php @@ -0,0 +1,10 @@ +"; + echo "window.close()"; + echo ""; + exit; +} \ No newline at end of file