Skip to content

Commit c87d028

Browse files
committed
refactor(api): update organization ownership checks in membership management
1 parent b39e29b commit c87d028

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

apps/api/src/db/queries.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,13 +1717,19 @@ export async function isOrganizationOwner(
17171717
organizationId: string,
17181718
userId: string
17191719
): Promise<boolean> {
1720-
const [user] = await db
1720+
const [membership] = await db
17211721
.select()
1722-
.from(users)
1723-
.where(eq(users.id, userId))
1722+
.from(memberships)
1723+
.where(
1724+
and(
1725+
eq(memberships.userId, userId),
1726+
eq(memberships.organizationId, organizationId),
1727+
eq(memberships.role, OrganizationRole.OWNER)
1728+
)
1729+
)
17241730
.limit(1);
17251731

1726-
return user?.organizationId === organizationId;
1732+
return !!membership;
17271733
}
17281734

17291735
/**
@@ -1810,7 +1816,8 @@ export async function addOrUpdateMembership(
18101816
const targetUserId = targetUser.id;
18111817

18121818
// Prevent adding the organization owner as a member (they're already the owner)
1813-
if (targetUser.organizationId === organizationId) {
1819+
const isTargetUserOwner = await isOrganizationOwner(db, organizationId, targetUserId);
1820+
if (isTargetUserOwner) {
18141821
return null; // Cannot add/change role of the organization owner
18151822
}
18161823

@@ -1937,7 +1944,8 @@ export async function deleteMembership(
19371944
const targetUserId = targetUser.id;
19381945

19391946
// Prevent removing the organization owner
1940-
if (targetUser.organizationId === organizationId) {
1947+
const isTargetUserOwner = await isOrganizationOwner(db, organizationId, targetUserId);
1948+
if (isTargetUserOwner) {
19411949
return false; // Cannot remove the organization owner
19421950
}
19431951

0 commit comments

Comments
 (0)