add get user/group,

invert  return value for CookieFetcher
This commit is contained in:
Arthur Lu 2024-08-02 04:35:04 +00:00
parent 783bc37c94
commit c059b528fa
4 changed files with 57 additions and 11 deletions

View File

@ -110,6 +110,7 @@ export default class LocalDB extends DB_BACKEND {
getAllGroups (params) {
return null;
}
setGroup (group, attributes, params) {}
delGroup (group, params) {}

View File

@ -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();

View File

@ -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 });
});

View File

@ -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 });
});