Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web-backend): add organization admin policy and membership lookups

Richie McIlroy06d15a7de518
diff --git a/packages/web-backend/src/Organisations/OrganisationsPolicy.ts b/packages/web-backend/src/Organisations/OrganisationsPolicy.ts
index da7a34e92..fb2203e20 100644
--- a/packages/web-backend/src/Organisations/OrganisationsPolicy.ts
+++ b/packages/web-backend/src/Organisations/OrganisationsPolicy.ts
@@ -28,7 +28,19 @@ export class OrganisationsPolicy extends Effect.Service<OrganisationsPolicy>()(
),
);
- return { isMember, isOwner };
+ const isAdminOrOwner = (orgId: Organisation.OrganisationId) =>
+ Policy.policy((user) =>
+ repo.membership(user.id, orgId).pipe(
+ Effect.map((v) =>
+ v.pipe(
+ Option.filter((v) => v.role === "owner" || v.role === "admin"),
+ Option.isSome,
+ ),
+ ),
+ ),
+ );
+
+ return { isMember, isOwner, isAdminOrOwner };
}),
dependencies: [
OrganisationsRepo.Default,
diff --git a/packages/web-backend/src/Organisations/OrganisationsRepo.ts b/packages/web-backend/src/Organisations/OrganisationsRepo.ts
index 73c75ccc1..19db18843 100644
--- a/packages/web-backend/src/Organisations/OrganisationsRepo.ts
+++ b/packages/web-backend/src/Organisations/OrganisationsRepo.ts
@@ -37,17 +37,44 @@ export class OrganisationsRepo extends Effect.Service<OrganisationsRepo>()(
db
.select({
membershipId: Db.organizationMembers.id,
+ ownerId: Db.organizations.ownerId,
role: Db.organizationMembers.role,
})
- .from(Db.organizationMembers)
- .where(
+ .from(Db.organizations)
+ .leftJoin(
+ Db.organizationMembers,
Dz.and(
+ Dz.eq(
+ Db.organizationMembers.organizationId,
+ Db.organizations.id,
+ ),
Dz.eq(Db.organizationMembers.userId, userId),
- Dz.eq(Db.organizationMembers.organizationId, orgId),
+ ),
+ )
+ .where(
+ Dz.and(
+ Dz.eq(Db.organizations.id, orgId),
+ Dz.or(
+ Dz.eq(Db.organizations.ownerId, userId),
+ Dz.eq(Db.organizationMembers.userId, userId),
+ ),
),
),
)
- .pipe(Effect.map(Array.get(0))),
+ .pipe(
+ Effect.map(Array.get(0)),
+ Effect.map(
+ Option.map((row) => ({
+ membershipId: row.membershipId,
+ role:
+ row.ownerId === userId
+ ? ("owner" as const)
+ : row.role === "owner"
+ ? ("member" as const)
+ : row.role,
+ })),
+ ),
+ ),
allowedEmailDomain: (orgId: Organisation.OrganisationId) =>
db
.use((db) =>
diff --git a/packages/web-backend/src/Organisations/index.ts b/packages/web-backend/src/Organisations/index.ts
index 0e6c22bc2..7ebf594bc 100644
--- a/packages/web-backend/src/Organisations/index.ts
+++ b/packages/web-backend/src/Organisations/index.ts
@@ -31,7 +31,7 @@ export class Organisations extends Effect.Service<Organisations>()(
"NoSuchElementException",
() => new Organisation.NotFoundError(),
),
- Policy.withPolicy(policy.isOwner(payload.id)),
+ Policy.withPolicy(policy.isAdminOrOwner(payload.id)),
);
if (payload.image) {
@@ -55,7 +55,6 @@ export class Organisations extends Effect.Service<Organisations>()(
yield* Policy.withPolicy(policy.isOwner(id))(Effect.void);
- // Perform tombstone, find other org, and update user in a single transaction
yield* db.use((db) =>
db.transaction(async (tx) => {
await tx
@@ -63,7 +62,6 @@ export class Organisations extends Effect.Service<Organisations>()(
.set({ tombstoneAt: new Date() })
.where(Dz.eq(Db.organizations.id, id));
- // Find another active organization owned by the user
const [otherOrg] = await tx
.select({ id: Db.organizations.id })
.from(Db.organizations)