mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Problem Migrating Studio's permission checks from the old RLS json logic to openfga backed permissions. ### New hooks and helpers - `usePermissionsQueryV2` fetches the new profile openfga permisisons - `useAsyncCheckPermissionsV2` the replacement for the old check hook; takes an FGA permission string (or a list) - `FGA_PERMISSIONS` permission constants derived from `@supabase/shared-types`. At the moment, the hook also accepts plain strings for permissions not yet published in the package - `useAsyncCheckUserContentPermissions` new hook for permission checks on user content (SQL snippets, reports, log snippets). This wraps ``useAsyncCheckPermissionsV2` and adds existing content level rules on top of the fga permission check ### Notes - ~~Need api to support new permissions: project_operations_write, billing_read/write, audit_logs_read~~ [DONE] - ~~Still need to support: organization_config_read/write, auth hooks and users permissions~~ [DONE] --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
109 lines
3.8 KiB
TypeScript
109 lines
3.8 KiB
TypeScript
import type { PGPublication, PGTable } from '@supabase/pg-meta'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Badge, Switch, TableCell, TableRow, Tooltip, TooltipContent, TooltipTrigger } from 'ui'
|
|
|
|
import { useDatabasePublicationUpdateMutation } from '@/data/database-publications/database-publications-update-mutation'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { useProtectedSchemas } from '@/hooks/useProtectedSchemas'
|
|
|
|
interface PublicationsTableItemProps {
|
|
table: PGTable
|
|
selectedPublication: PGPublication
|
|
}
|
|
|
|
export const PublicationsTableItem = ({
|
|
table,
|
|
selectedPublication,
|
|
}: PublicationsTableItemProps) => {
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { data: protectedSchemas } = useProtectedSchemas()
|
|
const enabledForAllTables = selectedPublication.tables == null
|
|
|
|
const isProtected = protectedSchemas.map((x) => x.name).includes(table.schema)
|
|
|
|
const [checked, setChecked] = useState(
|
|
selectedPublication.tables?.find((x) => x.id == table.id) != undefined
|
|
)
|
|
|
|
const { can: canUpdatePublications } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.DATABASE_WRITE
|
|
)
|
|
|
|
const { mutate: updatePublications, isPending } = useDatabasePublicationUpdateMutation()
|
|
|
|
const toggleReplicationForTable = async (table: PGTable, publication: PGPublication) => {
|
|
if (project === undefined) return console.error('Project is required')
|
|
|
|
const originalChecked = checked
|
|
setChecked(!checked)
|
|
|
|
const publicationTables = publication?.tables ?? []
|
|
const exists = publicationTables.some((x) => x.id == table.id)
|
|
const tables = !exists
|
|
? [`${table.schema}.${table.name}`].concat(
|
|
publicationTables.map((t) => `${t.schema}.${t.name}`)
|
|
)
|
|
: publicationTables.filter((x) => x.id != table.id).map((x) => `${x.schema}.${x.name}`)
|
|
|
|
updatePublications(
|
|
{
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
id: publication.id,
|
|
tables,
|
|
},
|
|
{
|
|
onSuccess: () => {
|
|
toast.success(
|
|
`Successfully ${checked ? 'disabled' : 'enabled'} replication for ${table.name}`
|
|
)
|
|
},
|
|
onError: (error) => {
|
|
toast.error(`Failed to toggle replication for ${table.name}: ${error.message}`)
|
|
setChecked(originalChecked)
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<TableRow key={table.id}>
|
|
<TableCell className="py-3 whitespace-nowrap">{table.name}</TableCell>
|
|
<TableCell className="py-3 whitespace-nowrap hidden lg:table-cell max-w-sm truncate text-foreground-light">
|
|
{table.comment}
|
|
</TableCell>
|
|
<TableCell className="py-3">
|
|
<div className="flex justify-end gap-2">
|
|
{enabledForAllTables ? (
|
|
<Badge>
|
|
<span>Enabled</span>
|
|
<span className="hidden lg:inline-block"> for all tables</span>
|
|
</Badge>
|
|
) : (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<div>
|
|
<Switch
|
|
size="small"
|
|
aria-label={`Toggle replication for ${table.name}`}
|
|
disabled={!canUpdatePublications || isPending || isProtected}
|
|
checked={checked}
|
|
onClick={() => toggleReplicationForTable(table, selectedPublication)}
|
|
/>
|
|
</div>
|
|
</TooltipTrigger>
|
|
{isProtected && (
|
|
<TooltipContent side="bottom" className="w-64 text-center">
|
|
This table belongs to a protected schema, and its publication cannot be toggled
|
|
</TooltipContent>
|
|
)}
|
|
</Tooltip>
|
|
)}
|
|
</div>
|
|
</TableCell>
|
|
</TableRow>
|
|
)
|
|
}
|