mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +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>
130 lines
4.6 KiB
TypeScript
130 lines
4.6 KiB
TypeScript
import { CirclePauseIcon } from 'lucide-react'
|
|
import { useRouter } from 'next/router'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from 'ui'
|
|
|
|
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
|
|
import { useSetProjectStatus } from '@/data/projects/project-detail-query'
|
|
import { useProjectPauseMutation } from '@/data/projects/project-pause-mutation'
|
|
import { useCheckEntitlements } from '@/hooks/misc/useCheckEntitlements'
|
|
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
|
|
import {
|
|
useIsHighAvailability,
|
|
useIsProjectActive,
|
|
useSelectedProjectQuery,
|
|
} from '@/hooks/misc/useSelectedProject'
|
|
import { PROJECT_STATUS } from '@/lib/constants'
|
|
|
|
export const PauseProjectButton = () => {
|
|
const router = useRouter()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { data: organization } = useSelectedOrganizationQuery()
|
|
const { setProjectStatus } = useSetProjectStatus()
|
|
|
|
const isProjectActive = useIsProjectActive()
|
|
const isHighAvailability = useIsHighAvailability()
|
|
const isProjectUnhealthy = project?.status === PROJECT_STATUS.ACTIVE_UNHEALTHY
|
|
const [isModalOpen, setIsModalOpen] = useState(false)
|
|
|
|
const projectRef = project?.ref ?? ''
|
|
const isPaused = project?.status === PROJECT_STATUS.INACTIVE
|
|
const { can: canPauseProject } = useAsyncCheckPermissionsV2([FGA_PERMISSIONS.PROJECT.ADMIN_WRITE])
|
|
|
|
const isFreePlan = organization?.plan.id === 'free'
|
|
const isBranch = Boolean(project?.parent_project_ref)
|
|
const entityLabel = isBranch ? 'branch' : 'project'
|
|
const { hasAccess: projectPausingAllowedInOrg } = useCheckEntitlements(
|
|
'project_pausing',
|
|
organization?.slug
|
|
)
|
|
|
|
const { mutate: pauseProject, isPending: isPausing } = useProjectPauseMutation({
|
|
onSuccess: (_, variables) => {
|
|
setProjectStatus({ ref: variables.ref, status: PROJECT_STATUS.PAUSING })
|
|
toast.success('Pausing project...')
|
|
router.push(`/project/${projectRef}`)
|
|
},
|
|
})
|
|
|
|
const requestPauseProject = () => {
|
|
if (!canPauseProject) {
|
|
return toast.error(`You do not have the required permissions to pause this ${entityLabel}`)
|
|
}
|
|
pauseProject({ ref: projectRef })
|
|
}
|
|
|
|
const buttonDisabled =
|
|
(!isBranch && !projectPausingAllowedInOrg) ||
|
|
project === undefined ||
|
|
isPaused ||
|
|
!canPauseProject ||
|
|
!isProjectActive ||
|
|
isHighAvailability
|
|
|
|
function getTooltipText() {
|
|
if (isPaused) {
|
|
return `Your ${entityLabel} is already paused`
|
|
} else if (isHighAvailability) {
|
|
return 'Pausing is unavailable on High Availability projects'
|
|
} else if (!canPauseProject) {
|
|
return `You need additional permissions to pause this ${entityLabel}`
|
|
} else if (isProjectUnhealthy) {
|
|
return `Your ${entityLabel} is unhealthy — restart it instead to restore normal operation`
|
|
} else if (!isProjectActive) {
|
|
return `Unable to pause ${entityLabel} as ${entityLabel} is not active`
|
|
} else if (!isBranch && !projectPausingAllowedInOrg && !isFreePlan) {
|
|
return 'Projects on a paid plan will always be running'
|
|
} else {
|
|
return undefined
|
|
}
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<ButtonTooltip
|
|
icon={<CirclePauseIcon />}
|
|
onClick={() => setIsModalOpen(true)}
|
|
loading={isPausing}
|
|
disabled={buttonDisabled}
|
|
tooltip={{
|
|
content: {
|
|
side: 'bottom',
|
|
text: getTooltipText(),
|
|
},
|
|
}}
|
|
>
|
|
Pause {entityLabel}
|
|
</ButtonTooltip>
|
|
|
|
<AlertDialog open={isModalOpen} onOpenChange={setIsModalOpen}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Pause {entityLabel}?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This {entityLabel} will be unavailable while paused. Paused {entityLabel} can be
|
|
resumed for up to 1 year. After that, backups remain available to download.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel disabled={isPausing}>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction disabled={isPausing} onClick={requestPauseProject} variant="danger">
|
|
{isPausing ? `Pausing ${entityLabel}...` : `Pause ${entityLabel}`}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
)
|
|
}
|