Merged in marcoAntonioNina/processmaker/GI-143 (pull request #3163)

GI-143
This commit is contained in:
Dante Loayza
2015-11-17 15:35:16 -04:00
4 changed files with 272 additions and 35 deletions

View File

@@ -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();
$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,6 +179,33 @@ class PMGoogleApi
*/
public function serviceClient()
{
$client = null;
if ($this->typeAuthentication == 'webApplication') {
$credential = file_get_contents(PATH_DATA_SITE . $this->accountJson);
$client = new Google_Client();
$client->setAuthConfig($credential);
$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();
echo '<script type="text/javascript">
var opciones = "width=450,height=480,scrollbars=NO, locatin=NO,toolbar=NO, status=NO, menumbar=NO, top=10%, left=25%";
window.open("' . $authUrl . '","Gmail", opciones);
</script>';
die;
}
} else if ($this->typeAuthentication == 'serviceAccount') {
$key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12);
$assertionCredentials = new Google_Auth_AssertionCredentials(
@@ -151,6 +218,9 @@ class PMGoogleApi
$client = new Google_Client();
$client->setApplicationName("PMDrive");
$client->setAssertionCredentials($assertionCredentials);
} else {
throw new Exception(G::LoadTranslation('ID_SERVER_COMMUNICATION_ERROR'));
}
return $client;
}
@@ -160,13 +230,10 @@ 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(
$scope = array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.readonly',
@@ -174,7 +241,36 @@ class PMGoogleApi
'https://www.googleapis.com/auth/drive.appdata',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.photos.readonly'
),
);
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 '<script type="text/javascript">
var opciones = "width=450,height=480,scrollbars=NO, locatin=NO,toolbar=NO, status=NO, menumbar=NO, top=10%, left=25%";
window.open("' . $authUrl . '","Gmail", opciones);
</script>';
die;
}
} else {
$key = file_get_contents($credentials->pathServiceAccountP12);
$assertionCredentials = new Google_Auth_AssertionCredentials(
$credentials->emailServiceAccount,
$scope,
$key
);
$assertionCredentials->sub = $this->user;
@@ -182,6 +278,9 @@ class PMGoogleApi
$client = new Google_Client();
$client->setApplicationName("PMDrive");
$client->setAssertionCredentials($assertionCredentials);
}
$service = new Google_Service_Drive($client);

View File

@@ -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();

View File

@@ -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;"
}
]
});

View File

@@ -0,0 +1,10 @@
<?php
session_start();
if (isset($_GET['code'])) {
$_SESSION['CODE_GMAIL'] = $_GET['code'];
echo "<SCRIPT language='Javascript' type='text/javascript'>";
echo "window.close()";
echo "</script>";
exit;
}