feat: prevent OAuth client creation for non platform orgs (#14200)

* feat: isPlatform flag for teams

* feat: create organizations service to check if org is platform

* feat: check if organization is platform in the OrganizationRolesGuard used by OAuthClientsController

* remove unused variable

* refactor: reduce nesting in create handler and abstract check in function

* feat: dont create domain for platform orgs
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2024-03-25 12:21:41 +01:00
1 parent b520238d33
commit 070548555c
10 files changed
+239 -115

No files matched your search

@@ -36,7 +36,7 @@ const getIPAddress = async (url: string): Promise<string> => {
};
export const createHandler = async ({ input, ctx }: CreateOptions) => {
const { slug, name, adminEmail, adminUsername, check } = input;
const { slug, name, adminEmail, adminUsername, check, isPlatform } = input;
const userCollisions = await prisma.user.findUnique({
where: {
@@ -69,121 +69,120 @@ export const createHandler = async ({ input, ctx }: CreateOptions) => {
const t = await getTranslation(ctx.user.locale ?? "en", "common");
const availability = getAvailabilityFromSchedule(DEFAULT_SCHEDULE);
let isOrganizationConfigured = false;
if (check === false) {
isOrganizationConfigured = await createDomain(slug);
if (check) {
await checkOrg(adminEmail, input.language);
return { checked: true };
}
if (!isOrganizationConfigured) {
// Otherwise, we proceed to send an administrative email to admins regarding
// the need to configure DNS registry to support the newly created org
const instanceAdmins = await prisma.user.findMany({
where: { role: UserPermissionRole.ADMIN },
select: { email: true },
const isOrganizationConfigured = isPlatform ? true : await createDomain(slug);
if (!isOrganizationConfigured) {
// Otherwise, we proceed to send an administrative email to admins regarding
// the need to configure DNS registry to support the newly created org
const instanceAdmins = await prisma.user.findMany({
where: { role: UserPermissionRole.ADMIN },
select: { email: true },
});
if (instanceAdmins.length) {
await sendAdminOrganizationNotification({
instanceAdmins,
orgSlug: slug,
ownerEmail: adminEmail,
webappIPAddress: await getIPAddress(
WEBAPP_URL.replace("https://", "")?.replace("http://", "").replace(/(:.*)/, "")
),
t,
});
if (instanceAdmins.length) {
await sendAdminOrganizationNotification({
instanceAdmins,
orgSlug: slug,
ownerEmail: adminEmail,
webappIPAddress: await getIPAddress(
WEBAPP_URL.replace("https://", "")?.replace("http://", "").replace(/(:.*)/, "")
),
t,
});
} else {
console.warn("Organization created: subdomain not configured and couldn't notify adminnistrators");
}
} else {
console.warn("Organization created: subdomain not configured and couldn't notify adminnistrators");
}
}
const { user: createOwnerOrg, organization } = await prisma.$transaction(async (tx) => {
const organization = await tx.team.create({
data: {
name,
isOrganization: true,
...(!IS_TEAM_BILLING_ENABLED ? { slug } : {}),
organizationSettings: {
create: {
isOrganizationVerified: true,
isOrganizationConfigured,
orgAutoAcceptEmail: emailDomain,
},
},
metadata: {
...(IS_TEAM_BILLING_ENABLED ? { requestedSlug: slug } : {}),
const { user: createOwnerOrg, organization } = await prisma.$transaction(async (tx) => {
const organization = await tx.team.create({
data: {
name,
isOrganization: true,
...(!IS_TEAM_BILLING_ENABLED ? { slug } : {}),
organizationSettings: {
create: {
isOrganizationVerified: true,
isOrganizationConfigured,
orgAutoAcceptEmail: emailDomain,
},
},
});
metadata: {
...(IS_TEAM_BILLING_ENABLED ? { requestedSlug: slug } : {}),
},
},
});
const user = await tx.user.create({
data: {
username: slugify(adminUsername),
email: adminEmail,
emailVerified: new Date(),
password: { create: { hash: hashedPassword } },
organizationId: organization.id,
// Default schedule
schedules: {
create: {
name: t("default_schedule_name"),
availability: {
createMany: {
data: availability.map((schedule) => ({
days: schedule.days,
startTime: schedule.startTime,
endTime: schedule.endTime,
})),
},
const user = await tx.user.create({
data: {
username: slugify(adminUsername),
email: adminEmail,
emailVerified: new Date(),
password: { create: { hash: hashedPassword } },
organizationId: organization.id,
// Default schedule
schedules: {
create: {
name: t("default_schedule_name"),
availability: {
createMany: {
data: availability.map((schedule) => ({
days: schedule.days,
startTime: schedule.startTime,
endTime: schedule.endTime,
})),
},
},
},
profiles: {
create: {
username: slugify(adminUsername),
organizationId: organization.id,
uid: ProfileRepository.generateProfileUid(),
},
},
profiles: {
create: {
username: slugify(adminUsername),
organizationId: organization.id,
uid: ProfileRepository.generateProfileUid(),
},
},
});
await tx.membership.create({
data: {
userId: user.id,
role: MembershipRole.OWNER,
accepted: true,
teamId: organization.id,
},
});
return { user, organization };
});
if (!organization.id) throw Error("User not created");
return { user: { ...createOwnerOrg, organizationId: organization.id, password } };
} else {
const language = await getTranslation(input.language ?? "en", "common");
const secret = createHash("md5")
.update(adminEmail + process.env.CALENDSO_ENCRYPTION_KEY)
.digest("hex");
totp.options = { step: 900 };
const code = totp.generate(secret);
await sendOrganizationEmailVerification({
user: {
email: adminEmail,
},
code,
language,
});
}
// Sync Services: Close.com
//closeComUpsertOrganizationUser(createTeam, ctx.user, MembershipRole.OWNER);
await tx.membership.create({
data: {
userId: user.id,
role: MembershipRole.OWNER,
accepted: true,
teamId: organization.id,
},
});
return { user, organization };
});
return { checked: true };
if (!organization.id) throw Error("User not created");
return { user: { ...createOwnerOrg, organizationId: organization.id, password } };
};
async function checkOrg(adminEmail: string, inputLanguage?: string) {
const language = await getTranslation(inputLanguage ?? "en", "common");
const secret = createHash("md5")
.update(adminEmail + process.env.CALENDSO_ENCRYPTION_KEY)
.digest("hex");
totp.options = { step: 900 };
const code = totp.generate(secret);
await sendOrganizationEmailVerification({
user: {
email: adminEmail,
},
code,
language,
});
}
export default createHandler;
@@ -9,6 +9,7 @@ export const ZCreateInputSchema = z.object({
adminUsername: z.string(),
check: z.boolean().default(true),
language: z.string().optional(),
isPlatform: z.boolean().default(false),
});
export type TCreateInputSchema = z.infer<typeof ZCreateInputSchema>;