Commit Graph
2 Commits
Author SHA1 Message Date
Charis 1bcc472d64 feat(studio): add Grant type and useGrant/getGrant/ensureGrant (#52105)
## Problem

Resolves FE-4688.

Core types and minting for endpoint-bound FGA grants. A `Grant<E, V>` is
minted by checking the user's v2 permissions against the generated
`fgaRequirements` map for endpoint `E` (and optional variant `V`).
Nothing consumes grants yet: the fetch wrappers that require them come
in a follow-up PR.

## Solution

### What changed

- **`lib/grants.ts`** (new)
- `Grant<E, V>`: the brand is a function type, so it's invariant in both
the endpoint and the variant. A grant only satisfies its exact endpoint
and variant.
- `GrantResult` is `loading | granted | denied | error`, and `.grant` is
defined only when granted.
- `GrantFor<Method, Path>` resolves to the grant type for an annotated
endpoint, or `never` otherwise.
  - Minting functions:
    - `getGrant` is pure.
- `useGrant` resolves org and project from the URL unless the scope
overrides them.
    - `ensureGrant` is for imperative code and prefetch.
- Self-hosted always mints. A failed permissions request returns
`error`. Grants are never part of query keys.
- **`lib/fga-refinements.ts`** (new): an empty map defining the shape
for named variants. Each entry records the base annotation it was
written against, so typecheck fails if that annotation changes.
- **`permissions-query-v2.ts`**: adds a `permissionsQueryOptionsV2`
factory. `usePermissionsQueryV2` is now a thin wrapper around it, with
the same signature and the same 5-minute `staleTime`.
- **`doPermissionsCheckV2`**:
- Now accepts OR-of-AND requirement groups. Existing single-permission
and flat-list calls behave as before.
  - Empty input never grants, including an empty group.
- **`usePermissionScopeV2`**: org/project/branch-parent resolution
extracted from the V2 hook, so `useGrant` and
`useAsyncCheckPermissionsV2` share one implementation.
- **Invalidation**: the role assign, unassign, and update mutations,
`UpdateRolesConfirmationModal`, and project creation now invalidate the
v2 permissions query.
- **knip**: temporary `files` ignore for `lib/grants.ts` until its first
consumer lands.

### Behavior change

`useAsyncCheckPermissionsV2` with an explicit `projectRef` equal to the
URL's project ref now resolves branch projects to their parent before
checking. Previously, any explicit ref skipped that resolution, so users
with project-scoped roles were denied on branch projects. An explicit
ref that differs from the URL ref is still used as-is.

## Review instructions

### Tests

- Unit tests for `doPermissionsCheckV2` with OR-of-AND groups.
- `lib/grants.test.ts`:
- `getGrant`, `useGrant`, and `ensureGrant` across OR-of-AND groups,
branch projects, explicit scope, self-hosted, and loading/denied/error
states.
- Type tests: a grant can't be used for a different endpoint, a plain
object isn't a grant, `GrantFor`, and status narrowing.

The type test that a grant without a variant can't satisfy a variant
parameter moves to the api-keys refinement PR, since no endpoint has
variants yet.

1. Run `pnpm test:studio` and confirm the `doPermissionsCheckV2` and
`lib/grants` tests pass.
2. Run `pnpm typecheck` to confirm the grant type tests hold.
3. Check the branch-project behavior in `useAsyncCheckPermissionsV2`
with an explicit `projectRef` matching the URL's project ref.

## Checklist

Check all before review:

- [ ] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-10-22 13:32:04 -04:00
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