Skip to content

feat(db): add per-brand access for people: brand scope, member brands and access helpers (00388) - #1878

Merged
gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-db
Oct 31, 2026
Merged

gkhngyk merged 2 commits into
ansvisor:mainfrom
cihan-gyk:feature/brand-access-db

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

The first PR of brand-level access (spec: one Account, several brands; a person sees All brands or Selected brands). Migration 00388. Nothing changes for anyone yet: everyone stays all, and nothing reads the new pieces until the RLS and UI PRs.

Owner's decisions (2026-10-30):

  • one role per person, plus a brand list;
  • Admins always see all brands;
  • only Admins change brand access;
  • every plan with more than one brand gets it;
  • the demo brand stays visible to everyone;
  • integrations move per brand later.

00388

  • profiles.brand_scope ('all' | 'selected', default 'all'):
    • managed by the server only; the 00374 guard refuses a person changing their own;
    • Admins are always 'all' (check), and promotion to Admin resets it;
    • leaving the Account resets it to 'all';
    • a direct move to another Account keeps 'selected', so a Selected-brands invite can't turn into All brands. Grants are deleted in both cases.
  • member_brands(organization_id, user_id, brand_id, granted_by, granted_at):
    • PK (user_id, brand_id); cascades on brand, user and Account deletion;
    • a trigger checks that the brand and the person belong to the row's Account;
    • RLS: the person reads their own rows, Admins read the Account's; only the server writes.
  • invitations.brand_scope and brand_ids uuid[]. The ids are only allowed with 'selected', an Admin invite must be 'all', and the ids must be the invitation's Account's brands.
  • Helpers can_access_brand(p_brand_id, p_user_id default auth.uid()) and user_brand_ids():
    • STABLE, SECURITY DEFINER, search_path = '';
    • executable by authenticated and service_role, not anon;
    • true when the brand is in the person's Account and the person is an Admin, is 'all', was given the brand, or the brand is the demo brand;
    • asking about another user works only with a service-role JWT; otherwise false.
  • A brand created by a 'selected' person is granted to them (after-insert trigger).
  • web/src/types/supabase.ts updated. member_brands is on the demo coverage skip list.

Related issue

None. Brand-level access, PR 1 of ~16 (spec BRAND-ACCESS).

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00388 in one transaction (psql -1 -f).
  2. As an authenticated member:
    • update profiles set brand_scope='selected' where id = auth.uid() → 42501;
    • insert into member_brands … → 42501.
  3. select public.user_brand_ids() as anon → permission denied.
  4. Run server/src/lib/brand-access-schema.test.js and coverage.test.js.

Applied on the dev database and checked:

  • ownership and grants;
  • trigger order (guard < reset_brand_scope < reset_menu_access);
  • the lifecycle: selected → all clears grants; promotion to Admin; leaving the Account; a move keeps the scope; brand deletion cascades;
  • invitation checks: unknown or other-Account brand → 23514, all with ids → check, Admin + selected → check.
  • Every probe ran inside a rolled-back transaction, and no row was left.

Checklist

  • Branch follows the naming convention
  • Commits follow Conventional Commits
  • yarn lint passes (0 errors; 7 old warnings in files this PR doesn't touch)
  • yarn typecheck passes
  • yarn format:check passes
  • Changes are focused — one concern per PR
  • Help article updated under help/ (or no user-visible change): no user-visible change

supabase/schema.sql was rebuilt with build-schema.sh. Web and server suites pass. The one exception was chat-menu.test.tsx's clock case, fixed in #1876.

For production (@gkhngyk): apply 00388 in one transaction. It sorts before 00400–00423, so it needs --include-all, as #1873's 00387 did. No env or cron.

🤖 Generated with Claude Code

… and access helpers (00388)

BRAND-ACCESS PR 1 of 16: the data and the rule, no behaviour change.
Everyone starts with All brands, and no policy reads it yet.

- profiles.brand_scope ('all' | 'selected', default 'all'), set by the
  server: 00374's guard refuses a person's own change; admins are always
  'all' (a check, and a reset on promotion); leaving the organization
  resets it to 'all'. A move straight into another organization keeps the
  scope the update gives, so it never widens access by itself.
- member_brands (organization, person, brand, granted_by, granted_at),
  one row per person and brand, gone with the brand, the person or the
  organization; a trigger keeps the brand and the person in the row's
  organization. The person reads their own rows, admins the
  organization's; only the server writes. The rows go when the person
  moves, leaves or gets All brands.
- invitations.brand_scope / brand_ids, checked against the organization.
- public.can_access_brand(brand, person default auth.uid()) and
  public.user_brand_ids(): STABLE, SECURITY DEFINER, empty search path,
  for authenticated and service_role only. Admin, All brands, a shared
  brand or the demo brand, in the person's organization; another
  person's id only for the service role.
- A brand added by a Selected-brands person is shared with them (after
  insert trigger).
- web types, and member_brands on the demo coverage's skipped list.
- server/src/lib/brand-access-schema.test.js reads the migration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 30, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. 00388 adds the brand scope, member brands and the access helpers; nothing reads them for enforcement until #1879. The profile guard keeps every check 00374 has. Apply 00388 before merging.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging in order with the other approved PRs. This head merges current main; its tree is what main will be after this squash, checked with the full server and web suites, tsc, lint, format and next build.

@gkhngyk
gkhngyk merged commit 5b975d7 into ansvisor:main Oct 31, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants