fix(database): restrict db delete to agent-owning org (system agents admin-only)

- UI: delete button gated by canDeleteDatabases prop threaded from each page
  (org page: only org-created agents; admin page: system agents)
- authz: org path requires agent.organizationId === active org; drop join-based
  attribution so a system agent merely linked to an org is admin-delete-only
This commit is contained in:
charles-gauthereau
2026-07-05 18:31:52 +02:00
parent cd72cfbff4
commit df692c4729
5 changed files with 23 additions and 20 deletions
@@ -35,11 +35,7 @@ async function assertCanDeleteDatabase(databaseId: string): Promise<void> {
const database = await db.query.database.findFirst({
where: eq(drizzleDb.schemas.database.id, databaseId),
with: {
agent: {
with: {
organizations: true,
},
},
agent: true,
},
});
@@ -64,10 +60,11 @@ async function assertCanDeleteDatabase(databaseId: string): Promise<void> {
const canManage = activeMember
? computeOrganizationPermissions(activeMember).canManageAgents
: false;
// Only the organization that CREATED the agent may delete its databases.
// A system agent merely attributed to an org via the join table is
// handled by the isAdmin branch above (agent.organizationId === null).
const hasAccess =
!!organization &&
(agent.organizationId === organization.id ||
agent.organizations.some((o) => o.organizationId === organization.id));
!!organization && agent.organizationId === organization.id;
authorized = canManage && hasAccess;
}