mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
fix(database): authorize soft-delete by agent org + add onError + guard bulk-restore lists
This commit is contained in:
@@ -24,6 +24,9 @@ export const DatabaseDeleteButton = (props: DatabaseDeleteButtonProps) => {
|
|||||||
toast.error(result.data?.actionError?.message || "Unknown error occurred.");
|
toast.error(result.data?.actionError?.message || "Unknown error occurred.");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
onError: () => {
|
||||||
|
toast.error("Failed to delete database.");
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export async function assertDatabasesInOrgProject(projectId: string, databaseIds
|
|||||||
eq(drizzleDb.schemas.project.id, projectId),
|
eq(drizzleDb.schemas.project.id, projectId),
|
||||||
eq(drizzleDb.schemas.project.organizationId, organization.id),
|
eq(drizzleDb.schemas.project.organizationId, organization.id),
|
||||||
),
|
),
|
||||||
with: {databases: true},
|
with: {databases: {where: isNull(drizzleDb.schemas.database.deletedAt)}},
|
||||||
});
|
});
|
||||||
if (!project) throw new ActionError("Project not found.");
|
if (!project) throw new ActionError("Project not found.");
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ export type RestorePreviewRow = {
|
|||||||
async function resolveLatestRestorable(projectId: string, databaseIds: string[]): Promise<RestorePreviewRow[]> {
|
async function resolveLatestRestorable(projectId: string, databaseIds: string[]): Promise<RestorePreviewRow[]> {
|
||||||
const project = await db.query.project.findFirst({
|
const project = await db.query.project.findFirst({
|
||||||
where: eq(drizzleDb.schemas.project.id, projectId),
|
where: eq(drizzleDb.schemas.project.id, projectId),
|
||||||
with: {databases: true},
|
with: {databases: {where: isNull(drizzleDb.schemas.database.deletedAt)}},
|
||||||
});
|
});
|
||||||
const nameById = new Map((project?.databases ?? []).map((d) => [d.id, d.name] as const));
|
const nameById = new Map((project?.databases ?? []).map((d) => [d.id, d.name] as const));
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,9 @@ import {ServerActionResult} from "@/types/action-type";
|
|||||||
import {userAction} from "@/lib/safe-actions/actions";
|
import {userAction} from "@/lib/safe-actions/actions";
|
||||||
import {zString} from "@/lib/zod";
|
import {zString} from "@/lib/zod";
|
||||||
import {withUpdatedAt} from "@/db/utils";
|
import {withUpdatedAt} from "@/db/utils";
|
||||||
|
import {currentUser} from "@/lib/auth/current-user";
|
||||||
|
import {getActiveMember, getOrganization} from "@/lib/auth/auth";
|
||||||
|
import {computeOrganizationPermissions} from "@/lib/acl/organization-acl";
|
||||||
|
|
||||||
type DeleteDatabaseInput = {
|
type DeleteDatabaseInput = {
|
||||||
databaseId: string;
|
databaseId: string;
|
||||||
@@ -21,9 +24,63 @@ class DatabaseNotFoundError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class UnauthorizedError extends Error {
|
||||||
|
constructor(databaseId: string) {
|
||||||
|
super(`Not authorized to delete this database: ${databaseId}`);
|
||||||
|
this.name = "UnauthorizedError";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!database || !database.agent) {
|
||||||
|
throw new DatabaseNotFoundError(databaseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await currentUser();
|
||||||
|
if (!user) {
|
||||||
|
throw new UnauthorizedError(databaseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
const agent = database.agent;
|
||||||
|
const isAdmin = user.role === "superadmin" || user.role === "admin";
|
||||||
|
|
||||||
|
let authorized: boolean;
|
||||||
|
if (agent.organizationId === null) {
|
||||||
|
authorized = isAdmin;
|
||||||
|
} else {
|
||||||
|
const organization = await getOrganization({});
|
||||||
|
const activeMember = await getActiveMember();
|
||||||
|
const canManage = activeMember
|
||||||
|
? computeOrganizationPermissions(activeMember).canManageAgents
|
||||||
|
: false;
|
||||||
|
const hasAccess =
|
||||||
|
!!organization &&
|
||||||
|
(agent.organizationId === organization.id ||
|
||||||
|
agent.organizations.some((o) => o.organizationId === organization.id));
|
||||||
|
authorized = canManage && hasAccess;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!authorized) {
|
||||||
|
throw new UnauthorizedError(databaseId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteDatabaseService(input: DeleteDatabaseInput): Promise<Database> {
|
export async function deleteDatabaseService(input: DeleteDatabaseInput): Promise<Database> {
|
||||||
const {databaseId} = input;
|
const {databaseId} = input;
|
||||||
|
|
||||||
|
await assertCanDeleteDatabase(databaseId);
|
||||||
|
|
||||||
await db
|
await db
|
||||||
.delete(drizzleDb.schemas.retentionPolicy)
|
.delete(drizzleDb.schemas.retentionPolicy)
|
||||||
.where(eq(drizzleDb.schemas.retentionPolicy.databaseId, databaseId))
|
.where(eq(drizzleDb.schemas.retentionPolicy.databaseId, databaseId))
|
||||||
@@ -81,6 +138,19 @@ export const deleteDatabaseAction = userAction
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (error instanceof UnauthorizedError) {
|
||||||
|
return {
|
||||||
|
success: false,
|
||||||
|
actionError: {
|
||||||
|
message: "Not authorized to delete this database.",
|
||||||
|
status: 403,
|
||||||
|
messageParams: {
|
||||||
|
databaseId: parsedInput.databaseId,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (error instanceof DatabaseNotFoundError) {
|
if (error instanceof DatabaseNotFoundError) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user