Repository navigation
Description
Symptom
An Analyst-role user goes to Prompts, adds a topic, generates AI suggestions (Generate works — they're fetched into a separate prompt_suggestions table that any member can read/write), reviews them, clicks Save Selected — and gets a generic red toast:
Failed to save prompts
The same flow as an Admin works fine. From the user's side it looks broken; from the server's side it's working exactly as designed.
Root cause
Database RLS on prompts and prompt_sets only allows admin and manager roles to write. From supabase/migrations/00001_initial_schema.sql:
"prompts: admin/manager insert"— INSERT gated onprofiles.role IN ('admin', 'manager')"prompts: member select"— any org member can read- Same pattern on
prompt_sets
So Analyst gets the data they're supposed to see, but the write call from handleSaveSuggestions (web/src/app/[locale]/(dashboard)/dashboard/brands/[id]/prompts/page.tsx:207) is rejected by Postgres, the catch block runs the generic toast, and the user sees no explanation.
This is intentional access control — Analyst is meant to be read-only — but the UI exposes write controls anyway. Same shape as #131 (Settings → Agent showing Save / Remove key to non-admins).
Proposed fix
Part 1 — introduce a shared role primitive (this PR)
We already have useFeatureGate for plan gating. We need the equivalent for role gating. Add:
web/src/hooks/use-user-role.ts— returns the current user'sprofile.roleplus a couple of derived flags (canManage, which is true foradmin/manager). Source from the existing auth / profile context so it doesn't add a network call.- Optional
<RoleGate roles={[…]}>component that conditionally renders children — small ergonomic wrapper for templates with several gated controls.
Part 2 — apply it to the Manage Prompts and Manage Topics pages
In web/src/app/[locale]/(dashboard)/dashboard/brands/[id]/prompts/page.tsx and web/src/app/[locale]/(dashboard)/dashboard/brands/[id]/topics/page.tsx:
- Hide write controls when
!canManage:- "Generate suggestions" + "Save Selected" + "Review AI Suggestions" panel
- Manual prompt add row
- Edit / delete row actions
- Bulk activate / deactivate
- Replace them with a small read-only banner: "You're viewing this page as Analyst. Ask an admin or manager to make changes."
- Selecting / viewing existing prompts still works.
Same treatment on the topics page for: add topic, rename, delete, toggle active.
Acceptance criteria
-
useUserRole()hook + (optional)<RoleGate>component exist and are used in both pages - Analyst sees no write controls on Manage Prompts and Manage Topics — only the read-only listing + the banner
- Admin / manager flow is unchanged
- Agency partner is gated the same as Analyst (the role exists per
TeamRole, currently has the same RLS posture) -
pnpm tsc --noEmit+yarn lintclean - No new server-side checks needed — RLS continues to be the source of truth; this is purely a UI-clarity fix
Out of scope
Other surfaces that have the same admin/manager RLS but still expose write controls to Analyst — drive-by audit picked these up while writing this issue:
brand_domains(Manage Brand Domains screen)brand_platforms(the brand setup screen)content_opportunities(Content page approve / dismiss)- Settings → Agent key (already tracked in Settings → Agent: non-admin roles see Save / Remove key controls #131)
Each of those should follow this PR using the same useUserRole() primitive — separate PRs to keep diffs small. This issue's PR sets the primitive up; the rest become small follow-ups.
Also out of scope:
- Re-litigating the role hierarchy (whether Analyst should have write on prompts). Status quo: no, Analyst is read-only. If that changes, it's a separate decision and means dropping the RLS gate too.
Related
- Settings → Agent: non-admin roles see Save / Remove key controls #131 — Settings → Agent: non-admin sees Save / Remove controls (same pattern; will use this hook once it lands)
Activity
feat(rbac/ui): hide write controls on Manage Prompts & Manage Topics …