Skip to content

fix(rbac/ui): hide write controls on Manage Prompts & Manage Topics for Analyst role #140

Closed

Description

@gkhngyk

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 on profiles.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's profile.role plus a couple of derived flags (canManage, which is true for admin / 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 lint clean
  • 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:

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

Activity

  1. added
    bug
    help wanted
    on Jun 1, 2026
  2. added a commit that references this issue on Jun 1, 2026
    589accd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions