Skip to content

feat(rbac/ui): hide write controls on Manage Prompts & Manage Topics for non-admin/manager roles - #141

Merged
gkhngyk merged 1 commit into
mainfrom
feat/rbac-role-gate
Jun 1, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
feat/rbac-role-gate

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Closes #140.

What

Analyst-role users (and agency_partner) were seeing Generate Suggestions, Save Selected, Add Prompt, Add Topic, Edit, Delete — all the write controls — even though the DB-side RLS on prompts / prompt_sets / topics restricts inserts to admin | manager. Clicking any of them fired the action, Postgres rejected it, and the user got a generic 'Failed to save' toast.

Pieces

Reusable RBAC primitive (mirrors the PlanProvider / useFeatureGate pattern)

  • web/src/components/providers/role-provider.tsx — RoleProvider + the TeamRole type. Lives next to the provider so client components can import the type without crossing the 'use server' boundary in lib/actions/team.ts.
  • web/src/hooks/use-user-role.ts — returns { role, canManage, canAdmin }.
    • canManage = admin | manager (matches the DB RLS gate on prompts, prompt_sets, brand_domains, content_opportunities, topics).
    • canAdmin = admin only (matches team management + Anthropic-key write gate).
  • web/src/components/auth/role-gate.tsx — small <RoleGate roles={[…]} fallback?> sugar wrapper. Not used in this PR's diff but included so the next surface can use either the hook or the wrapper without a fresh round-trip through the codebase.
  • The dashboard layout already fetches profile.organization_id / onboarding_completed; this PR adds role to the same select and passes it into RoleProvider.

Apply it to the two pages this issue called out

  • Manage Prompts (brands/[id]/prompts/page.tsx): the entire top 'Add Prompt + AI Generate' grid and the 'Review AI Suggestions' Save Selected card are wrapped in {canManage && …}. Read-only users see a small <Lock> banner explaining 'ask an admin or manager to make changes'.
  • Manage Topics (brands/[id]/topics/page.tsx): per-row Pencil + Trash2 controls and the 'Add Topic' form are gated. Same banner in the bottom slot.

Test plan

  1. Sign in as an admin / manager → behavior is unchanged: generate, save, add, edit, delete all work.
  2. Sign in as an analyst → both pages render the listing (read) plus the read-only banner; no write controls visible; no broken toasts on click because there's nothing to click.

Out of scope (separate PRs that should reuse useUserRole)

  • Per-row edit / delete inside the prompt list (PromptSuggestionCard mode='manage'). Those controls also fail silently for analyst — same RLS — but the issue specifically called out the suggestions flow as the painful path. Tracked.
  • Other write surfaces gated by the same RLS pattern: brand_domains (Manage Brand Domains), brand_platforms, content_opportunities (Content page approve / dismiss).
  • Settings → Agent: non-admin roles see Save / Remove key controls #131 — Settings → Agent role gate. With useUserRole landed, that issue collapses to a few-line change.

Verification

  • pnpm tsc --noEmit clean
  • yarn lint clean (0 errors, 11 pre-existing warnings)

…for non-admin/manager roles

Closes #140.

Analyst-role users (and agency_partner) were seeing Generate Suggestions,
Save Selected, Add Prompt, Add Topic, Edit, Delete — all the write
controls — even though the DB-side RLS on prompts / prompt_sets /
topics restricts inserts to admin | manager. Clicking any of them
fired the action, Postgres rejected it, and the user got a generic
'Failed to save' toast with no explanation.

The fix mirrors the existing PlanProvider / useFeatureGate pattern:

- RoleProvider + TeamRole type in web/src/components/providers/role-provider.tsx.
- useUserRole() hook returning { role, canManage, canAdmin }.
  canManage matches the admin-or-manager RLS gate used across prompts,
  prompt_sets, brand_domains, content_opportunities — the same shape
  the DB enforces. canAdmin is the stricter admin-only check.
- Optional <RoleGate roles={[...]} fallback?> sugar wrapper.
- The dashboard layout already fetches profile.organization_id /
  onboarding_completed; just add role to the select and pass it into
  RoleProvider.

Then apply canManage on the two pages this issue called out:

- Manage Prompts: the top 'Add Prompt + AI Generate' section and the
  'Review AI Suggestions' Save Selected card are gated. Read-only users
  see a small banner explaining 'ask an admin or manager to make changes'.
- Manage Topics: per-row Pencil + Trash2 controls and the bottom Add
  Topic form are gated. Same banner pattern in the empty space.

Per-row edit / delete inside the prompt list (PromptSuggestionCard
mode='manage') is out of scope for this PR — those controls also fail
silently for analyst, but the issue specifically called out the
suggestions flow as the painful path. Tracked in the issue body as a
follow-up that should reuse useUserRole once the primitive exists.
@gkhngyk
gkhngyk merged commit 589accd into main Jun 1, 2026
3 checks passed
gkhngyk added a commit that referenced this pull request Jun 1, 2026
…Admin (#142)

Closes #131.

Same pattern as #141 — server-side /api/settings/anthropic-key PUT and
DELETE already enforce role === 'admin' (returning 403 for anyone else),
but the AgentSection rendered the Save input + Remove Key button to every
member. Clicking either just produced an unhelpful toast.

Wraps the new-key input, Save button, and Remove key button in a single
canAdmin branch via the useUserRole hook landed in #141. Non-admin users
see the same configured / unconfigured status block (GET is intentionally
member-readable — only metadata is returned, never the key) plus a small
'Admin-only setting' banner explaining why the controls are gone.

This branch was successfully deployed

1 active deployment
Preview — 6f9d60b6 Deployed Jun 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant