Merge pull request #5056 from frederic34/patch-10
FIX If oauth has never been activated two tables are missing and printing is not working
This commit is contained in:
commit
230807b292
@ -64,53 +64,53 @@ class printing_printgcp extends PrintingDriver
|
|||||||
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
|
$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
|
||||||
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
|
$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT; // This is to use external domain name found into config file
|
||||||
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
|
//$urlwithroot=DOL_MAIN_URL_ROOT; // This is to use same domain name than current
|
||||||
|
|
||||||
$this->db = $db;
|
$this->db = $db;
|
||||||
$this->google_id = $conf->global->OAUTH_GOOGLE_ID;
|
|
||||||
$this->google_secret = $conf->global->OAUTH_GOOGLE_SECRET;
|
|
||||||
// Token storage
|
|
||||||
$storage = new DoliStorage($this->db, $this->conf);
|
|
||||||
//$storage->clearToken('Google');
|
|
||||||
// Setup the credentials for the requests
|
|
||||||
$credentials = new Credentials(
|
|
||||||
$this->google_id,
|
|
||||||
$this->google_secret,
|
|
||||||
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
|
|
||||||
);
|
|
||||||
$access = ($storage->hasAccessToken('Google')?'HasAccessToken':'NoAccessToken');
|
|
||||||
$serviceFactory = new \OAuth\ServiceFactory();
|
|
||||||
$apiService = $serviceFactory->createService('Google', $credentials, $storage, array());
|
|
||||||
$token_ok=true;
|
|
||||||
try {
|
|
||||||
$token = $storage->retrieveAccessToken('Google');
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->errors[] = $e->getMessage();
|
|
||||||
$token_ok = false;
|
|
||||||
}
|
|
||||||
//var_dump($this->errors);exit;
|
|
||||||
|
|
||||||
$expire = false;
|
|
||||||
// Is token expired or will token expire in the next 30 seconds
|
|
||||||
if ($token_ok) {
|
|
||||||
$expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Token expired so we refresh it
|
|
||||||
if ($token_ok && $expire) {
|
|
||||||
try {
|
|
||||||
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
|
|
||||||
$refreshtoken = $token->getRefreshToken();
|
|
||||||
$token = $apiService->refreshAccessToken($token);
|
|
||||||
$token->setRefreshToken($refreshtoken);
|
|
||||||
$storage->storeAccessToken('Google', $token);
|
|
||||||
} catch (Exception $e) {
|
|
||||||
$this->errors[] = $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!$conf->oauth->enabled) {
|
if (!$conf->oauth->enabled) {
|
||||||
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'ModuleAuthNotActive', 'type'=>'info');
|
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'ModuleAuthNotActive', 'type'=>'info');
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
$this->google_id = $conf->global->OAUTH_GOOGLE_ID;
|
||||||
|
$this->google_secret = $conf->global->OAUTH_GOOGLE_SECRET;
|
||||||
|
// Token storage
|
||||||
|
$storage = new DoliStorage($this->db, $this->conf);
|
||||||
|
//$storage->clearToken('Google');
|
||||||
|
// Setup the credentials for the requests
|
||||||
|
$credentials = new Credentials(
|
||||||
|
$this->google_id,
|
||||||
|
$this->google_secret,
|
||||||
|
$urlwithroot.'/core/modules/oauth/google_oauthcallback.php'
|
||||||
|
);
|
||||||
|
$access = ($storage->hasAccessToken('Google')?'HasAccessToken':'NoAccessToken');
|
||||||
|
$serviceFactory = new \OAuth\ServiceFactory();
|
||||||
|
$apiService = $serviceFactory->createService('Google', $credentials, $storage, array());
|
||||||
|
$token_ok=true;
|
||||||
|
try {
|
||||||
|
$token = $storage->retrieveAccessToken('Google');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->errors[] = $e->getMessage();
|
||||||
|
$token_ok = false;
|
||||||
|
}
|
||||||
|
//var_dump($this->errors);exit;
|
||||||
|
|
||||||
|
$expire = false;
|
||||||
|
// Is token expired or will token expire in the next 30 seconds
|
||||||
|
if ($token_ok) {
|
||||||
|
$expire = ($token->getEndOfLife() !== -9002 && $token->getEndOfLife() !== -9001 && time() > ($token->getEndOfLife() - 30));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Token expired so we refresh it
|
||||||
|
if ($token_ok && $expire) {
|
||||||
|
try {
|
||||||
|
// il faut sauvegarder le refresh token car google ne le donne qu'une seule fois
|
||||||
|
$refreshtoken = $token->getRefreshToken();
|
||||||
|
$token = $apiService->refreshAccessToken($token);
|
||||||
|
$token->setRefreshToken($refreshtoken);
|
||||||
|
$storage->storeAccessToken('Google', $token);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->errors[] = $e->getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
if ($this->google_id != '' && $this->google_secret != '') {
|
if ($this->google_id != '' && $this->google_secret != '') {
|
||||||
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthConfigured', 'type'=>'info');
|
$this->conf[] = array('varname'=>'PRINTGCP_INFO', 'info'=>'GoogleAuthConfigured', 'type'=>'info');
|
||||||
$this->conf[] = array('varname'=>'PRINTGCP_TOKEN_ACCESS', 'info'=>$access, 'type'=>'info', 'renew'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'delete'=>($storage->hasAccessToken('Google')?$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'):''));
|
$this->conf[] = array('varname'=>'PRINTGCP_TOKEN_ACCESS', 'info'=>$access, 'type'=>'info', 'renew'=>$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?state=userinfo_email,userinfo_profile,cloud_print&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'), 'delete'=>($storage->hasAccessToken('Google')?$urlwithroot.'/core/modules/oauth/google_oauthcallback.php?action=delete&backtourl='.urlencode(DOL_URL_ROOT.'/printing/admin/printing.php?mode=setup&driver=printgcp'):''));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user