feature: Implement user lookups.
This commit is contained in:
parent
59b012c715
commit
aeef8e4931
4 changed files with 87 additions and 5 deletions
|
@ -11,8 +11,9 @@ import { route as backendCreate } from "./routes/backends/create.js";
|
|||
|
||||
import { route as forwardCreate } from "./routes/forward/create.js";
|
||||
|
||||
import { route as userRemove } from "./routes/user/remove.js";
|
||||
import { route as userCreate } from "./routes/user/create.js";
|
||||
import { route as userRemove } from "./routes/user/remove.js";
|
||||
import { route as userLookup } from "./routes/user/lookup.js";
|
||||
import { route as userLogin } from "./routes/user/login.js";
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
@ -45,8 +46,9 @@ backendCreate(fastify, prisma, sessionTokens, serverOptions);
|
|||
|
||||
forwardCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||
|
||||
userRemove(fastify, prisma, sessionTokens, serverOptions);
|
||||
userCreate(fastify, prisma, sessionTokens, serverOptions);
|
||||
userRemove(fastify, prisma, sessionTokens, serverOptions);
|
||||
userLookup(fastify, prisma, sessionTokens, serverOptions);
|
||||
userLogin(fastify, prisma, sessionTokens, serverOptions);
|
||||
|
||||
// Run the server!
|
||||
|
|
|
@ -9,7 +9,7 @@ export const permissionListDisabled: Record<string, boolean> = {
|
|||
"routes.edit": false,
|
||||
"routes.visible": false,
|
||||
|
||||
"backends.add": false,
|
||||
"backends.add": false,
|
||||
"backends.remove": false,
|
||||
"backends.start": false,
|
||||
"backends.stop": false,
|
||||
|
@ -17,10 +17,12 @@ export const permissionListDisabled: Record<string, boolean> = {
|
|||
"backends.visible": false,
|
||||
"backends.secretVis": false,
|
||||
|
||||
"permissions.see": false,
|
||||
"permissions.see": false,
|
||||
|
||||
"users.add": false,
|
||||
"users.remove": false
|
||||
"users.remove": false,
|
||||
"users.lookup": false,
|
||||
"users.edit": false,
|
||||
};
|
||||
|
||||
// FIXME: This solution fucking sucks.
|
||||
|
|
60
src/routes/user/lookup.ts
Normal file
60
src/routes/user/lookup.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import type { PrismaClient } from "@prisma/client";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
import { ServerOptions, SessionToken } from "../../libs/types.js";
|
||||
import { hasPermissionByToken } from "../../libs/permissions.js";
|
||||
|
||||
export function route(fastify: FastifyInstance, prisma: PrismaClient, tokens: Record<number, SessionToken[]>, options: ServerOptions) {
|
||||
function hasPermission(token: string, permissionList: string[]): Promise<boolean> {
|
||||
return hasPermissionByToken(permissionList, token, tokens, prisma);
|
||||
};
|
||||
|
||||
fastify.post("/api/v1/users/lookup", {
|
||||
schema: {
|
||||
body: {
|
||||
type: "object",
|
||||
required: ["token"],
|
||||
|
||||
properties: {
|
||||
token: { type: "string" },
|
||||
name: { type: "string" },
|
||||
email: { type: "string" },
|
||||
isServiceAccount: { type: "boolean" }
|
||||
}
|
||||
}
|
||||
}
|
||||
}, async(req, res) => {
|
||||
// @ts-ignore
|
||||
const body: {
|
||||
token: string,
|
||||
name?: string,
|
||||
email?: string,
|
||||
isServiceAccount?: boolean
|
||||
} = req.body;
|
||||
|
||||
if (!await hasPermission(body.token, [
|
||||
"users.lookup"
|
||||
])) {
|
||||
return res.status(403).send({
|
||||
error: "Unauthorized"
|
||||
});
|
||||
};
|
||||
|
||||
const users = await prisma.user.findMany({
|
||||
where: {
|
||||
name: body.name,
|
||||
email: body.email,
|
||||
isRootServiceAccount: body.isServiceAccount
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: users.map((i) => ({
|
||||
name: i.name,
|
||||
email: i.email,
|
||||
isServiceAccount: i.isRootServiceAccount
|
||||
}))
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue