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>
169 lines
5.6 KiB
TypeScript
169 lines
5.6 KiB
TypeScript
import { parseAsBoolean, useQueryState } from 'nuqs'
|
|
import { useEffect, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
Button,
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogSection,
|
|
DialogSectionSeparator,
|
|
DialogTitle,
|
|
RadioGroupStacked,
|
|
RadioGroupStackedItem,
|
|
} from 'ui'
|
|
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { useOrganizationCustomerProfileUpdateMutation } from '@/data/organizations/organization-customer-profile-update-mutation'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
|
|
type IndirectTaxDeclaration = 'yes' | 'no'
|
|
type DeclarationModal = 'declaration-form' | 'submission-confirmation' | null
|
|
|
|
export const IndirectTaxDeclarationModal = () => {
|
|
const { data: organization } = useSelectedOrganizationQuery({ enabled: IS_PLATFORM })
|
|
|
|
const [response, setResponse] = useState<IndirectTaxDeclaration | ''>('')
|
|
const [failedSubmissionSlug, setFailedSubmissionSlug] = useState<string>()
|
|
|
|
const [shouldShowDeclarationConfirmation, setShouldShowDeclarationConfirmation] = useQueryState(
|
|
'submit_indirect_tax_declaration',
|
|
parseAsBoolean.withDefault(false)
|
|
)
|
|
|
|
useEffect(() => {
|
|
setResponse('')
|
|
}, [organization?.slug])
|
|
|
|
const { can: canUpdateBillingInfo, isSuccess: permissionsLoaded } = useAsyncCheckPermissionsV2(
|
|
FGA_PERMISSIONS.ORGANIZATION.BILLING_WRITE
|
|
)
|
|
|
|
const { mutate: updateCustomerProfile, isPending } = useOrganizationCustomerProfileUpdateMutation(
|
|
{
|
|
onSuccess: () => {
|
|
if (!shouldShowDeclarationConfirmation) {
|
|
toast.success('GST declaration submitted')
|
|
}
|
|
},
|
|
onError: (_error, variables) => {
|
|
setFailedSubmissionSlug(variables.slug)
|
|
toast.error("We couldn't submit your GST declaration. Reload the page and try again.", {
|
|
duration: Infinity,
|
|
})
|
|
},
|
|
}
|
|
)
|
|
|
|
const canViewDeclaration =
|
|
IS_PLATFORM && organization !== undefined && permissionsLoaded && canUpdateBillingInfo
|
|
|
|
let declarationModal: DeclarationModal = null
|
|
|
|
if (canViewDeclaration) {
|
|
if (organization.requires_indirect_tax_declaration) {
|
|
if (organization.slug !== failedSubmissionSlug) {
|
|
declarationModal = 'declaration-form'
|
|
}
|
|
} else if (shouldShowDeclarationConfirmation) {
|
|
declarationModal = 'submission-confirmation'
|
|
}
|
|
}
|
|
|
|
const onSubmit = () => {
|
|
if (organization?.slug === undefined || response === '') return
|
|
|
|
updateCustomerProfile({
|
|
slug: organization.slug,
|
|
indirect_tax_registration_declaration: response,
|
|
})
|
|
}
|
|
|
|
const closeSubmissionConfirmation = () => {
|
|
setShouldShowDeclarationConfirmation(null)
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<Dialog open={declarationModal === 'declaration-form'}>
|
|
<DialogContent
|
|
size="medium"
|
|
hideClose
|
|
onInteractOutside={(event) => event.preventDefault()}
|
|
onEscapeKeyDown={(event) => event.preventDefault()}
|
|
>
|
|
<DialogHeader>
|
|
<DialogTitle>Confirm your Australian GST status</DialogTitle>
|
|
<DialogDescription>
|
|
Confirm the following for your organization {organization?.name}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogSectionSeparator />
|
|
|
|
<DialogSection className="py-4">
|
|
<RadioGroupStacked
|
|
className="[&_p]:text-pretty"
|
|
value={response}
|
|
onValueChange={(value) => {
|
|
if (value === 'yes' || value === 'no') setResponse(value)
|
|
}}
|
|
>
|
|
<RadioGroupStackedItem
|
|
value="yes"
|
|
label="Yes, I confirm"
|
|
description="We are and were registered for GST in Australia when we acquired services from Supabase, and the services were acquired in the course or furtherance of our business."
|
|
/>
|
|
<RadioGroupStackedItem
|
|
value="no"
|
|
label="No, I do not confirm"
|
|
description="We are not or were not registered for GST in Australia when we acquired services from Supabase, or the services were acquired for a purpose unrelated to our business."
|
|
/>
|
|
</RadioGroupStacked>
|
|
</DialogSection>
|
|
|
|
<DialogFooter>
|
|
<ButtonTooltip
|
|
variant="primary"
|
|
onClick={onSubmit}
|
|
disabled={response === ''}
|
|
loading={isPending}
|
|
tooltip={{
|
|
content: {
|
|
side: 'top',
|
|
text: response === '' ? 'Select Yes or No to continue' : undefined,
|
|
},
|
|
}}
|
|
>
|
|
Submit declaration
|
|
</ButtonTooltip>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
<Dialog
|
|
open={declarationModal === 'submission-confirmation'}
|
|
onOpenChange={(open) => {
|
|
if (!open) closeSubmissionConfirmation()
|
|
}}
|
|
>
|
|
<DialogContent size="small">
|
|
<DialogHeader>
|
|
<DialogTitle>GST declaration submitted</DialogTitle>
|
|
<DialogDescription>
|
|
The GST declaration for {organization?.name} has been submitted. No further action is
|
|
required.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<DialogFooter>
|
|
<Button onClick={closeSubmissionConfirmation}>Close</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</>
|
|
)
|
|
}
|