mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-04 10:05:53 +02:00
support postgresql as database option
This commit is contained in:
parent
62a0104e70
commit
2cca561e51
218 changed files with 1417 additions and 713 deletions
|
@ -1,6 +1,6 @@
|
|||
import { ActionsEnum } from "@server/auth/actions";
|
||||
import { db } from "@server/db";
|
||||
import { actions, roles, roleActions } from "../db/schemas/schema";
|
||||
import { actions, roles, roleActions } from "@server/db";
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import logger from "@server/logger";
|
||||
|
||||
|
@ -22,85 +22,37 @@ export async function ensureActions() {
|
|||
.where(eq(roles.isAdmin, true))
|
||||
.execute();
|
||||
|
||||
await db.transaction(async (trx) => {
|
||||
await db.transaction(async (trx) => {
|
||||
// Add new actions
|
||||
for (const actionId of actionsToAdd) {
|
||||
logger.debug(`Adding action: ${actionId}`);
|
||||
await trx.insert(actions).values({ actionId }).execute();
|
||||
// Add new actions to the Default role
|
||||
if (defaultRoles.length != 0) {
|
||||
await trx
|
||||
.insert(roleActions)
|
||||
.values(
|
||||
defaultRoles.map((role) => ({
|
||||
roleId: role.roleId!,
|
||||
actionId,
|
||||
orgId: role.orgId!
|
||||
}))
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
// Add new actions
|
||||
for (const actionId of actionsToAdd) {
|
||||
logger.debug(`Adding action: ${actionId}`);
|
||||
await trx.insert(actions).values({ actionId }).execute();
|
||||
// Add new actions to the Default role
|
||||
if (defaultRoles.length != 0) {
|
||||
// Remove deprecated actions
|
||||
if (actionsToRemove.length > 0) {
|
||||
logger.debug(`Removing actions: ${actionsToRemove.join(", ")}`);
|
||||
await trx
|
||||
.insert(roleActions)
|
||||
.values(
|
||||
defaultRoles.map((role) => ({
|
||||
roleId: role.roleId!,
|
||||
actionId,
|
||||
orgId: role.orgId!
|
||||
}))
|
||||
)
|
||||
.delete(actions)
|
||||
.where(inArray(actions.actionId, actionsToRemove))
|
||||
.execute();
|
||||
await trx
|
||||
.delete(roleActions)
|
||||
.where(inArray(roleActions.actionId, actionsToRemove))
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
// Remove deprecated actions
|
||||
if (actionsToRemove.length > 0) {
|
||||
logger.debug(`Removing actions: ${actionsToRemove.join(", ")}`);
|
||||
await trx
|
||||
.delete(actions)
|
||||
.where(inArray(actions.actionId, actionsToRemove))
|
||||
.execute();
|
||||
await trx
|
||||
.delete(roleActions)
|
||||
.where(inArray(roleActions.actionId, actionsToRemove))
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function createAdminRole(orgId: string) {
|
||||
let roleId: any;
|
||||
await db.transaction(async (trx) => {
|
||||
|
||||
const [insertedRole] = await trx
|
||||
.insert(roles)
|
||||
.values({
|
||||
orgId,
|
||||
isAdmin: true,
|
||||
name: "Admin",
|
||||
description: "Admin role with the most permissions"
|
||||
})
|
||||
.returning({ roleId: roles.roleId })
|
||||
.execute();
|
||||
|
||||
if (!insertedRole || !insertedRole.roleId) {
|
||||
throw new Error("Failed to create Admin role");
|
||||
}
|
||||
|
||||
roleId = insertedRole.roleId;
|
||||
|
||||
const actionIds = await trx.select().from(actions).execute();
|
||||
|
||||
if (actionIds.length === 0) {
|
||||
logger.info("No actions to assign to the Admin role");
|
||||
return;
|
||||
}
|
||||
|
||||
await trx
|
||||
.insert(roleActions)
|
||||
.values(
|
||||
actionIds.map((action) => ({
|
||||
roleId,
|
||||
actionId: action.actionId,
|
||||
orgId
|
||||
}))
|
||||
)
|
||||
.execute();
|
||||
});
|
||||
|
||||
if (!roleId) {
|
||||
throw new Error("Failed to create Admin role");
|
||||
}
|
||||
|
||||
return roleId;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue