# Permissions

In Meadowstack every rule about who may do what lives in one file per module, `domain/permissions.ts`, and screens ask it with the record, `can("job.read", job)`, through `<Can>`, `usePermission` or `useCan`, never by comparing roles. The api asks the same function before every change and a real backend enforces it again with Postgres row-level security, so a role changes the data a person sees, not only the menu.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/patterns/permissions/

*The print shop's rules in the demo: one table of actions by role, where a customer may read jobs and decide on proofs and nothing else. The function under it narrows each action by the record. Read from the repository when the site was built.*

From `apps/demo/src/modules/print/domain/permissions.ts` in the Meadowstack repository:

```ts
const rules: Record<string, string[]> = {
  "job.read": ["sales", "manager", "prepress", "press", "customer"],
  // The shop's insides: a colleague's proof check, the machine plan, internal steps in the history.
  "job.internal": ["sales", "manager", "prepress", "press"],
  "job.create": ["sales", "manager"],
  // The assistant's draft of a job from an enquiry, with its calculation (Foldline's AI moment): the approver signs
  // the price off as the second pair of eyes, customer care sends it.
  "job.draft": ["sales", "manager"],
  "job.advance": ["sales", "manager", "press"],
  "job.edit": ["sales", "manager"],
  "job.cancel": ["sales", "manager"],
  // A note in the history is the shop's own: a customer does not see it.
  "job.note": ["sales", "manager", "prepress", "press"],
  "proof.request": ["sales", "manager"],
  "file.check": ["prepress", "manager"],
  "proof.make": ["prepress", "manager"],
  "proof.check": ["prepress", "manager"],
  "proof.decide": ["customer", "sales", "manager"],
  "mail.read": ["sales", "manager", "prepress"],
  "mail.send": ["sales", "manager", "prepress"],
  "machine.read": ["sales", "manager", "prepress", "press"],
  "machine.plan": ["manager"],
  "machine.done": ["press", "manager"],
};
```

Open it in the live demo: https://meadowstack.com/demo/?lang=en#/foldline/editor/d-etiopie?jako=ik-tereza

## The flow, step by step

The demo's print shop, as a customer meets it: she signs in to approve the proofs of her own jobs.

1. **Sign in.** Her role is customer. The table above lets a customer read jobs and decide on proofs, and nothing else: no mailbox, no machines, no quotes.
2. **Her jobs only.** Another customer's job does not exist for her. The rule is not a filter in a page: `job.read` with a job answers yes only when the job's customer is her (`ownJob`), and the api filters by the same rule.
3. **A proof.** A proof the shop sent her she can approve or return. On anything else the actions are not there at all, hidden rather than greyed out.
4. **The history.** The job's history shows her side of it, from the quote to the delivery. The shop's notes and internal steps, such as a colleague's check of the proof, never reach her.
5. **Inside the shop.** The prepress operator who made a proof cannot check it: `proof.check` needs a proof someone else made. Only the production manager plans the machines; everyone else in the shop reads the plan.
6. **A page she may not open.** It shows `NoAccess` under its header, so she knows the page exists and is closed to her, never a blank screen.
7. **A request sent by hand.** The api asks the same function and refuses it. In the starter, the sentence is "You don't have permission to do that.", and with a real database the row-level security policies refuse it again.

## The rules and where each one is enforced

| Rule | Where it is enforced |
| --- | --- |
| A module's rules live in one file | `domain/permissions.ts`: a table of actions by role, then conditions on the record; `.claude/rules/apps.md` |
| Components ask, they never compare roles | the kit's `Can` (`do` and `on`), `usePermission` and `useCan`; a rule in the root `AGENTS.md` |
| Hide what a person cannot do | `Can` renders nothing without the permission; the one exception is a bulk bar's disabled button, whose tooltip says why |
| A page someone may not open says so | `NoAccess` under the page header, inside its `Screen` |
| The api asks the same function before every change | the starter's api refuses with "You don't have permission to do that."; api tests "never lets a member decide" and "never lets anyone decide on their own request (four-eyes)" |
| The database enforces it again | the policies in `docs/recipes/examples/supabase/0001_requests.sql`; a draft someone else writes is "not found", the way row-level security hides a row |
| A new action is registered | its prefix in the module manifest's `actionPrefixes`, or it is refused for everybody (`docs/recipes/permissions.md`) |
| Roles change the data, not only the menu | ask with the record in widgets, feeds, the bell, the palette and the assistant answers as on the pages |
| The demo's default visitor meets no wall | the audit's `no-wall` rule: no `NoAccess` and no empty view on any route (`docs/audit.md`) |
| The rules are tested | the starter's `test/requests-permissions.test.ts`, down to "refuses actions it does not know"; `docs/recipes/permissions.md` adds an e2e per role and the database statements run as an ordinary role |

Roles changing the data came from a finding. The audit of 25 Oct 2026 in the demo's [construction company](https://meadowstack.com/examples/construction-site-management/) found that the client's supervisor could see "Cost against plan" and other sites in the "What changed" and "Lately" lists. Now `site.costs`, the company's cost against its plan, is an action no external role has, and a screen, a feed, the bell and the assistant all ask `site.read` with the site.

## What AI coding tools build instead

