Handling Complex Role-Based UI Permissions
Build dynamic navigation and component-level access around explicit capabilities. Keep React permission checks consistent with tenant scope and server authorization.
A dashboard starts with a simple rule: admins can see everything. Then editors can publish but cannot manage members, auditors can view invoices but cannot export them, and one user belongs to two organizations with different roles. A few role checks become a permission system spread across the component tree.

The useful abstraction is a capability: a named action the current principal can perform within a scope. Roles group capabilities. Resource attributes, ownership, and workflow state can further restrict them. Keep that vocabulary consistent across navigation, pages, and individual controls.
The Problem: Role Names Embedded in Components
// A role name does not explain the action being authorized.
const showPublish = user.role === 'admin' || user.role === 'editor';
const showExport = user.role === 'admin';If every component interprets the role independently, a new role requires a search across the entire frontend. The same permission may be interpreted differently on a sidebar item, a page, and its action button.
Step 1: Expose a Small Capability Contract
type Capability = 'article:read' | 'article:publish' | 'member:manage';
type Access = {
tenantId: string;
capabilities: ReadonlySet<Capability>;
};
const can = (access: Access, action: Capability) =>
access.capabilities.has(action);Load this context from an authenticated server endpoint for the selected tenant. A capability set is useful for broad UI decisions. A record-specific decision, such as publishing only an assigned article, needs a resource-aware policy or a server-provided action decision as well.
Step 2: Derive Dynamic Navigation
const navigation: Array<{
label: string; href: string; requires: Capability;
}> = [
{ label: 'Articles', href: '/articles', requires: 'article:read' },
{ label: 'Members', href: '/members', requires: 'member:manage' },
];
const visibleItems = navigation.filter(item => can(access, item.requires));For nested menus, filter children first and remove empty groups. Keep product entitlements separate from user authority: a plan can enable an export feature while the current membership still lacks permission to use it. Combine both decisions where the UI needs them.
Step 3: Guard Components and Direct Routes
import type { ReactNode } from 'react';
function Allowed({ access, action, children }: {
access: Access; action: Capability; children: ReactNode;
}) {
return can(access, action) ? children : null;
}
// Use with access from your authenticated tenant context.
// <Allowed access={access} action="article:publish">
// <PublishButton />
// </Allowed>The wrapper removes repeated checks; it does not protect the API. A route also needs a deliberate unauthorized state when opened directly. While permissions are loading, avoid briefly showing protected actions. Use a neutral skeleton, then render the permitted state.
Tenant Switching and Revocation
Key permission requests by both principal and tenant, invalidate them when memberships change, and discard private cached data when the identity changes. A stale browser tab can still display an old button. The server must reject a revoked action and the UI should refresh its context after the resulting authorization failure.
OWASP recommends deny-by-default authorization and permission validation on every request. Implement the decisive check in the server service or endpoint using the authenticated identity, selected tenant membership, requested action, and target resource.
Test the Policy Matrix
Test allowed and denied actions for each role, cross-tenant access, a missing capability, direct navigation, and a revoked membership. Prefer policy tests plus a few meaningful UI flows over a snapshot for every hidden button. The maintainable outcome is one named policy that every surface can explain.