Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(dashboard): validate default org without duplicate membership rows

Richie McIlroyfb23e843a00a
diff --git a/apps/web/app/(org)/dashboard/settings/account/server.ts b/apps/web/app/(org)/dashboard/settings/account/server.ts
index 998e3f6db..e2e0c1dec 100644
--- a/apps/web/app/(org)/dashboard/settings/account/server.ts
+++ b/apps/web/app/(org)/dashboard/settings/account/server.ts
@@ -10,7 +10,7 @@ import {
users,
} from "@cap/database/schema";
import type { Organisation } from "@cap/web-domain";
-import { eq, or, sql } from "drizzle-orm";
+import { and, eq, or, sql } from "drizzle-orm";
import { revalidatePath } from "next/cache";
export async function patchAccountSettings(
@@ -30,29 +30,30 @@ export async function patchAccountSettings(
if (firstName !== undefined) updatePayload.name = firstName;
if (lastName !== undefined) updatePayload.lastName = lastName;
if (defaultOrgId !== undefined) {
- const userOrganizations = await db()
+ const [userOrganization] = await db()
.select({
id: organizations.id,
})
.from(organizations)
.leftJoin(
organizationMembers,
- eq(organizations.id, organizationMembers.organizationId),
+ and(
+ eq(organizations.id, organizationMembers.organizationId),
+ eq(organizationMembers.userId, currentUser.id),
+ ),
)
.where(
- or(
- // User owns the organization
- eq(organizations.ownerId, currentUser.id),
- // User is a member of the organization
- eq(organizationMembers.userId, currentUser.id),
+ and(
+ eq(organizations.id, defaultOrgId),
+ or(
+ eq(organizations.ownerId, currentUser.id),
+ eq(organizationMembers.userId, currentUser.id),
+ ),
),
)
- // Remove duplicates if user is both owner and member
- .groupBy(organizations.id);
-
- const userOrgIds = userOrganizations.map((org) => org.id);
+ .limit(1);
- if (!userOrgIds.includes(defaultOrgId))
+ if (!userOrganization)
throw new Error(
"Forbidden: User does not have access to the specified organization",
);