Files
supabase/apps/studio/components/interfaces/Account/AccessTokens/hooks/useTokenAccessEvaluation.ts
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

68 lines
2.2 KiB
TypeScript

import { useMemo } from 'react'
import type { PermissionSelection, ResourceAccessMode } from '../AccessToken.permissions'
import {
applySelectionToRoleContext,
computeTokenRoleContext,
type TokenAccessEvaluation,
} from '../AccessToken.roles'
import { useOrgAndProjectData } from './useOrgAndProjectData'
import { usePermissionsQueryV2 } from '@/data/permissions/permissions-query-v2'
interface UseTokenAccessEvaluationArgs {
selection: PermissionSelection
resourceAccess: ResourceAccessMode
organizationSlugs: string[]
projectRefs: string[]
enabled?: boolean
}
/**
* Evaluates a token's scope selection and bound resources against the current user's live access.
* Advisory only — actual enforcement is the per-request intersection on the API side. While the
* underlying queries load (or on self-hosted), the evaluation reports `status: 'unknown'` and
* callers must show no warnings rather than flash false ones.
*
* Role resolution (the expensive part) is memoized separately from the selection, so toggling
* permissions in the form only re-runs the cheap selection pass.
*/
export const useTokenAccessEvaluation = ({
selection,
resourceAccess,
organizationSlugs,
projectRefs,
enabled = true,
}: UseTokenAccessEvaluationArgs): TokenAccessEvaluation => {
const { data: permissions } = usePermissionsQueryV2({ enabled })
const { organizations, projects, isLoadingOrgs, isLoadingProjects } = useOrgAndProjectData({
enabled,
})
// Org/project lists still loading: resources the user *does* have access to would read as
// inaccessible, so report unknown instead.
const hasCompleteResourceLists = !isLoadingOrgs && !isLoadingProjects
const context = useMemo(
() =>
computeTokenRoleContext({
resourceAccess,
organizationSlugs,
projectRefs,
permissions: hasCompleteResourceLists ? permissions : undefined,
organizations,
projects,
}),
[
resourceAccess,
organizationSlugs,
projectRefs,
permissions,
organizations,
projects,
hasCompleteResourceLists,
]
)
return useMemo(() => applySelectionToRoleContext(context, selection), [context, selection])
}