Files
fffb1e23ca feat: implement profile permissions v2 api (#50966)
## 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>
2026-10-21 06:54:16 -04:00

145 lines
5.2 KiB
TypeScript

import { useParams } from 'common'
import { noop } from 'lodash'
import { LockIcon, TableIcon } from 'lucide-react'
import { AiIconAnimation, Badge, CardTitle } from 'ui'
import type { PolicyTable } from './PolicyTableRow.types'
import { SIDEBAR_KEYS } from '@/components/layouts/ProjectLayout/LayoutSidebar/LayoutSidebarProvider'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { EditorTablePageLink } from '@/data/prefetchers/project.$ref.editor.$id'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
import { useSidebarManagerSnapshot } from '@/state/sidebar-manager-state'
interface PolicyTableRowHeaderProps {
table: PolicyTable
isLocked: boolean
hasApiAccess: boolean
isLoadingApiAccess: boolean
onSelectToggleRLS: (table: PolicyTable) => void
onSelectCreatePolicy: (table: PolicyTable) => void
}
export const PolicyTableRowHeader = ({
table,
isLocked,
hasApiAccess,
isLoadingApiAccess,
onSelectToggleRLS = noop,
onSelectCreatePolicy,
}: PolicyTableRowHeaderProps) => {
const { ref } = useParams()
const aiSnap = useAiAssistantStateSnapshot()
const { openSidebar } = useSidebarManagerSnapshot()
const { can: canCreatePolicies } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.DATABASE_WRITE
)
const { can: canToggleRLS } = useAsyncCheckPermissionsV2(FGA_PERMISSIONS.PROJECT.DATABASE_WRITE)
const isRealtimeSchema = table.schema === 'realtime'
const isRealtimeMessagesTable = isRealtimeSchema && table.name === 'messages'
const isTableLocked = isRealtimeSchema ? !isRealtimeMessagesTable : isLocked
return (
<div id={table.id.toString()} className="flex w-full items-center justify-between">
<div className="flex gap-x-4 text-left flex-wrap">
<EditorTablePageLink
projectRef={ref}
id={String(table.id)}
className="flex items-center gap-3 flex-wrap"
>
<TableIcon strokeWidth={1.5} size={16} className="text-foreground-muted" />
<CardTitle className="m-0 normal-case">{table.name}</CardTitle>
</EditorTablePageLink>
<div className="flex items-center gap-x-1">
{!table.rls_enabled && (
<Badge variant="warning" className="h-5">
RLS Disabled
</Badge>
)}
{!isLoadingApiAccess && !hasApiAccess && (
<Badge variant="default" className="h-5">
API Disabled
</Badge>
)}
{isTableLocked && (
<Badge className="h-5">
<span className="flex gap-x-1 items-center text-foreground-lighter">
<LockIcon size={10} /> Locked
</span>
</Badge>
)}
</div>
</div>
{!isTableLocked && (
<div className="flex-1">
<div className="flex flex-row justify-end gap-x-2">
{!isRealtimeMessagesTable && (
<ButtonTooltip
disabled={!canToggleRLS}
onClick={() => onSelectToggleRLS(table)}
data-testid={`${table.name}-toggle-rls`}
tooltip={{
content: {
side: 'bottom',
text: !canToggleRLS
? 'You need additional permissions to toggle RLS'
: undefined,
},
}}
>
{table.rls_enabled ? 'Disable RLS' : 'Enable RLS'}
</ButtonTooltip>
)}
<ButtonTooltip
disabled={!canToggleRLS || !canCreatePolicies}
onClick={() => onSelectCreatePolicy(table)}
data-testid={`${table.name}-create-policy`}
tooltip={{
content: {
side: 'bottom',
text: !canToggleRLS
? !canToggleRLS || !canCreatePolicies
? 'You need additional permissions to create RLS policies'
: undefined
: undefined,
},
}}
>
Create policy
</ButtonTooltip>
<ButtonTooltip
className="px-1"
onClick={() => {
openSidebar(SIDEBAR_KEYS.AI_ASSISTANT)
aiSnap.newChat({
name: 'Create new policy',
initialInput: `Create and name a new policy for the ${table.schema} schema on the ${table.name} table that ...`,
})
}}
tooltip={{
content: {
side: 'bottom',
text:
!canToggleRLS || !canCreatePolicies
? 'You need additional permissions to create RLS policies'
: 'Create with Supabase Assistant',
},
}}
aria-label={
!canToggleRLS || !canCreatePolicies
? 'You need additional permissions to create RLS policies'
: 'Create with Supabase Assistant'
}
>
<AiIconAnimation size={16} />
</ButtonTooltip>
</div>
</div>
)}
</div>
)
}