GI-26 Drive - Evaluar y revisar la implementación de EndPoints

This commit is contained in:
Marco A. Nina Mena
2015-11-16 15:10:57 -04:00
parent 1edc723b1e
commit b242ac1544
5 changed files with 278 additions and 43 deletions

View File

@@ -16,6 +16,12 @@ class PMGoogleApi
private $domain;
private $user;
private $typeAuthentication;
private $clientId;
private $clientSecret;
private $redirectUrl = 'http://localhost/index.html';
private $accountJson;
public function __construct()
{
$licensedFeatures = &PMLicensedFeatures::getSingleton();
@@ -114,6 +120,81 @@ 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;
}
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
*
@@ -122,11 +203,25 @@ 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'];
$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);
$this->setStatusService($statusService);
@@ -139,18 +234,39 @@ class PMGoogleApi
*/
public function serviceClient()
{
$key = file_get_contents(PATH_DATA_SITE . $this->serviceAccountP12);
$client = null;
if ($this->getTypeAuthentication == 'webApplication') {
$key = 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($key);
$client->setRedirectUri($this->redirectUrl);
$client = new Google_Client();
$client->setApplicationName("PMDrive");
$client->setAssertionCredentials($assertionCredentials);
if (!empty($_SESSION['google_token'])) {
$client->setAccessToken($_SESSION['google_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['google_token']);
}
} else {
$authUrl = $client->createAuthUrl();
print_r($authUrl);
}
} else if ($this->getTypeAuthentication == '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;
}

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');

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'),
//typeAhead : false,
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){
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

@@ -16,6 +16,7 @@ $gCurl = curl_init( 'https://' . $server . '/api/1.0/' . $pmws . '/gmailIntegrat
curl_setopt( $gCurl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $pmtoken ) );
curl_setopt( $gCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $gCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $gCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $gCurl, CURLOPT_CONNECTTIMEOUT, 0);
$gCurl_response = curl_exec( $gCurl );
@@ -33,6 +34,7 @@ $curl = curl_init( 'https://' . $server . '/api/1.0/' . $pmws . '/gmailIntegrati
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' . $pmtoken ) );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 0);
$curl_response = curl_exec( $curl );
@@ -54,6 +56,7 @@ if( !isset($_SESSION['USER_LOGGED']) || $_SESSION['USER_LOGGED'] != $decodedResp
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYHOST, false,
CURLOPT_SSL_VERIFYPEER => false
);
// apply those options
@@ -68,8 +71,8 @@ if( !isset($_SESSION['USER_LOGGED']) || $_SESSION['USER_LOGGED'] != $decodedResp
//If the email corresponds I get the username and with the gmail user_id the session is created.
if($decodedResp['0']->USR_STATUS == "ACTIVE"){
//User Active! lets create the Session
@session_destroy();
session_start();
@session_destroy();
session_start();
session_regenerate_id();
if (PHP_VERSION < 5.2) {
@@ -78,17 +81,17 @@ if( !isset($_SESSION['USER_LOGGED']) || $_SESSION['USER_LOGGED'] != $decodedResp
setcookie("workspaceSkin", "neoclasic", time() + (24 * 60 * 60), "/sys" . "neoclasic", null, false, true);
}
$_SESSION = array();
$_SESSION['__EE_INSTALLATION__'] = 2;
$_SESSION['__EE_SW_PMLICENSEMANAGER__'] = 1;
$_SESSION['phpLastFileFound'] = '';
$_SESSION['USERNAME_PREVIOUS1'] = 'admin';
$_SESSION['USERNAME_PREVIOUS2'] = 'admin';
$_SESSION['WORKSPACE'] = $pmws;
$_SESSION['USER_LOGGED'] = $decodedResp['0']->USR_UID;
$_SESSION['USR_USERNAME'] = $decodedResp['0']->USR_USERNAME;
$_SESSION['USR_FULLNAME'] = $decodedResp['0']->USR_FIRSTNAME. ' ' .$decodedResp['0']->USR_LASTNAME;
$_SESSION['__sw__'] = 1;
$_SESSION = array();
$_SESSION['__EE_INSTALLATION__'] = 2;
$_SESSION['__EE_SW_PMLICENSEMANAGER__'] = 1;
$_SESSION['phpLastFileFound'] = '';
$_SESSION['USERNAME_PREVIOUS1'] = 'admin';
$_SESSION['USERNAME_PREVIOUS2'] = 'admin';
$_SESSION['WORKSPACE'] = $pmws;
$_SESSION['USER_LOGGED'] = $decodedResp['0']->USR_UID;
$_SESSION['USR_USERNAME'] = $decodedResp['0']->USR_USERNAME;
$_SESSION['USR_FULLNAME'] = $decodedResp['0']->USR_FIRSTNAME. ' ' .$decodedResp['0']->USR_LASTNAME;
$_SESSION['__sw__'] = 1;
//session created
} else {
echo Bootstrap::LoadTranslation( 'ID_USER_NOT_ACTIVE' );
@@ -107,6 +110,7 @@ if ($action == "draft"){
curl_setopt( $curlApp, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $curlApp, CURLOPT_RETURNTRANSFER, true);
curl_setopt( $curlApp, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt( $curlApp, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $curlApp, CURLOPT_CONNECTTIMEOUT, 0);
$curl_response_app = curl_exec( $curlApp );