mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +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>
110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import { zodResolver } from '@hookform/resolvers/zod'
|
|
import { useParams } from 'common'
|
|
import { SubmitHandler, useForm } from 'react-hook-form'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
Form,
|
|
FormControl,
|
|
FormField,
|
|
Input,
|
|
} from 'ui'
|
|
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
|
|
import * as z from 'zod'
|
|
|
|
import { useUserInviteMutation } from '@/data/auth/user-invite-mutation'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
|
|
export type InviteUserModalProps = {
|
|
visible: boolean
|
|
setVisible: (visible: boolean) => void
|
|
}
|
|
|
|
const formSchema = z.object({
|
|
email: z.string().min(1, 'Please enter a valid email').email('Please enter a valid email'),
|
|
})
|
|
const formId = 'invite-user-form'
|
|
|
|
const InviteUserModal = ({ visible, setVisible }: InviteUserModalProps) => {
|
|
const { ref: projectRef } = useParams()
|
|
const form = useForm<z.infer<typeof formSchema>>({
|
|
resolver: zodResolver(formSchema),
|
|
defaultValues: {
|
|
email: '',
|
|
},
|
|
})
|
|
|
|
const handleToggle = () => setVisible(!visible)
|
|
const { mutate: inviteUser, isPending: isInviting } = useUserInviteMutation({
|
|
onSuccess: (_, variables) => {
|
|
toast.success(`Sent invite email to ${variables.email}`)
|
|
setVisible(false)
|
|
},
|
|
})
|
|
const { can: canInviteUsers } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.PROJECT.AUTH_USERS_WRITE
|
|
)
|
|
|
|
const onInviteUser: SubmitHandler<z.infer<typeof formSchema>> = async (values) => {
|
|
if (!projectRef) return console.error('Project ref is required')
|
|
inviteUser(
|
|
{ projectRef, email: values.email },
|
|
{
|
|
onSuccess: () => {
|
|
form.reset()
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
return (
|
|
<Dialog key="invite-user-modal" open={visible} onOpenChange={handleToggle}>
|
|
<DialogContent size="small">
|
|
<DialogHeader>
|
|
<DialogTitle>Invite a new user</DialogTitle>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
<Form {...form}>
|
|
<DialogSection>
|
|
<form id={formId} onSubmit={form.handleSubmit(onInviteUser)} noValidate>
|
|
<FormField
|
|
control={form.control}
|
|
name="email"
|
|
render={({ field }) => (
|
|
<FormItemLayout layout="vertical" label="User email">
|
|
<FormControl className="relative col-span-6">
|
|
<Input {...field} />
|
|
</FormControl>
|
|
</FormItemLayout>
|
|
)}
|
|
/>
|
|
</form>
|
|
</DialogSection>
|
|
|
|
<DialogFooter>
|
|
<Button onClick={handleToggle}>Cancel</Button>
|
|
<Button
|
|
variant="primary"
|
|
form={formId}
|
|
type="submit"
|
|
loading={isInviting}
|
|
disabled={!canInviteUsers || isInviting}
|
|
>
|
|
Invite user
|
|
</Button>
|
|
</DialogFooter>
|
|
</Form>
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|
|
|
|
export default InviteUserModal
|