Files
supabase/apps/studio/components/interfaces/Database/Hooks/DeleteHookModal.tsx
Charis 115a2ffeef feat(studio): gate database functions, triggers, and webhooks on sql grants (#53269)
Resolves FE-4744. Part of the grants rollout; builds on the read/write
SQL variants from #53185.

## What

- **Data layer:** the database functions, triggers, database webhooks,
and event triggers queries now take a `read` grant (exposed as
`queryOptions` factories). All 8 create/update/delete mutations take a
`write` grant and mark their SQL with `asWriteSql`.
- **Guards:** the hand-written FGA checks in the functions, triggers,
event triggers, and database webhooks lists, the functions page, the
triggers layout/route, and the webhooks integration tab now derive from
grants, with denied and error states.
- **Other consumers** updated to pass grants: `CreateFunction`,
`TriggerSheet`, `EditHookPanel`, `DeleteHookModal`,
`ChooseFunctionForm`, `FunctionSelector`, `AutoEnableRLSNotice`, and SQL
editor definition/autocomplete hooks.

## Behavior changes

- **Webhooks integration tab:** was gated on
`database_webhooks_config_read`; now gated on `database_read`, which is
what the backend checks for the pg-meta query it uses. Both permissions
are granted to the same built-in roles, so there is no change for
built-in roles. A permissions-load failure now shows an error alert
instead of the content.
- **`AutoEnableRLSNotice`:** hidden unless the user has read access,
since it can't tell whether the trigger already exists without it.

## Out of scope

- User-authored SQL execution in `QueryEditor` / `useSqlEditorExecution`
stays `unguarded.migrating` (FE-4750).

## Testing

- New tests: query factories and write mutations
(`data/database-sql-grants.test.ts`), webhooks tab granted/denied/error,
functions list read/write granted/denied/error.
- Typecheck, Prettier, lint ratchet, and Studio vitest suites for
affected areas pass.
2026-11-12 14:58:01 -05:00

96 lines
3.1 KiB
TypeScript

import { useQuery } from '@tanstack/react-query'
import { parseAsString, useQueryState } from 'nuqs'
import { useEffect } from 'react'
import { toast } from 'sonner'
import { TextConfirmModal } from '@/components/ui/TextConfirmModalWrapper'
import { useDatabaseTriggerDeleteMutation } from '@/data/database-triggers/database-trigger-delete-mutation'
import { databaseHooksQueryOptions } from '@/data/database-triggers/database-triggers-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { getWriteGrantToastMessage, useGrant } from '@/lib/grants'
export const DeleteHookModal = () => {
const { data: project } = useSelectedProjectQuery()
const readGrant = useGrant(
'POST /platform/pg-meta/{ref}/query',
{ projectRef: project?.ref },
{ variant: 'read' }
)
const writeGrant = useGrant(
'POST /platform/pg-meta/{ref}/query',
{ projectRef: project?.ref },
{ variant: 'write' }
)
const { data: hooks = [], isSuccess } = useQuery(
databaseHooksQueryOptions({
projectRef: project?.ref,
connectionString: project?.connectionString,
grant: readGrant.grant,
})
)
const [selectedHookIdToDelete, setSelectedHookIdToDelete] = useQueryState(
'delete',
parseAsString.withDefault('')
)
const selectedHook = hooks.find((hook) => hook.id.toString() === selectedHookIdToDelete)
const { name, schema } = selectedHook ?? {}
const {
mutate: deleteDatabaseTrigger,
isPending: isDeleting,
isSuccess: isSuccessDelete,
} = useDatabaseTriggerDeleteMutation({
onSuccess: () => {
toast.success(`Successfully deleted ${name}`)
setSelectedHookIdToDelete(null)
},
})
async function handleDelete() {
if (!project) return console.error('Project ref is required')
if (!selectedHook) return toast.error('Unable to find selected hook')
if (!writeGrant.grant) {
return toast.error(getWriteGrantToastMessage(writeGrant.status, 'delete', 'webhooks'))
}
deleteDatabaseTrigger({
grant: writeGrant.grant,
trigger: selectedHook,
projectRef: project.ref,
connectionString: project.connectionString,
})
}
useEffect(() => {
if (isSuccess && !!selectedHookIdToDelete && !selectedHook && !isSuccessDelete) {
toast('Webhook not found')
setSelectedHookIdToDelete(null)
}
}, [isSuccess, isSuccessDelete, selectedHook, selectedHookIdToDelete, setSelectedHookIdToDelete])
return (
<TextConfirmModal
variant="destructive"
visible={!!selectedHook}
size="small"
onCancel={() => setSelectedHookIdToDelete(null)}
onConfirm={handleDelete}
title="Delete database webhook"
loading={isDeleting}
confirmLabel={`Delete ${name}`}
confirmPlaceholder="Type in name of webhook"
confirmString={name || ''}
text={
<>
This will delete the webhook <span className="text-bold text-foreground">{name}</span>{' '}
from the schema <span className="text-bold text-foreground">{schema}</span>
</>
}
alert={{ title: 'You cannot recover this webhook once deleted.' }}
/>
)
}