From c059b528fa7f8ca75bfa14d49d5b62355b38afd2 Mon Sep 17 00:00:00 2001 From: Arthur Lu Date: Fri, 2 Aug 2024 04:35:04 +0000 Subject: [PATCH] add get user/group, invert return value for CookieFetcher --- src/backends/localdb.js | 1 + src/routes/access.js | 12 ++++++------ src/routes/access/groups.js | 28 +++++++++++++++++++++++++--- src/routes/access/users.js | 27 +++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/backends/localdb.js b/src/backends/localdb.js index d537c23..79242a2 100644 --- a/src/backends/localdb.js +++ b/src/backends/localdb.js @@ -110,6 +110,7 @@ export default class LocalDB extends DB_BACKEND { getAllGroups (params) { return null; } + setGroup (group, attributes, params) {} delGroup (group, params) {} diff --git a/src/routes/access.js b/src/routes/access.js index bfa69f6..086d102 100644 --- a/src/routes/access.js +++ b/src/routes/access.js @@ -3,7 +3,7 @@ export const router = Router({ mergeParams: true }); ; const checkAuth = global.utils.checkAuth; -global.utils.recursiveImportRoutes(router, "/access", "access", import.meta.url); +global.utils.recursiveImportRoutes(router, "", "access", import.meta.url); /** * GET - check authentication @@ -30,7 +30,7 @@ class CookieFetcher { if (this.#fetchedBackends.indexOf(backend) === -1) { const response = await global.backends[backend].openSession(user, password); if (!response.ok) { - return false; + return response.message; } this.#cookies = this.#cookies.concat(response.cookies); this.#fetchedBackends.push(backend); @@ -39,7 +39,7 @@ class CookieFetcher { continue; } } - return true; + return null; } exportCookies () { @@ -67,9 +67,9 @@ router.post("/ticket", async (req, res) => { let backends = global.userManager.getBackendsByUser(userObj); backends = backends.concat(["pve"]); const cm = new CookieFetcher(); - const success = await cm.fetchBackends(backends, userObj, params.password); - if (!success) { - res.status(401).send({ auth: false }); + const error = await cm.fetchBackends(backends, userObj, params.password); + if (error) { + res.status(401).send({ auth: false, error }); return; } const cookies = cm.exportCookies(); diff --git a/src/routes/access/groups.js b/src/routes/access/groups.js index d2693d1..7bd4de1 100644 --- a/src/routes/access/groups.js +++ b/src/routes/access/groups.js @@ -6,8 +6,8 @@ const checkAuth = global.utils.checkAuth; /** * GET - get all groups * responses: - * - 200: {auth:true, groups: Array} - * - 201: {auth: false} + * - 200: {auth: true, groups: Array} + * - 401: {auth: false} */ router.get("/", async (req, res) => { // check auth @@ -15,5 +15,27 @@ router.get("/", async (req, res) => { if (!auth) { return; } - res.status(200).send(global.userManager.getAllGroups()); + const groups = await global.userManager.getAllGroups(req.cookies); + res.status(200).send({ groups }); +}); + +/** + * GET - get specific group + * request: + * - groupname: name of group to get + * responses: + * - 200: {auth: true, group: Object} + * - 401: {auth: false} + */ +router.get("/:groupname", async (req, res) => { + const params = { + groupname: req.params.groupname + }; + // check auth + const auth = await checkAuth(req.cookies, res); + if (!auth) { + return; + } + const group = await global.userManager.getGroup(params.groupname, req.cookies); + res.status(200).send({ group }); }); diff --git a/src/routes/access/users.js b/src/routes/access/users.js index df29917..21f9ce9 100644 --- a/src/routes/access/users.js +++ b/src/routes/access/users.js @@ -7,7 +7,7 @@ const checkAuth = global.utils.checkAuth; * GET - get all users * responses: * - 200: {auth:true, users: Array} - * - 201: {auth: false} + * - 401: {auth: false} */ router.get("/", async (req, res) => { // check auth @@ -15,5 +15,28 @@ router.get("/", async (req, res) => { if (!auth) { return; } - res.status(200).send(global.userManager.getAllUsers()); + const users = await global.userManager.getAllUsers(req.cookies); + res.status(200).send({ users }); +}); + +/** + * GET - get specific user + * request: + * - username: username (id@realm) of user to get + * responses: + * - 200: {auth: true, user: Object} + * - 401: {auth: false} + */ +router.get("/:username", async (req, res) => { + const params = { + username: req.params.username + }; + // check auth + const auth = await checkAuth(req.cookies, res); + if (!auth) { + return; + } + const userObj = global.utils.getUserObjFromUsername(params.username); + const user = await global.userManager.getUser(userObj, req.cookies); + res.status(200).send({ user }); });