- **`if (user.role === "approver")` in a component.** The rule is copied into every screen that needs it and drifts, and a new role means a hunt through the code. Here the component asks `can("request.approve", request)`.
- **A hidden button the api trusts.** The page hides it, the endpoint accepts the request from anyone who sends it. Here the api asks the same function, and the database asks again.
- **A disabled button with no reason.** People cannot tell whether they lack the right, the data or the timing. Hide it, or say who decides.
- **A page that renders nothing** for a role without access, or a silent redirect to the start. `NoAccess` says the page exists and is closed.
- **Roles that change only the menu.** The customer cannot open the costs page, but the dashboard widget, the activity feed and the bell still show internal margins and other clients.
- **A backend that connects as the table's owner,** or with a key that bypasses row-level security, so every policy is skipped without a sound.

## Where to see it

- [Roles and permissions](https://meadowstack.com/screens/roles-and-permissions/): the settings page where an admin gives people their roles.
- [Proof approval](https://meadowstack.com/screens/proof-approval/): what a customer signed in to approve proofs can see and do.
- The [approval flow](https://meadowstack.com/patterns/approval-flow/), whose four-eyes rule is one line of a permissions file, and the [audit trail](https://meadowstack.com/patterns/audit-trail/), filtered by the same rules.
- The [Supabase integration](https://meadowstack.com/integrations/supabase/), where the rules become row-level security policies.

## Example apps with pages some roles may not open

*Which example apps and modules render the kit's NoAccess, computed from the demo's code when the site was built.*

| Example app | Modules that render it |
| --- | --- |
| [An app for a packaging and print company](https://meadowstack.com/examples/packaging-print-production/) | Jobs, Print file editor, Quotes, Cutting dies, Colour |
| [An app for a studio or agency](https://meadowstack.com/examples/agency-capacity-planning/) | Capacity, Tasks, Time, Finance, Channels |
| [An app for a haulage company](https://meadowstack.com/examples/haulage-dispatch-planning/) | Freight, Drivers, Costs, Reports |
| [An app for a roastery with an online shop](https://meadowstack.com/examples/coffee-roastery-production/) | Wholesale |
| [An app for a sports club](https://meadowstack.com/examples/sports-club-court-bookings/) | Club, Coaching, Rentals |
| [An app for a construction company](https://meadowstack.com/examples/construction-site-management/) | Sites, Schedule, Crews, Machines, Site diary, Drawings, Foreman's phone, Budget |
| [An app for an accounting office](https://meadowstack.com/examples/accounting-office-documents/) | Documents, Bookkeeping, Official mail, Invoicing |
| [An app for restaurants and venues](https://meadowstack.com/examples/restaurant-staff-rota/) | Shifts, Kitchen, Food safety, People |
| [An app for one person](https://meadowstack.com/examples/personal-app-for-a-creator/) | Brand deals, Content, Money, Training |

Read from the demo's source when the site was built: 9 of 9 example apps render <NoAccess>, in the modules named here as each app's sidebar names them.

## The same rules in Postgres

*The starter's Supabase example: the read and create rules of its permissions file as row-level security policies, so a query that skips the screens still gets only what the person may see.*

From `docs/recipes/examples/supabase/0001_requests.sql` in the Meadowstack repository:

```sql
-- canFor "request.read": a draft is private to its author and the admins.
-- A row this hides is "not found" to the api, the way the simulated api answers.
create policy requests_select on public.requests for select
  using (status <> 'draft' or requester_id = auth.uid() or public.app_role() = 'admin');

-- canFor "request.create": everyone with a role asks, as themselves, as a draft.
create policy requests_insert on public.requests for insert
  with check (public.app_role() is not null and requester_id = auth.uid() and status = 'draft');
```

## Questions

### Is hiding a button enough to protect an action?

No. Hiding keeps the screen honest, but the api asks the same rules before every change, and with a real database the row-level security policies refuse it again. A request sent by hand gets the same answer as the screen would.

### 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 bar, where a disabled button with a tooltip says why none of the selected rows qualify, and a decision that belongs to someone else names who decides.

### Where do the permission rules live?

In one file per module, domain/permissions.ts: a table of actions by role, then conditions on the record, such as a draft being private to its author. The screens, the api and the tests all call the same function.

### Can a customer or another external role see internal costs?

No. An external role never sees internal costs, margins or other clients' records, in widgets, feeds, notifications, search results and assistant answers as on the pages, because each of them asks with the record instead of checking the menu.

### Do I need row-level security if the api already checks?

It is the second lock. The starter's Supabase example writes the same rules as policies, so a query that skips the api still gets only the rows the person may see. Check that the api does not connect as a role that bypasses them.

## Related

- [Team and roles, with the rules in code](https://meadowstack.com/screens/roles-and-permissions/) (Screens)
- [The approval flow](https://meadowstack.com/patterns/approval-flow/) (Patterns)
- [The audit trail](https://meadowstack.com/patterns/audit-trail/) (Patterns)
- [Postgres: the person in a transaction setting](https://meadowstack.com/integrations/postgres/) (Integrations)
- [Supabase: the rules in the database](https://meadowstack.com/integrations/supabase/) (Integrations)
- [A time-off request waiting for a decision](https://meadowstack.com/screens/time-off-approval/) (Screens)
