Skip to content

feat(settings): store who sees which menu sections, by role, person and work area (menu access, part 1) - #1702

Merged
gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/menu-access-data
Oct 28, 2026
Merged

gkhngyk merged 5 commits into
ansvisor:mainfrom
cihan-gyk:feature/menu-access-data

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Summary

Menu access, part 1 (data + rules, no UI yet): admins, and managers they allow, choose which left-menu sections each role sees, with per-person exceptions; each person can pick what they work on, which narrows their own menu. Hide only: a hidden section's pages still open from a link, and roles and data access don't change.

  • Migration 00374_menu_access.sql:
    • organization_menu_roles: per organization and non-admin role, the hidden sections. Members read; the server writes.
    • On profiles: menu_shown, menu_hidden, can_manage_menus (server-set; the 00228 guard now blocks a person's own change), work_areas (checked against 9 ids) and work_areas_asked_at.
    • A trigger clears a person's menu exceptions and the menu-manager right when they move organization (removed, then invited elsewhere), and the right on a role change, so neither carries over.
  • web/src/lib/menu-access.ts: the pure rule. The role's hidden sections, changed by the person's exceptions, then narrowed to their work areas. Action Center is always on. Nobody hides a section from an admin; only their own work areas narrow it. A section no work area names stays.
  • web/src/lib/actions/menu-access.ts: server actions getMyMenu, getMenuAccessSettings, setRoleMenu, setMemberMenu (never on admins), setMenuManager (admins only, managers only), setMyWorkAreas.
  • schema.sql and types/supabase.ts regenerated.

Next, stacked: part 2 filters the sidebar by this, part 3 adds Settings › Menu access, then the "What do you work on?" step. No read-only lock here: menu settings cost nothing; the read-only guard (#1612 chain) decides once it is on main.

Spec and mockup: Drive › Ansvisor Product Developments › Menu Access.

Related issue

None.

Type of change

  • New feature

How to test

  1. Apply supabase/migrations/00374_menu_access.sql to a dev database.
  2. cd web && yarn test src/lib/menu-access.test.ts src/lib/actions/menu-access.test.ts.
  3. In SQL, as an authenticated user: update profiles set can_manage_menus = true where id = auth.uid() → "profile menu access is managed server-side"; update profiles set work_areas = '{brand}' → succeeds; '{sales}' → check violation.
  4. As service role, give a manager can_manage_menus = true and menu_hidden = '{content}', then set their organization_id to null → all three are cleared; change only their role → can_manage_menus goes false.
  5. insert into organization_menu_roles (organization_id, role) values (…, 'admin') → check violation.

Checklist

  • yarn format && yarn lint && yarn typecheck && yarn test && yarn build pass in web/ (459 files, 4,516 tests)
  • bash supabase/build-schema.sh leaves schema.sql unchanged
  • Tests added
  • npm run version:check (fails on main too: docker-compose images 0.2.0)

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 28, 2026 14:02
… part 1)

Menu access, part 1 of 7: the data and the rule, with no UI yet.

- 00374 adds organization_menu_roles, the sections a role (not admin)
  doesn't see. Members of the organization read it and the server
  writes it.
- On profiles it adds menu_shown, menu_hidden and can_manage_menus,
  which only the server sets (00228's guard is extended), and
  work_areas and work_areas_asked_at, which the person sets.
- lib/menu-access.ts holds the rule: the role's sections, then the
  person's exceptions, narrowed to their work areas.
  - Action Center is always on.
  - Nobody takes a section off an admin's menu; only their own work
    areas narrow it.
  - A section is named by its route, so a new one shows by default.
- lib/actions/menu-access.ts covers getMyMenu, the Settings data, the
  role table, per-person exceptions and menu managers (admin only).
  It also writes the person's own work areas.

Hide only: pages still open from a link, roles and data access don't
change.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A removed member keeps their profile and an invitation moves it to the
next organization, so a menu manager of one organization arrived in
another still able to change its menus, with their old exceptions. A
trigger now clears menu_shown, menu_hidden and can_manage_menus on a new
organization and can_manage_menus on a new role, unless the same update
sets them; it fires after the guard, so the guard still checks what the
person asked for.

Also reloads PostgREST's schema at the end of 00374, validates
setMenuManager's flag with Zod, and tests that the work-area check in
the database lists exactly the app's work areas.

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 28, 2026
…areas

"What do you work on?" is for new sign-ups and people joining from an
invite, once. Without this, every existing person would get the step at
their next sign-in. They can still pick work areas in Settings › Account.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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, with follow-ups.

What I checked:

  • Own change. 8a04bfa (feature) and 5120e33 (reset on an organization or role change), with one merge of main in between (c572e41); a re-merge of its parents gives the same tree. On today's main only supabase/schema.sql conflicts. 00374 clashes with no other open PR.
  • 00374.
    • organization_menu_roles: RLS on, authenticated gets select only, and only for the person's own organization; no grant to anon; writes go through the service role. role <> 'admin' is a check.
    • guard_profile_server_columns keeps 00228's role and organization rules word for word and adds the three menu columns. work_areas and work_areas_asked_at stay the person's own, and the check lists exactly WORK_AREA_IDS (tested).
    • reset_profile_menu_access runs after the guard (name order, tested), only edits NEW, and has search_path = ''. Removing someone and then inviting them elsewhere clears their exceptions and the menu right. A role change clears the right.
    • The new columns are not null with constant defaults, so adding them doesn't rewrite the table.
  • Actions. Every write re-reads the caller's profile on the server, and changes another member's row only within the caller's organization. Admin-only and manager-only targets are enforced, and inputs are bounded by Zod. setMyWorkAreas writes through the person's own client. I removed the admin check, the organization filter, the manager-only target and the role condition in canManage, one at a time, and the tests caught each one.
  • Tests. Both test files pass on the PR's tree (35 tests). Without the change they don't run, because the modules are new.
  • Deploy. 00374 is applied to production (re-checked as signed-in and anonymous users before applying). The combined tree of parts 1–6 passes the server and web tests, lint, format, typecheck and the schema build.

Follow-ups (none block):

  1. No test covers the guard's new menu block. If I delete it, every test still passes. Add a text test like the reset one: 00374's guard_profile_server_columns body is 00228's plus a block that names menu_shown, menu_hidden and can_manage_menus. That way a later edit can't drop the menu block or 00228's role and organization rules without a test failing.
  2. Make each check and its write one statement. In setMemberMenu, add .neq('role', 'admin') to the update. In setMenuManager, add .eq('role', 'manager'). Then read the updated row back (.select('id')) and throw when nothing matched. A role change between the read and the write then can't slip through.
  3. For part 2: currentProfile throws "No organization" for someone without one, for example during onboarding or after removal. Have the layout's getMyMenu call return an empty menu for them instead of failing the page.

@gkhngyk
gkhngyk merged commit 8f27956 into ansvisor:main Oct 28, 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