From 79ec20ad74ce71e6488135dbf4d0a63e6d6cb0bf Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Wed, 10 Jul 2024 22:38:14 +0000 Subject: [PATCH] fix delUser naming, update config template, implement getAllUsers/getAllGroups in USER_BACKEND_MANAGER --- config/template.config.json | 13 ++++++++++-- src/backends/backends.js | 40 ++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/config/template.config.json b/config/template.config.json index a396259..7e4ced2 100644 --- a/config/template.config.json +++ b/config/template.config.json @@ -34,9 +34,18 @@ "pve": "pve" }, "users": { - "pve": [ + "realm": { + "pve": [ + "localdb" + ], + "ldap": [ + "localdb", + "paasldap" + ] + }, + "any": [ "localdb", - "pve" + "paasldap" ] } }, diff --git a/src/backends/backends.js b/src/backends/backends.js index 7d62777..b563b00 100644 --- a/src/backends/backends.js +++ b/src/backends/backends.js @@ -98,7 +98,7 @@ class USER_BACKEND extends BACKEND { * @param {Object} params authentication params, usually req.cookies * @returns {{ok: boolean, status: number, message: string}} error object or null */ - deluser (user, params = null) {} + delUser (user, params = null) {} /** * Add group to backend @@ -188,14 +188,14 @@ class USER_BACKEND_MANAGER extends USER_BACKEND { } getBackendsByUser (user) { - return this.#config[user.realm]; + return this.#config.realm[user.realm]; } addUser (user, attributes, params = null) {} async getUser (user, params = null) { let userData = {}; - for (const backend of this.#config[user.realm]) { + for (const backend of this.#config.realm[user.realm]) { const backendData = await global.backends[backend].getUser(user, params); if (backendData) { userData = { ...backendData, ...userData }; @@ -204,7 +204,18 @@ class USER_BACKEND_MANAGER extends USER_BACKEND { return userData; } - async getAllUsers (params = null) {} + async getAllUsers (params = null) { + const userData = {}; + for (const backend of this.#config.any) { + const backendData = await global.backends[backend].getAllUsers(params); + if (backendData) { + for (const user of Object.keys(backendData)) { + userData[user] = { ...backendData[user], ...userData[user] }; + } + } + } + return userData; + } async setUser (user, attributes, params = null) { const results = { @@ -212,9 +223,9 @@ class USER_BACKEND_MANAGER extends USER_BACKEND { status: 200, message: "" }; - for (const backend of this.#config[user.realm]) { - const r = await global.backends[backend].setUser(user, attributes, params); - if (!r) { + for (const backend of this.#config.realm[user.realm]) { + const result = await global.backends[backend].setUser(user, attributes, params); + if (!result) { results.ok = false; results.status = 500; return results; @@ -223,13 +234,24 @@ class USER_BACKEND_MANAGER extends USER_BACKEND { return results; } - deluser (user, params = null) {} + delUser (user, params = null) {} addGroup (group, attributes, params = null) {} getGroup (group, params = null) {} - getAllGroups (params = null) {} + async getAllGroups (params = null) { + const groupData = {}; + for (const backend of this.#config.any) { + const backendData = await global.backends[backend].getAllGroups(params); + if (backendData) { + for (const group of Object.keys(backendData)) { + groupData[group] = { ...backendData[group], ...groupData[group] }; + } + } + } + return groupData; + } setGroup (group, attributes, params = null) {}