diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index 183d091d8e5..1e4b61412ce 100644
--- a/htdocs/install/upgrade2.php
+++ b/htdocs/install/upgrade2.php
@@ -76,6 +76,7 @@ require_once $dolibarr_main_document_root.'/core/lib/price.lib.php';
require_once $dolibarr_main_document_root.'/core/class/menubase.class.php';
require_once $dolibarr_main_document_root.'/core/lib/admin.lib.php';
require_once $dolibarr_main_document_root.'/core/lib/files.lib.php';
+require_once $dolibarr_main_document_root.'/user/class/user.class.php';
global $langs;
@@ -129,7 +130,6 @@ if ((!$versionfrom || preg_match('/version/', $versionfrom)) && (!$versionto ||
// Test if batch mode
$sapi_type = php_sapi_name();
$script_file = basename(__FILE__);
- $path = __DIR__.'/';
if (substr($sapi_type, 0, 3) == 'cli') {
print 'Syntax from command line: '.$script_file." x.y.z a.b.c [MAIN_MODULE_NAME1_TO_ENABLE,MAIN_MODULE_NAME2_TO_ENABLE...]\n";
print 'Example, upgrade from 19 to 20: '.$script_file." 19.0.0 20.0.0\n";
@@ -5380,10 +5380,10 @@ function migrate_accountingbookkeeping(int $entity)
*/
function migrate_apiresttokens()
{
- global $db, $langs;
+ global $conf, $db, $langs;
print '
';
- print ''.$langs->trans('MigrationApiRestTokens')." \n";
+ print ''.$langs->trans('MigrationApiRestTokens').":\n";
$error = 0;
$nbofmigration = 0;
@@ -5408,17 +5408,26 @@ function migrate_apiresttokens()
}
if (!$error) {
- $sql = "SELECT 'dolibarr_rest_api' AS service, u.api_key AS token, u.rowid AS fk_user, CURRENT_TIMESTAMP AS datec, u.entity";
+ $sql = "SELECT 'dolibarr_rest_api' AS service, u.api_key AS token, u.rowid AS fk_user, u.entity";
$sql .= " FROM llx_user AS u";
- $sql .= " WHERE u.api_key IS NOT NULL";
+ $sql .= " WHERE u.api_key IS NOT NULL AND u.api_key <> ''";
$result = $db->query($sql);
if ($result) {
+ $tmpuser = new User($db);
+
while ($obj = $db->fetch_object($result)) {
if (!in_array(dolDecrypt($obj->token), $allexistingtokens)) {
- $sqlforinsert = "INSERT INTO ".MAIN_DB_PREFIX."oauth_token (service, token, fk_user, datec, entity)";
- $sqlforinsert .= " VALUES ('".$db->escape($obj->service)."', '".$db->escape($obj->token)."', ".((int) $obj->fk_user).", '".$db->escape($obj->datec)."', ".((int) $obj->entity).")";
+ // Load the object of the user of token so we can get the API_COUNT_CALL
+ unset($tmpuser->conf); $tmpuser->conf = new stdClass();
+ $tmpuser->fetch((int) $obj->fk_user, '', '', 1, ($obj->entity ? $obj->entity : $conf->entity));
+
+ $sqlforinsert = "INSERT INTO ".MAIN_DB_PREFIX."oauth_token (service, token, fk_user, datec, entity, apicount_total)";
+ $sqlforinsert .= " VALUES ('".$db->escape($obj->service)."', '".$db->escape(dolEncrypt(dolDecrypt($obj->token)))."', ";
+ $sqlforinsert .= ((int) $obj->fk_user).", '".$db->idate(dol_now())."', ".((int) $obj->entity).", ";
+ $sqlforinsert .= getDolUserInt('API_COUNT_CALL', 0, $tmpuser);
+ $sqlforinsert .= ")";
$insertresult = $db->query($sqlforinsert);
if (!$insertresult) {
@@ -5442,10 +5451,9 @@ function migrate_apiresttokens()
}
if (!$nbofmigration) {
- print ' |
';
- print '| '.$langs->trans("NothingToDo")." |
\n";
+ print $langs->trans("NothingToDo")."\n";
} else {
print $langs->trans('MigratedTokens', $nbofmigration);
- print '';
}
+ print '';
}
diff --git a/htdocs/user/class/user.class.php b/htdocs/user/class/user.class.php
index b53f66ed67f..72ed4fccbac 100644
--- a/htdocs/user/class/user.class.php
+++ b/htdocs/user/class/user.class.php
@@ -523,7 +523,7 @@ class User extends CommonObject
* @param string $login If defined, login to used for search
* @param string $sid If defined, sid to used for search
* @param int<0,1> $loadpersonalconf 1=also load personal conf of user (in $user->conf->xxx), 0=do not load personal conf.
- * @param int $entity If a value is >= 0, we force the search on a specific entity. If -1, means search depens on default setup.
+ * @param int $entity If a value is >= 0, we force the search on a specific entity. If -1, means search depends on default setup.
* @param string $email If defined, email to used for search
* @param int $fk_socpeople If defined, id of contact for search
* @param int<0,1> $use_email_oauth2 1=Use also email_oauth2 to fetch on email
@@ -761,7 +761,7 @@ class User extends CommonObject
// To get back the global configuration unique to the user
if ($loadpersonalconf) {
- $result = $this->loadPersonalConf();
+ $result = $this->loadPersonalConf($entity);
$result = $this->loadDefaultValues();
@@ -784,16 +784,18 @@ class User extends CommonObject
/**
* Load const values from database table user_param and set it into user->conf->XXX
*
+ * @param int $entity If >=0, force the entity, if -1, use the default entity
* @return int >= 0 if OK, < 0 if KO
*/
- public function loadPersonalConf()
+ public function loadPersonalConf($entity = -1)
{
global $conf;
// Load user->conf for user
$sql = "SELECT param, value FROM ".$this->db->prefix()."user_param";
$sql .= " WHERE fk_user = ".((int) $this->id);
- $sql .= " AND entity = ".((int) $conf->entity);
+ $sql .= " AND entity = ".($entity == -1 ? (int) $conf->entity : (int) $entity);
+
//dol_syslog(get_class($this).'::fetch load personalized conf', LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql) {