From 56c4f060543542692accdbfab7cb1fff82a7f40f Mon Sep 17 00:00:00 2001
From: alex <alex@alexloehr.net>
Date: Sun, 22 Jun 2025 16:47:10 +0000
Subject: [PATCH] GS-2156

---
 lib/libIlias.js |  108 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 103 insertions(+), 5 deletions(-)

diff --git a/lib/libIlias.js b/lib/libIlias.js
index 4a5fe9b..88a2004 100644
--- a/lib/libIlias.js
+++ b/lib/libIlias.js
@@ -1,17 +1,90 @@
 /* Lib for interacting with customized ILIAS php */
+const _ = require("lodash")
+const settings = require("../settings")
+const {getObjIdFromRefId} = require("./db")
+const db = require("./db")
+const {url, iliastoken} = settings.ilias
+const search = require("./search")
+
+/////////////////////////////////////////////////////////////////////////
 
 module.exports = {
+   getUser,
+
+   importIliasUser,
+
+
    deleteUser,
+   deleteAllUsers,
    deleteTeilnahme,
 }
 
 /////////////////////////////////////////////////////////////////////////
 
-const settings = require("../settings")
-const {getObjIdFromRefId} = require("./db")
-const {url} = settings.ilias
+/////// GET USER ////////////////////////////////////////////////////////////////
+async function getUser (usr_id) {
+   const sp = new URLSearchParams({
+      command: "getUser",
+      usr_id,
+      token: iliastoken,
+   })
+   let url2 = `${url}?${sp.toString()}`
+   const res = await fetch(url2, {method: "GET"})
+   return await res.json() //
+}
 
-/////////////////////////////////////////////////////////////////////////
+/////// USER IMPORT ////////////////////////////////////////////////////////////////
+
+/**
+ * Format in POPCORN für SOAP; ausprobieren ob das hier auch funktioniert, v.a. die user-defined-fields
+ *   const user = {
+ *       login: "123456789",
+ *       passwd: "123456789",
+ *       passwd_type: "plain",
+ *       firstname: "Adolfo",
+ *       lastname: "de la Cruz",
+ *       email: "alex@gorillaeis.com",
+ *       gender: "m",
+ *       department: "Bananenpflücker",
+ *       institution: "Globus Budapest",
+ *       role: 4, // assigned global role id
+ *       udf: {
+ *          "Markt": "Markt UDF 2",
+ *          "Marktnummer": "Marktnummer UDF 2",
+ *          "Personalnummer": "Personal UDF 2",
+ *       },
+ *    }
+ * @param user
+ * @returns {Promise<any>}
+ */
+async function importIliasUser (user) {
+   const sp = new URLSearchParams({
+      command: "importUser",
+      token: iliastoken,
+   })
+   let url2 = `${url}?${sp.toString()}`
+
+   const udfDef = await db.getUdf()
+   const udfMap = _.keyBy(udfDef, "field_name")
+   user.udf = _.mapKeys(user.udf, function (value, key) {
+      return udfMap[key].field_id
+   })
+
+   const res = await fetch(url2, {
+      method: "POST",
+      body: JSON.stringify(user)
+   })
+   const text = await res.text()
+   try {
+      return JSON.parse(text)
+   } catch (ex) {
+      console.error(ex.message)
+      console.log(text)
+      throw ex
+   }
+}
+
+/////// USER DELETE ////////////////////////////////////////////////////////////////
 
 async function deleteUser (obj_id, dry = false) {
    const sp = new URLSearchParams({
@@ -19,11 +92,34 @@
       obj_id,
       // dry: "1",
       dry: dry ? "1" : "0",
+      token: iliastoken,
    })
    let url2 = `${url}?${sp.toString()}`
    const res = await fetch(url2, {method: "DELETE"})
    const data = await res.json()
-   return data
+   // TODO update search index
+   return data //
+}
+
+async function deleteAllUsers () {
+   const {data: users} = await db.getUsers(0, 100000)
+   const res = {
+      command: "deleteAllUsers",
+      start: new Date(),
+      end: new Date(),
+      duration: null,
+      count: 0,
+      userIds: [],
+   }
+   for (const user of users) {
+      await deleteUser(user.usr_id)
+      res.count += 1
+      res.userIds.push(user.usr_id)
+   }
+   res.end = new Date()
+   res.duration = res.end - res.start
+   // TODO update search index
+   return res
 }
 
 async function deleteTeilnahme (ref_id, usr_id, dry = false) {
@@ -33,6 +129,7 @@
       obj_id,
       usr_id,
       dry: dry ? "1" : "0",
+      token: iliastoken,
    })
    let url2 = `${url}?${sp.toString()}`
    console.log("libIlias.deleteTeilnahme >>>", url2)
@@ -45,3 +142,4 @@
       throw ex
    }
 }
+

--
Gitblit v1.8.0