diff --git a/htdocs/install/upgrade2.php b/htdocs/install/upgrade2.php
index 5883c494205..83f74362657 100644
--- a/htdocs/install/upgrade2.php
+++ b/htdocs/install/upgrade2.php
@@ -593,6 +593,13 @@ if (!GETPOST('action', 'aZ09') || preg_match('/upgrade/i', GETPOST('action', 'aZ
migrate_accountingbookkeeping($entity);
}
+
+ // Scripts for 23.0
+ $afterversionarray = explode('.', '22.0.9');
+ $beforeversionarray = explode('.', '23.0.9');
+ if (versioncompare($versiontoarray, $afterversionarray) >= 0 && versioncompare($versiontoarray, $beforeversionarray) <= 0) {
+ migrate_apiresttokens();
+ }
}
// Code executed only if migration is LAST ONE. Must always be done.
@@ -5353,3 +5360,80 @@ function migrate_accountingbookkeeping(int $entity)
print '
| '.$langs->trans("NothingToDo")." |
\n";
}
}
+
+/**
+ * Migrate API key in oauth_token table
+ *
+ * @return void
+ */
+function migrate_apiresttokens()
+{
+ global $db, $langs;
+
+ print '';
+ print ''.$langs->trans('MigrationApiRestTokens')." \n";
+
+ $error = 0;
+ $nbofmigration = 0;
+ $allexistingtokens = array();
+
+ $db->begin();
+
+ $sqlforalltokens = "SELECT oat.token";
+ $sqlforalltokens .= " FROM ".$db->prefix()."oauth_token AS oat";
+ $sqlforalltokens .= " WHERE oat.service = 'dolibarr_rest_api'";
+
+ $resalltoken = $db->query($sqlforalltokens);
+
+ if ($resalltoken) {
+ while ($tokenobj = $db->fetch_object($resalltoken)) {
+ $allexistingtokens[] = dolDecrypt($tokenobj->token);
+ }
+ } else {
+ $error++;
+ dol_print_error($db);
+ $db->rollback();
+ }
+
+ 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 .= " FROM llx_user AS u";
+ $sql .= " WHERE u.api_key IS NOT NULL";
+
+ $result = $db->query($sql);
+
+ if ($result) {
+ 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).")";
+
+ $insertresult = $db->query($sqlforinsert);
+ if (!$insertresult) {
+ $error++;
+ dol_print_error($db);
+ } else {
+ $nbofmigration++;
+ }
+ }
+ }
+
+ if (!$error) {
+ $db->commit();
+ } else {
+ $db->rollback();
+ }
+ } else {
+ dol_print_error($db);
+ $db->rollback();
+ }
+ }
+
+ if (!$nbofmigration) {
+ print ' |
';
+ print '| '.$langs->trans("NothingToDo")." |
\n";
+ } else {
+ print $langs->trans('MigratedTokens', $nbofmigration);
+ print '';
+ }
+}
diff --git a/htdocs/langs/en_US/install.lang b/htdocs/langs/en_US/install.lang
index 2037f3d8896..8652d9bb15f 100644
--- a/htdocs/langs/en_US/install.lang
+++ b/htdocs/langs/en_US/install.lang
@@ -220,3 +220,5 @@ MigrationContractLineRank=Migrate Contract Line to use Rank (and enable Reorder)
MigrationProductLotPath=Migrate Product Batch files path
MigrationAccountancyBookkeeping=Migrate accountancy bookkeeping to use a ref
InvoiceExportModelsMigration=Migrate invoice export models
+MigrationApiRestTokens=Migrate user's API keys to llx_oauth_token
+MigratedTokens=%s token(s) migrated