Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(auth): revoke members after workspace deletion

Tinf49ed186e59f
diff --git a/apps/api/src/auth.ts b/apps/api/src/auth.ts
index c92b74d21..3614b95a5 100644
--- a/apps/api/src/auth.ts
+++ b/apps/api/src/auth.ts
@@ -207,6 +207,8 @@ function getDeviceAuthVerificationUri(): string {
return `${base}/device`;
}
+const deletedWorkspaceMembers = new WeakMap<object, string[]>();
+
export const auth = betterAuth({
baseURL: baseURLWithoutPath,
trustedOrigins,
@@ -484,7 +486,7 @@ export const auth = betterAuth({
ownerId: user.id,
});
},
- beforeDeleteOrganization: async ({ organization }) => {
+ beforeDeleteOrganization: async ({ organization }, ctx) => {
const billable = await findBillableWorkspaces([organization.id]);
if (billable.length > 0) {
throw new APIError("CONFLICT", {
@@ -493,6 +495,31 @@ export const auth = betterAuth({
),
});
}
+ if (ctx) {
+ const members = await db
+ .select({ userId: schema.workspaceUserTable.userId })
+ .from(schema.workspaceUserTable)
+ .where(
+ eq(schema.workspaceUserTable.workspaceId, organization.id),
+ );
+ deletedWorkspaceMembers.set(
+ ctx.context,
+ members.map((member) => member.userId),
+ );
+ }
+ },
+ afterDeleteOrganization: async ({ organization }, ctx) => {
+ const userIds = ctx
+ ? (deletedWorkspaceMembers.get(ctx.context) ?? [])
+ : [];
+ if (ctx) deletedWorkspaceMembers.delete(ctx.context);
+ await Promise.all(
+ userIds.map((userId) =>
+ revokeWorkspaceConnections(userId, organization.id, {
+ force: true,
+ }),
+ ),
+ );
},
afterAddMember: async ({ member }) => {
if (member?.organizationId) {
diff --git a/tests/api-integration/account-deletion.test.ts b/tests/api-integration/account-deletion.test.ts
index 226984e6a..a2696d486 100644
--- a/tests/api-integration/account-deletion.test.ts
+++ b/tests/api-integration/account-deletion.test.ts
@@ -110,6 +110,47 @@ describe("API integration: account deletion", () => {
}
});
+ it("revokes other members after deleting a workspace through Better Auth", async () => {
+ const owner = await createWorkspaceMember({ role: "owner" });
+ const member = await addMember(owner.workspace.id, "member");
+ const token = `delete-${randomUUID()}`;
+ await db.insert(schema.sessionTable).values({
+ id: randomUUID(),
+ userId: owner.user.id,
+ token,
+ expiresAt: new Date(Date.now() + 60_000),
+ });
+ const ws = { send: vi.fn(), close: vi.fn() };
+ const conn = addConnection(
+ "project",
+ ws as unknown as WSContext,
+ member.id,
+ "window",
+ owner.workspace.id,
+ );
+ try {
+ const response = await auth.handler(
+ new Request("http://localhost:1337/api/auth/organization/delete", {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ authorization: `Bearer ${token}`,
+ },
+ body: JSON.stringify({ organizationId: owner.workspace.id }),
+ }),
+ );
+ expect(response.status).toBe(200);
+ expect(ws.close).toHaveBeenCalledWith(1008, "Workspace access revoked");
+ expect(
+ await db.query.workspaceTable.findFirst({
+ where: eq(schema.workspaceTable.id, owner.workspace.id),
+ }),
+ ).toBeUndefined();
+ } finally {
+ removeConnection("project", conn);
+ }
+ });
+
it("revokes project sockets during account deletion", async () => {
const owner = await createWorkspaceMember({ role: "owner" });
const member = await addMember(owner.workspace.id, "member");