2024-11-05 22:38:57 -05:00
|
|
|
import { Request, Response, NextFunction } from "express";
|
|
|
|
import { z } from "zod";
|
|
|
|
import { db } from "@server/db";
|
|
|
|
import { eq } from "drizzle-orm";
|
2025-02-16 17:28:10 -05:00
|
|
|
import {
|
2025-05-02 10:44:50 -04:00
|
|
|
apiKeyOrg,
|
|
|
|
apiKeys,
|
2025-02-16 17:28:10 -05:00
|
|
|
domains,
|
|
|
|
Org,
|
|
|
|
orgDomains,
|
|
|
|
orgs,
|
|
|
|
roleActions,
|
|
|
|
roles,
|
2025-05-02 10:44:50 -04:00
|
|
|
userOrgs,
|
2025-06-04 12:02:07 -04:00
|
|
|
users,
|
|
|
|
actions
|
|
|
|
} from "@server/db";
|
2025-01-01 21:41:31 -05:00
|
|
|
import response from "@server/lib/response";
|
2024-11-05 22:38:57 -05:00
|
|
|
import HttpCode from "@server/types/HttpCode";
|
|
|
|
import createHttpError from "http-errors";
|
|
|
|
import logger from "@server/logger";
|
2025-01-01 21:41:31 -05:00
|
|
|
import config from "@server/lib/config";
|
2024-11-05 22:38:57 -05:00
|
|
|
import { fromError } from "zod-validation-error";
|
2024-11-09 23:59:19 -05:00
|
|
|
import { defaultRoleAllowedActions } from "../role";
|
2025-04-06 22:44:14 -04:00
|
|
|
import { OpenAPITags, registry } from "@server/openApi";
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-11-14 00:00:17 -05:00
|
|
|
const createOrgSchema = z
|
|
|
|
.object({
|
|
|
|
orgId: z.string(),
|
2024-12-21 14:11:10 -05:00
|
|
|
name: z.string().min(1).max(255)
|
2024-11-14 00:00:17 -05:00
|
|
|
})
|
|
|
|
.strict();
|
2024-10-01 21:34:07 -04:00
|
|
|
|
2025-03-26 22:20:22 -04:00
|
|
|
// const MAX_ORGS = 5;
|
2024-10-03 22:31:20 -04:00
|
|
|
|
2025-04-06 22:44:14 -04:00
|
|
|
registry.registerPath({
|
|
|
|
method: "put",
|
|
|
|
path: "/org",
|
|
|
|
description: "Create a new organization",
|
|
|
|
tags: [OpenAPITags.Org],
|
|
|
|
request: {
|
|
|
|
body: {
|
|
|
|
content: {
|
|
|
|
"application/json": {
|
|
|
|
schema: createOrgSchema
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
responses: {}
|
|
|
|
});
|
|
|
|
|
2024-11-05 22:38:57 -05:00
|
|
|
export async function createOrg(
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
|
|
|
): Promise<any> {
|
2024-10-06 18:05:20 -04:00
|
|
|
try {
|
2024-12-25 15:54:32 -05:00
|
|
|
// should this be in a middleware?
|
2025-01-01 17:50:12 -05:00
|
|
|
if (config.getRawConfig().flags?.disable_user_create_org) {
|
2025-05-02 10:44:50 -04:00
|
|
|
if (req.user && !req.user?.serverAdmin) {
|
2024-12-25 15:54:32 -05:00
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.FORBIDDEN,
|
|
|
|
"Only server admins can create organizations"
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
const parsedBody = createOrgSchema.safeParse(req.body);
|
|
|
|
if (!parsedBody.success) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.BAD_REQUEST,
|
2024-11-03 13:57:51 -05:00
|
|
|
fromError(parsedBody.error).toString()
|
2024-10-06 18:05:20 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
const { orgId, name } = parsedBody.data;
|
2024-10-06 16:43:59 -04:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
// make sure the orgId is unique
|
2024-11-05 22:38:57 -05:00
|
|
|
const orgExists = await db
|
|
|
|
.select()
|
2024-10-14 19:30:38 -04:00
|
|
|
.from(orgs)
|
|
|
|
.where(eq(orgs.orgId, orgId))
|
|
|
|
.limit(1);
|
2024-11-05 22:38:57 -05:00
|
|
|
|
2024-10-14 19:30:38 -04:00
|
|
|
if (orgExists.length > 0) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.CONFLICT,
|
|
|
|
`Organization with ID ${orgId} already exists`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 14:11:10 -05:00
|
|
|
let error = "";
|
|
|
|
let org: Org | null = null;
|
2024-10-26 12:15:03 -04:00
|
|
|
|
2024-12-21 14:11:10 -05:00
|
|
|
await db.transaction(async (trx) => {
|
2025-02-16 18:09:17 -05:00
|
|
|
const allDomains = await trx
|
|
|
|
.select()
|
|
|
|
.from(domains)
|
|
|
|
.where(eq(domains.configManaged, true));
|
2024-10-01 21:53:49 -04:00
|
|
|
|
2024-12-21 14:11:10 -05:00
|
|
|
const newOrg = await trx
|
|
|
|
.insert(orgs)
|
|
|
|
.values({
|
|
|
|
orgId,
|
2025-02-16 17:28:10 -05:00
|
|
|
name
|
2024-12-21 14:11:10 -05:00
|
|
|
})
|
|
|
|
.returning();
|
2024-10-14 19:30:38 -04:00
|
|
|
|
2024-12-21 14:11:10 -05:00
|
|
|
if (newOrg.length === 0) {
|
|
|
|
error = "Failed to create organization";
|
|
|
|
trx.rollback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
org = newOrg[0];
|
|
|
|
|
2025-06-04 12:02:07 -04:00
|
|
|
// Create admin role within the same transaction
|
|
|
|
const [insertedRole] = await trx
|
|
|
|
.insert(roles)
|
|
|
|
.values({
|
|
|
|
orgId: newOrg[0].orgId,
|
|
|
|
isAdmin: true,
|
|
|
|
name: "Admin",
|
|
|
|
description: "Admin role with the most permissions"
|
|
|
|
})
|
|
|
|
.returning({ roleId: roles.roleId });
|
2024-10-14 19:30:38 -04:00
|
|
|
|
2025-06-04 12:02:07 -04:00
|
|
|
if (!insertedRole || !insertedRole.roleId) {
|
2024-12-21 14:11:10 -05:00
|
|
|
error = "Failed to create Admin role";
|
|
|
|
trx.rollback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-06-04 12:02:07 -04:00
|
|
|
const roleId = insertedRole.roleId;
|
|
|
|
|
|
|
|
// Get all actions and create role actions
|
|
|
|
const actionIds = await trx.select().from(actions).execute();
|
|
|
|
|
|
|
|
if (actionIds.length > 0) {
|
|
|
|
await trx
|
|
|
|
.insert(roleActions)
|
|
|
|
.values(
|
|
|
|
actionIds.map((action) => ({
|
|
|
|
roleId,
|
|
|
|
actionId: action.actionId,
|
|
|
|
orgId: newOrg[0].orgId
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2025-02-16 17:28:10 -05:00
|
|
|
await trx.insert(orgDomains).values(
|
|
|
|
allDomains.map((domain) => ({
|
|
|
|
orgId: newOrg[0].orgId,
|
|
|
|
domainId: domain.domainId
|
|
|
|
}))
|
|
|
|
);
|
|
|
|
|
2025-05-02 10:44:50 -04:00
|
|
|
if (req.user) {
|
|
|
|
await trx.insert(userOrgs).values({
|
|
|
|
userId: req.user!.userId,
|
|
|
|
orgId: newOrg[0].orgId,
|
|
|
|
roleId: roleId,
|
|
|
|
isOwner: true
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// if org created by root api key, set the server admin as the owner
|
|
|
|
const [serverAdmin] = await trx
|
|
|
|
.select()
|
|
|
|
.from(users)
|
|
|
|
.where(eq(users.serverAdmin, true));
|
|
|
|
|
|
|
|
if (!serverAdmin) {
|
|
|
|
error = "Server admin not found";
|
|
|
|
trx.rollback();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await trx.insert(userOrgs).values({
|
|
|
|
userId: serverAdmin.userId,
|
|
|
|
orgId: newOrg[0].orgId,
|
|
|
|
roleId: roleId,
|
|
|
|
isOwner: true
|
|
|
|
});
|
|
|
|
}
|
2024-12-21 14:11:10 -05:00
|
|
|
|
|
|
|
const memberRole = await trx
|
|
|
|
.insert(roles)
|
|
|
|
.values({
|
|
|
|
name: "Member",
|
|
|
|
description: "Members can only view resources",
|
|
|
|
orgId
|
|
|
|
})
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
await trx.insert(roleActions).values(
|
2024-11-09 23:59:19 -05:00
|
|
|
defaultRoleAllowedActions.map((action) => ({
|
|
|
|
roleId: memberRole[0].roleId,
|
|
|
|
actionId: action,
|
2024-12-21 14:11:10 -05:00
|
|
|
orgId
|
2024-11-09 23:59:19 -05:00
|
|
|
}))
|
2024-12-21 14:11:10 -05:00
|
|
|
);
|
2025-05-02 10:44:50 -04:00
|
|
|
|
|
|
|
const rootApiKeys = await trx
|
|
|
|
.select()
|
|
|
|
.from(apiKeys)
|
|
|
|
.where(eq(apiKeys.isRoot, true));
|
|
|
|
|
|
|
|
for (const apiKey of rootApiKeys) {
|
|
|
|
await trx.insert(apiKeyOrg).values({
|
|
|
|
apiKeyId: apiKey.apiKeyId,
|
|
|
|
orgId: newOrg[0].orgId
|
|
|
|
});
|
|
|
|
}
|
2024-12-21 14:11:10 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!org) {
|
|
|
|
return next(
|
|
|
|
createHttpError(
|
|
|
|
HttpCode.INTERNAL_SERVER_ERROR,
|
2025-06-04 12:02:07 -04:00
|
|
|
"Failed to create org"
|
2024-12-21 14:11:10 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, error));
|
|
|
|
}
|
2024-11-09 23:59:19 -05:00
|
|
|
|
2024-10-06 18:05:20 -04:00
|
|
|
return response(res, {
|
2024-12-21 14:11:10 -05:00
|
|
|
data: org,
|
2024-10-06 18:05:20 -04:00
|
|
|
success: true,
|
|
|
|
error: false,
|
|
|
|
message: "Organization created successfully",
|
2024-12-21 14:11:10 -05:00
|
|
|
status: HttpCode.CREATED
|
2024-10-06 18:05:20 -04:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(error);
|
2024-11-05 22:38:57 -05:00
|
|
|
return next(
|
2024-11-09 00:08:17 -05:00
|
|
|
createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred")
|
2024-11-05 22:38:57 -05:00
|
|
|
);
|
2024-10-06 18:05:20 -04:00
|
|
|
}
|
2024-10-02 00:04:40 -04:00
|
|
|
}
|