2024-10-06 18:12:27 -04:00
|
|
|
import { ActionsEnum } from "@server/auth/actions";
|
|
|
|
import { db } from "@server/db";
|
2024-12-22 12:33:49 -05:00
|
|
|
import { actions, roles, roleActions } from "../db/schema";
|
|
|
|
import { eq, inArray } from "drizzle-orm";
|
2024-10-13 18:41:15 -04:00
|
|
|
import logger from "@server/logger";
|
2024-10-06 18:12:27 -04:00
|
|
|
|
|
|
|
export async function ensureActions() {
|
|
|
|
const actionIds = Object.values(ActionsEnum);
|
2024-10-10 21:59:30 -04:00
|
|
|
const existingActions = await db.select().from(actions).execute();
|
2024-11-05 22:38:57 -05:00
|
|
|
const existingActionIds = existingActions.map((action) => action.actionId);
|
2024-10-06 18:12:27 -04:00
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
const actionsToAdd = actionIds.filter(
|
|
|
|
(id) => !existingActionIds.includes(id)
|
|
|
|
);
|
|
|
|
const actionsToRemove = existingActionIds.filter(
|
|
|
|
(id) => !actionIds.includes(id as ActionsEnum)
|
|
|
|
);
|
2024-10-10 21:59:30 -04:00
|
|
|
|
|
|
|
const defaultRoles = await db
|
2024-10-06 18:12:27 -04:00
|
|
|
.select()
|
2024-10-10 21:59:30 -04:00
|
|
|
.from(roles)
|
2024-11-05 22:38:57 -05:00
|
|
|
.where(eq(roles.isAdmin, true))
|
2024-10-06 18:12:27 -04:00
|
|
|
.execute();
|
2024-10-10 21:59:30 -04:00
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
await db.transaction(async (trx) => {
|
|
|
|
|
2024-10-10 21:59:30 -04:00
|
|
|
// Add new actions
|
|
|
|
for (const actionId of actionsToAdd) {
|
2024-10-26 17:02:11 -04:00
|
|
|
logger.debug(`Adding action: ${actionId}`);
|
2024-12-24 16:00:02 -05:00
|
|
|
await trx.insert(actions).values({ actionId }).execute();
|
2024-10-10 21:59:30 -04:00
|
|
|
// Add new actions to the Default role
|
2024-10-26 17:02:11 -04:00
|
|
|
if (defaultRoles.length != 0) {
|
2024-12-24 16:00:02 -05:00
|
|
|
await trx
|
2024-11-05 22:38:57 -05:00
|
|
|
.insert(roleActions)
|
|
|
|
.values(
|
|
|
|
defaultRoles.map((role) => ({
|
|
|
|
roleId: role.roleId!,
|
|
|
|
actionId,
|
2024-12-25 15:54:32 -05:00
|
|
|
orgId: role.orgId!
|
2024-11-05 22:38:57 -05:00
|
|
|
}))
|
|
|
|
)
|
2024-10-21 22:13:53 -04:00
|
|
|
.execute();
|
|
|
|
}
|
2024-10-10 21:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deprecated actions
|
|
|
|
if (actionsToRemove.length > 0) {
|
2024-11-05 22:38:57 -05:00
|
|
|
logger.debug(`Removing actions: ${actionsToRemove.join(", ")}`);
|
2024-12-24 16:00:02 -05:00
|
|
|
await trx
|
2024-11-05 22:38:57 -05:00
|
|
|
.delete(actions)
|
|
|
|
.where(inArray(actions.actionId, actionsToRemove))
|
|
|
|
.execute();
|
2024-12-24 16:00:02 -05:00
|
|
|
await trx
|
2024-11-05 22:38:57 -05:00
|
|
|
.delete(roleActions)
|
|
|
|
.where(inArray(roleActions.actionId, actionsToRemove))
|
|
|
|
.execute();
|
2024-10-06 18:12:27 -04:00
|
|
|
}
|
2024-12-24 16:00:02 -05:00
|
|
|
});
|
2024-10-10 21:59:30 -04:00
|
|
|
}
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
export async function createAdminRole(orgId: string) {
|
2024-12-24 16:00:02 -05:00
|
|
|
let roleId: any;
|
|
|
|
await db.transaction(async (trx) => {
|
|
|
|
|
|
|
|
const [insertedRole] = await trx
|
2024-10-10 21:59:30 -04:00
|
|
|
.insert(roles)
|
|
|
|
.values({
|
|
|
|
orgId,
|
2024-11-05 22:38:57 -05:00
|
|
|
isAdmin: true,
|
|
|
|
name: "Admin",
|
2024-12-25 15:54:32 -05:00
|
|
|
description: "Admin role with the most permissions"
|
2024-10-10 21:59:30 -04:00
|
|
|
})
|
|
|
|
.returning({ roleId: roles.roleId })
|
|
|
|
.execute();
|
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
if (!insertedRole || !insertedRole.roleId) {
|
|
|
|
throw new Error("Failed to create Admin role");
|
|
|
|
}
|
|
|
|
|
|
|
|
roleId = insertedRole.roleId;
|
2024-10-10 21:59:30 -04:00
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
const actionIds = await trx.select().from(actions).execute();
|
2024-10-13 22:45:48 -04:00
|
|
|
|
|
|
|
if (actionIds.length === 0) {
|
2024-11-05 22:38:57 -05:00
|
|
|
logger.info("No actions to assign to the Admin role");
|
2024-10-13 22:45:48 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-12-24 16:00:02 -05:00
|
|
|
await trx
|
2024-11-05 22:38:57 -05:00
|
|
|
.insert(roleActions)
|
|
|
|
.values(
|
|
|
|
actionIds.map((action) => ({
|
|
|
|
roleId,
|
|
|
|
actionId: action.actionId,
|
2024-12-25 15:54:32 -05:00
|
|
|
orgId
|
2024-11-05 22:38:57 -05:00
|
|
|
}))
|
|
|
|
)
|
2024-10-10 21:59:30 -04:00
|
|
|
.execute();
|
2024-12-24 16:00:02 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!roleId) {
|
|
|
|
throw new Error("Failed to create Admin role");
|
|
|
|
}
|
2024-10-14 19:30:38 -04:00
|
|
|
|
|
|
|
return roleId;
|
2024-11-05 22:38:57 -05:00
|
|
|
}
|