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>
116 lines
3.8 KiB
TypeScript
116 lines
3.8 KiB
TypeScript
import { useEffect } from 'react'
|
|
import {
|
|
Button,
|
|
cn,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
Form,
|
|
} from 'ui'
|
|
import { Admonition } from 'ui-patterns/Admonition'
|
|
|
|
import { AIOptInLevelSelector } from '@/components/interfaces/Organization/GeneralSettings/AIOptInLevelSelector'
|
|
import { useAIOptInForm } from '@/hooks/forms/useAIOptInForm'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import type { AiOptInLevel } from '@/hooks/misc/useOrgOptedIntoAi'
|
|
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
|
|
import { isOptInLevelAtLeast } from '@/lib/ai/opt-in-level'
|
|
|
|
interface AIOptInModalProps {
|
|
visible: boolean
|
|
onCancel: () => void
|
|
/** The level the Assistant asked for. Shows it as proposed. */
|
|
requiredLevel?: AiOptInLevel
|
|
/** Called after the level is saved. */
|
|
onSaved?: (level: AiOptInLevel) => void
|
|
}
|
|
|
|
export const AIOptInModal = ({ visible, onCancel, requiredLevel, onSaved }: AIOptInModalProps) => {
|
|
const { data: organization } = useSelectedOrganizationQuery()
|
|
const { form, onSubmit, isUpdating, currentOptInLevel } = useAIOptInForm((level) => {
|
|
onSaved?.(level)
|
|
onCancel()
|
|
})
|
|
const { can: canUpdateOrganization } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.ORGANIZATION.ADMIN_WRITE
|
|
)
|
|
|
|
const onOpenChange = (open: boolean) => {
|
|
if (!open) {
|
|
onCancel()
|
|
}
|
|
}
|
|
|
|
const proposedLevel =
|
|
requiredLevel && !isOptInLevelAtLeast(currentOptInLevel, requiredLevel)
|
|
? requiredLevel
|
|
: undefined
|
|
|
|
useEffect(() => {
|
|
if (visible) {
|
|
form.reset({ aiOptInLevel: currentOptInLevel })
|
|
}
|
|
}, [visible, currentOptInLevel, form])
|
|
|
|
return (
|
|
<Dialog open={visible} onOpenChange={onOpenChange}>
|
|
<DialogContent size="large" aria-describedby={undefined}>
|
|
<Form {...form}>
|
|
<form id="ai-opt-in-form" onSubmit={form.handleSubmit(onSubmit)}>
|
|
<DialogHeader padding="small">
|
|
<DialogTitle>Update Supabase Assistant Opt-in Level</DialogTitle>
|
|
</DialogHeader>
|
|
|
|
<DialogSectionSeparator />
|
|
|
|
<DialogSection className="space-y-4 pb-0" padding="small">
|
|
<Admonition
|
|
type="warning"
|
|
title={`This changes the opt-in level for every project in ${organization?.name ?? 'this organization'}`}
|
|
description={
|
|
proposedLevel && 'The Assistant needs more access to answer your question.'
|
|
}
|
|
/>
|
|
<AIOptInLevelSelector
|
|
control={form.control}
|
|
disabled={!canUpdateOrganization || isUpdating}
|
|
currentLevel={proposedLevel && currentOptInLevel}
|
|
proposedLevel={proposedLevel}
|
|
/>
|
|
</DialogSection>
|
|
|
|
<DialogFooter
|
|
padding="small"
|
|
className={cn(!canUpdateOrganization && 'justify-between!')}
|
|
>
|
|
{!canUpdateOrganization && (
|
|
<p className="text-sm text-foreground-lighter">
|
|
You need additional permissions to update the opt-in level
|
|
</p>
|
|
)}
|
|
<div className="flex items-center gap-x-2">
|
|
<Button disabled={isUpdating} onClick={onCancel}>
|
|
Cancel
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
type="submit"
|
|
form="ai-opt-in-form"
|
|
loading={isUpdating}
|
|
disabled={isUpdating || !canUpdateOrganization || !form.formState.isDirty}
|
|
>
|
|
Confirm
|
|
</Button>
|
|
</div>
|
|
</DialogFooter>
|
|
</form>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|