Files
supabase/apps/studio/components/layouts/ComputeLayout/ComputeLayout.tsx
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

99 lines
3.0 KiB
TypeScript

import { FeatureFlagContext, useFlag, useParams } from 'common'
import { useRouter } from 'next/router'
import { useContext, useEffect, useMemo, type PropsWithChildren } from 'react'
import { Badge } from 'ui'
import { ProjectLayout } from '../ProjectLayout'
import { NoPermission } from '@/components/ui/NoPermission'
import { ProductMenu } from '@/components/ui/ProductMenu'
import type { ProductMenuGroup } from '@/components/ui/ProductMenu/ProductMenu.types'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { withAuth } from '@/hooks/misc/withAuth'
import { PRODUCT_NAME } from '@/lib/constants/compute'
const useGenerateComputeMenu = (): ProductMenuGroup[] => {
const { ref: projectRef = 'default' } = useParams()
return useMemo(
() => [
{
title: 'Manage',
items: [
{
name: PRODUCT_NAME,
key: 'main',
pages: ['', '[name]'],
url: `/project/${projectRef}/compute`,
items: [],
},
{
name: 'Secrets',
key: 'secrets',
url: `/project/${projectRef}/compute/secrets`,
items: [],
},
],
},
],
[projectRef]
)
}
export const ComputeProductMenu = () => {
const router = useRouter()
const page = router.pathname.split('/')[4]
const menu = useGenerateComputeMenu()
return <ProductMenu page={page} menu={menu} />
}
interface ComputeLayoutProps {
title?: string
}
const ComputeLayoutContent = ({ children, title }: PropsWithChildren<ComputeLayoutProps>) => {
const router = useRouter()
const { ref: projectRef } = useParams()
const { hasLoaded } = useContext(FeatureFlagContext)
const computeEnabled = useFlag('compute')
// The v2 compute routes require the FGA workers_read permission, which shared-types does not
// expose yet; they reuse the Edge Functions OAuth scope, so gate on the same product here.
const { isLoading: isLoadingPermissions, can: canReadCompute } = useAsyncCheckPermissionsV2(
FGA_PERMISSIONS.PROJECT.WORKERS_READ
)
useEffect(() => {
if (hasLoaded && !computeEnabled) {
router.replace(`/project/${projectRef}`)
}
}, [router, hasLoaded, computeEnabled, projectRef])
if (!computeEnabled) return null
if (isLoadingPermissions) {
return (
<ProjectLayout
isLoading
product={PRODUCT_NAME}
browserTitle={{ entity: PRODUCT_NAME, section: title }}
/>
)
}
return (
<ProjectLayout
product={PRODUCT_NAME}
productMenuBadge={<Badge variant="warning">Private Alpha</Badge>}
productMenu={<ComputeProductMenu />}
isBlocking={false}
browserTitle={{ entity: PRODUCT_NAME, section: title }}
>
{canReadCompute ? (
children
) : (
<NoPermission isFullPage resourceText="view this project's compute instances" />
)}
</ProjectLayout>
)
}
export const ComputeLayout = withAuth(ComputeLayoutContent)