2023-08-24 04:15:07 +02:00
|
|
|
const { ApiKey } = require("../models/apiKeys");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { Document } = require("../models/documents");
|
|
|
|
const { Invite } = require("../models/invite");
|
|
|
|
const { SystemSettings } = require("../models/systemSettings");
|
|
|
|
const { User } = require("../models/user");
|
|
|
|
const { DocumentVectors } = require("../models/vectors");
|
|
|
|
const { Workspace } = require("../models/workspace");
|
|
|
|
const { WorkspaceChats } = require("../models/workspaceChats");
|
|
|
|
const { getVectorDbClass } = require("../utils/helpers");
|
2024-01-22 23:14:01 +01:00
|
|
|
const {
|
|
|
|
validRoleSelection,
|
|
|
|
canModifyAdmin,
|
|
|
|
validCanModify,
|
|
|
|
} = require("../utils/helpers/admin");
|
2023-11-13 23:51:16 +01:00
|
|
|
const { reqBody, userFromSession } = require("../utils/http");
|
|
|
|
const {
|
|
|
|
strictMultiUserRoleValid,
|
2024-01-22 23:14:01 +01:00
|
|
|
ROLES,
|
2023-11-13 23:51:16 +01:00
|
|
|
} = require("../utils/middleware/multiUserProtected");
|
2023-07-25 19:37:04 +02:00
|
|
|
const { validatedRequest } = require("../utils/middleware/validatedRequest");
|
|
|
|
|
|
|
|
function adminEndpoints(app) {
|
|
|
|
if (!app) return;
|
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
app.get(
|
|
|
|
"/admin/users",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (_request, response) => {
|
|
|
|
try {
|
|
|
|
const users = (await User.where()).map((user) => {
|
|
|
|
const { password, ...rest } = user;
|
|
|
|
return rest;
|
|
|
|
});
|
|
|
|
response.status(200).json({ users });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 23:51:16 +01:00
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/admin/users/new",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
2024-01-22 23:14:01 +01:00
|
|
|
const currUser = await userFromSession(request, response);
|
2023-07-25 19:37:04 +02:00
|
|
|
const newUserParams = reqBody(request);
|
2024-01-22 23:14:01 +01:00
|
|
|
const roleValidation = validRoleSelection(currUser, newUserParams);
|
|
|
|
|
|
|
|
if (!roleValidation.valid) {
|
|
|
|
response
|
|
|
|
.status(200)
|
|
|
|
.json({ user: null, error: roleValidation.error });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-25 19:37:04 +02:00
|
|
|
const { user: newUser, error } = await User.create(newUserParams);
|
|
|
|
response.status(200).json({ user: newUser, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
app.post(
|
|
|
|
"/admin/user/:id",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
2024-01-22 23:14:01 +01:00
|
|
|
const currUser = await userFromSession(request, response);
|
2023-11-13 23:51:16 +01:00
|
|
|
const { id } = request.params;
|
|
|
|
const updates = reqBody(request);
|
2023-11-14 23:43:40 +01:00
|
|
|
const user = await User.get({ id: Number(id) });
|
|
|
|
|
2024-01-22 23:14:01 +01:00
|
|
|
const canModify = validCanModify(currUser, user);
|
|
|
|
if (!canModify.valid) {
|
|
|
|
response.status(200).json({ success: false, error: canModify.error });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const roleValidation = validRoleSelection(currUser, updates);
|
|
|
|
if (!roleValidation.valid) {
|
|
|
|
response
|
|
|
|
.status(200)
|
|
|
|
.json({ success: false, error: roleValidation.error });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const validAdminRoleModification = await canModifyAdmin(user, updates);
|
|
|
|
if (!validAdminRoleModification.valid) {
|
|
|
|
response
|
|
|
|
.status(200)
|
|
|
|
.json({ success: false, error: validAdminRoleModification.error });
|
|
|
|
return;
|
2023-11-14 23:43:40 +01:00
|
|
|
}
|
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
const { success, error } = await User.update(id, updates);
|
|
|
|
response.status(200).json({ success, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 23:51:16 +01:00
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/admin/user/:id",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
2024-01-22 23:14:01 +01:00
|
|
|
const currUser = await userFromSession(request, response);
|
2023-07-25 19:37:04 +02:00
|
|
|
const { id } = request.params;
|
2024-01-22 23:14:01 +01:00
|
|
|
const user = await User.get({ id: Number(id) });
|
|
|
|
|
|
|
|
const canModify = validCanModify(currUser, user);
|
|
|
|
if (!canModify.valid) {
|
|
|
|
response.status(200).json({ success: false, error: canModify.error });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
await User.delete({ id: Number(id) });
|
2023-07-25 19:37:04 +02:00
|
|
|
response.status(200).json({ success: true, error: null });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
app.get(
|
|
|
|
"/admin/invites",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (_request, response) => {
|
|
|
|
try {
|
|
|
|
const invites = await Invite.whereWithUsers();
|
|
|
|
response.status(200).json({ invites });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 23:51:16 +01:00
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
|
|
|
|
app.get(
|
|
|
|
"/admin/invite/new",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { invite, error } = await Invite.create(user.id);
|
|
|
|
response.status(200).json({ invite, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/admin/invite/:id",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { id } = request.params;
|
|
|
|
const { success, error } = await Invite.deactivate(id);
|
|
|
|
response.status(200).json({ success, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
"/admin/workspaces",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (_request, response) => {
|
2023-07-25 19:37:04 +02:00
|
|
|
try {
|
|
|
|
const workspaces = await Workspace.whereWithUsers();
|
|
|
|
response.status(200).json({ workspaces });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/admin/workspaces/new",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { name } = reqBody(request);
|
|
|
|
const { workspace, message: error } = await Workspace.new(
|
|
|
|
name,
|
|
|
|
user.id
|
|
|
|
);
|
|
|
|
response.status(200).json({ workspace, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/admin/workspaces/:workspaceId/update-users",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { workspaceId } = request.params;
|
|
|
|
const { userIds } = reqBody(request);
|
|
|
|
const { success, error } = await Workspace.updateUsers(
|
2023-09-28 23:00:03 +02:00
|
|
|
workspaceId,
|
2023-07-25 19:37:04 +02:00
|
|
|
userIds
|
|
|
|
);
|
|
|
|
response.status(200).json({ success, error });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/admin/workspaces/:id",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { id } = request.params;
|
|
|
|
const VectorDb = getVectorDbClass();
|
2023-09-28 23:00:03 +02:00
|
|
|
const workspace = await Workspace.get({ id: Number(id) });
|
2023-07-25 19:37:04 +02:00
|
|
|
if (!workspace) {
|
|
|
|
response.sendStatus(404).end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
await WorkspaceChats.delete({ workspaceId: Number(workspace.id) });
|
|
|
|
await DocumentVectors.deleteForWorkspace(Number(workspace.id));
|
|
|
|
await Document.delete({ workspaceId: Number(workspace.id) });
|
|
|
|
await Workspace.delete({ id: Number(workspace.id) });
|
2023-07-25 19:37:04 +02:00
|
|
|
try {
|
|
|
|
await VectorDb["delete-namespace"]({ namespace: workspace.slug });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e.message);
|
|
|
|
}
|
|
|
|
|
2023-09-28 23:00:03 +02:00
|
|
|
response.status(200).json({ success: true, error: null });
|
2023-07-25 19:37:04 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.get(
|
|
|
|
"/admin/system-preferences",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (_request, response) => {
|
2023-07-25 19:37:04 +02:00
|
|
|
try {
|
|
|
|
const settings = {
|
|
|
|
users_can_delete_workspaces:
|
2023-09-28 23:00:03 +02:00
|
|
|
(await SystemSettings.get({ label: "users_can_delete_workspaces" }))
|
2023-07-25 19:37:04 +02:00
|
|
|
?.value === "true",
|
|
|
|
limit_user_messages:
|
2023-09-28 23:00:03 +02:00
|
|
|
(await SystemSettings.get({ label: "limit_user_messages" }))
|
2023-07-25 19:37:04 +02:00
|
|
|
?.value === "true",
|
|
|
|
message_limit:
|
|
|
|
Number(
|
2023-09-28 23:00:03 +02:00
|
|
|
(await SystemSettings.get({ label: "message_limit" }))?.value
|
2023-07-25 19:37:04 +02:00
|
|
|
) || 10,
|
|
|
|
};
|
|
|
|
response.status(200).json({ settings });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/admin/system-preferences",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin, ROLES.manager])],
|
2023-07-25 19:37:04 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const updates = reqBody(request);
|
|
|
|
await SystemSettings.updateSettings(updates);
|
|
|
|
response.status(200).json({ success: true, error: null });
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-08-24 04:15:07 +02:00
|
|
|
|
2023-11-13 23:51:16 +01:00
|
|
|
app.get(
|
|
|
|
"/admin/api-keys",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
|
2023-11-13 23:51:16 +01:00
|
|
|
async (_request, response) => {
|
|
|
|
try {
|
|
|
|
const apiKeys = await ApiKey.whereWithUser({});
|
|
|
|
return response.status(200).json({
|
|
|
|
apiKeys,
|
|
|
|
error: null,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
response.status(500).json({
|
|
|
|
apiKey: null,
|
|
|
|
error: "Could not find an API Keys.",
|
|
|
|
});
|
2023-08-24 04:15:07 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-13 23:51:16 +01:00
|
|
|
);
|
2023-08-24 04:15:07 +02:00
|
|
|
|
|
|
|
app.post(
|
|
|
|
"/admin/generate-api-key",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
|
2023-08-24 04:15:07 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const user = await userFromSession(request, response);
|
|
|
|
const { apiKey, error } = await ApiKey.create(user.id);
|
|
|
|
return response.status(200).json({
|
|
|
|
apiKey,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
app.delete(
|
|
|
|
"/admin/delete-api-key/:id",
|
2024-01-22 23:14:01 +01:00
|
|
|
[validatedRequest, strictMultiUserRoleValid([ROLES.admin])],
|
2023-08-24 04:15:07 +02:00
|
|
|
async (request, response) => {
|
|
|
|
try {
|
|
|
|
const { id } = request.params;
|
2023-09-28 23:00:03 +02:00
|
|
|
await ApiKey.delete({ id: Number(id) });
|
2023-08-24 04:15:07 +02:00
|
|
|
return response.status(200).end();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
response.sendStatus(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2023-07-25 19:37:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { adminEndpoints };
|