Skip to content

Blog

Roles and permissions UI patterns for business apps

By Bohuslav PoláčekPublished

templates/starter/src/modules/requests/domain/permissions.ts
export function canFor(person: Person): CanFn {
  return (action, subject) => {
    if (!roleActions[person.role].includes(action as Action)) return false;
    const request = subject as PurchaseRequest | undefined;
    // Without a record the question is "may this person ever do it" (show the "New request" button?).
    if (!request) return true;
    const mine = request.requesterId === person.id;
    switch (action) {
      case "request.read":
      case "request.comment":
        return request.status !== "draft" || mine || person.role === "admin";
      case "request.edit":
      case "request.submit":
        return (request.status === "draft" || request.status === "rejected") && (mine || person.role === "admin");
      case "request.withdraw":
        return request.status === "submitted" && mine;
      case "request.delete":
        return request.status === "draft" && (mine || person.role === "admin");
      case "request.approve":
      case "request.reject":
        return request.status === "submitted" && !mine;
      case "request.reopen":
        return (request.status === "approved" || request.status === "rejected") && request.decidedById === person.id;
The starter's purchase requests: a role check first, then one rule per action on the record, four-eyes included (`!mine`). Read from the repository when the site was built.

One table, asked everywhere

Every pattern below rests on one decision: the rules about who may do what live in one file per module, domain/permissions.ts, and nothing else decides. The excerpt above is the starter's. It checks the role first (a member never reaches the approve rule), then answers for the record: whose it is and which state it is in.

The interface only asks. The kit gives three ways to ask the same function:

  • <Can do="request.approve" on={request}> renders its children only when the answer is yes, and takes a fallback.
  • usePermission("request.approve", request) returns the boolean, for a prop such as canDecide.
  • useCan() returns the function, for a handler or a check per selected row.

A component that writes if (user.role === "approver") has copied a rule out of the table, and the api cannot share it. The model in general is on the permissions pattern, and roles as a concept in RBAC. This post is about the screens: what each situation looks like, and what has to stand behind it.

The patterns at a glance

Hide, and disable only with a reason

An action a person cannot take is hidden, not greyed out. A disabled button with no explanation leaves people guessing whether they lack the right, the data or the timing, and they ask support about a button that was never theirs.

The one exception in the framework is the bulk bar: a person ticks five rows and none of them can be approved. The button stays, disabled, and its tooltip says why nothing in the selection applies, because the action is almost available and the reason helps. Bulk actions also name their count ("Approve 2 of 5"), so a person sees that the other three were left out on purpose.

On the server, hiding changes nothing. The starter's api checks every request of a batch with the same function before any of them moves (the second excerpt), so a hand-made request for a row the screen left out fails like the screen would.

A closed page keeps its header

A page someone may not open never renders blank and never redirects silently to the start. It shows its header, so the person knows where they are, and under it NoAccess, with one sentence that says who can and a button back. The demo's orders form uses "Orders are created by a salesperson or an admin." That sentence tells the person whom to ask, which is the only useful thing the page can do for them.

In the code, NoAccess sits inside the page's own Screen, so the page's one h1 is there whatever the person may do. A section inside a page uses <Can fallback={<NoAccess … />}> the same way.

Two rules keep this honest. The demo's default visitor meets no NoAccess and no empty view on any route; the runtime audit's no-wall rule checks it on every screen. And the data behind the page is refused by the api, not only by the route.

Four-eyes, and a reason on every rejection

An approval means that a second person looked. So nobody approves their own record: in the starter's table, request.approve and request.reject answer yes only for a submitted request that is not the person's own (!mine). On the screen the decision card simply is not there for the author. The four-eyes principle and the whole flow are on the approval flow pattern.

A rejection always has a reason. The kit's ApprovalPanel asks for it, the starter's api refuses a rejection without one ("Write why the request is rejected.") and keeps the reason in the history. The person whose request came back reads what to fix instead of guessing.

The starter tests both in its api tests: "never lets anyone decide on their own request (four-eyes)" and "asks for a reason before a rejection, and keeps it in the history". In the database the recipe's sketch puts the same two rules in a trigger and a check constraint, because a policy cannot compare the old row with the new one.

External roles change the data, not only the menu

A client, a customer or a guest who signs in must see less data; fewer menu items is not enough. The trap is everything that is not a page: dashboard widgets, activity feeds, the notification bell, palette results and assistant answers. Each of them has to ask with the record, can("site.read", site), the way the page does.

The demo's construction company is where this came from. The client's supervisor could see the cost against plan and other sites in the activity lists. Now site.costs is an action no external role has, and the site view asks useCan()("site.costs") before it draws the cost figures.

On the server this is a row and column rule, not a menu rule: every query filters by the record, and the database enforces the same with row-level security, so a query that skips the screens still gets only what the person may see.

The server and the database ask again

The browser is not trusted. Anything the interface hides, disables or validates is a convenience for the person; anyone with the developer tools can call the api directly. So each rule is enforced three times, from one source:

  1. The screen asks can(action, record) to decide what to show.
  2. The api asks the same function before every change and refuses with "You don't have permission to do that."
  3. The database enforces it with row-level security for who sees and changes which rows, and a trigger for the state machine and four-eyes.

The api must run as a role that the policies bind: a superuser, a role with BYPASSRLS or the table's owner without force row level security skips every policy without a sound. In Meadowstack 2.1 steps 1 and 2 are in the starter and step 3 is a worked sketch in docs/recipes/permissions.md and a Supabase example; the backend itself is yours. Meadowstack 3.0, an update every licence gets, moves the rules into one policy file per app that also generates the database policies. What each version covers is on the security page.

A prompt for your coding tool

Coding agents asked for a feature tend to add the permission check to the screen only. Paste this before you ask for a new action or role:

text
Before you add or change any action in this app:
1. Name the action <thing>.<verb> and add it to the module's permission table
   (domain/permissions.ts): which roles may ever take it, then the rule on the
   record (owner, state). No role comparisons in components.
2. In the UI, ask the table with the record (<Can>, usePermission, useCan).
   Hide what the person cannot do. Disable only in a bulk bar, with a tooltip
   that says why.
3. A page the role may not open shows its header and then NoAccess with one
   sentence on who can and a way back. Never a blank page or a silent redirect.
4. Nobody approves or rejects their own record. A rejection requires a
   written reason, stored in the history.
5. External roles (client, customer, guest) never see internal costs, margins
   or other clients' records: check widgets, feeds, notifications, search and
   assistant answers with the record, not only the pages.
6. The api calls the same permission function before every change and refuses
   otherwise. Add the matching row-level security policy and, for state
   changes, a trigger that enforces the allowed transitions and four-eyes.
7. Every state change writes a history entry in the same transaction.
8. Add tests: a unit test per rule and exception in the table, an api test
   that sends the forbidden call directly and expects a refusal.

Then run the forbidden calls yourself. The permissions checklist lists each one with a test you can do in the browser's developer tools.

The api asks the same function

templates/starter/src/modules/requests/data/api.simulated.ts
export function moveRequests(actor: Person, ids: string[], move: Move, reason?: string) {
  return request(() => {
    if (move === "reject" && !reason?.trim()) throw new Error("Write why the request is rejected.");
    const targets = ids.map(find);
    for (const target of targets) check(actor, moveAction(move), target);
The starter's simulated api: a rejection without a reason is refused before anything moves, and every request in the batch is checked against the same rules the screen asked.

Questions

Should a button someone cannot use be hidden or disabled?

Hidden. A disabled button with no reason leaves people guessing. The exception is a bulk action that fits none of the selected rows: it stays disabled with a tooltip that says why.

What should a page show to someone who may not open it?

Its header, then a short NoAccess message that says who can open it and offers a way back. Never a blank page or a silent redirect, because the person would not know where they are or whom to ask.

How do I stop people approving their own requests?

Put the rule in the permission table, so approve and reject answer yes only when the record is not the person's own, and enforce it again in the api and in a database trigger. Hiding the button for the author is the visible half only.

Why must a rejection have a reason?

So the person whose request came back knows what to fix, and so the history explains the decision later. The api should refuse a rejection without one, and the database can hold the same rule as a check constraint.

Is hiding menu items enough for client or guest roles?

No. Widgets, feeds, notifications, search and assistant answers can still show internal costs or other clients' records. Each of them must ask with the record, and the database must filter the rows with row-level security.

Two ways

Build it yourself, or have it built

  • Build it yourself

    Build your app on the framework

    Get the codebase these apps stand on and build yours with Claude Code, Cursor or Codex.

    One payment, lifetime updates

    Get the framework
  • Have it built

    Have us build your app

    Tell us how your team works. We plan the app with you and build it on the same framework at a fixed price.

    After launch, Care keeps it improving, from €99 a month.

    from €1,990

    Describe your app