# Team and roles, with the rules in code

The team page lists the people with their roles, invites a new one and opens only for administrators. What each role may do lives in code, one permission table per module: the buttons ask it before they show, and the green coffee and wholesale apis ask it again before they change anything.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/screens/roles-and-permissions/

*Settings, Team and roles, of the demo's roastery as its administrator sees it, rendered when the site was built from the kit's own components, with the demo's data.*

A live screen, rendered from the kit's components on the page: https://meadowstack.com/screens/roles-and-permissions/

Open it in the live demo: https://meadowstack.com/demo/?lang=en#/fernhill/nastaveni/tym?jako=fh-ondrej

From the example app [An app for a roastery with an online shop](https://meadowstack.com/examples/coffee-roastery-production/).

## What the page shows

Settings has the same four pages in every app built on Meadowstack: General, Team and roles, Connected apps, and MCP and AI ([the settings pattern](https://meadowstack.com/patterns/settings/)). The last three are for administrators, and at the roastery that is the owner.

Team and roles lists the six people with their e-mails, each role a neutral badge. Its one action is Invite: an e-mail and a role, then a row that appears at once marked "Invited, waiting to accept", and a link that stays valid for 7 days. An invitation can be taken back after a confirmation. The roles to pick from are the company's own: Owner, Roaster, Quality and cupping, Operations and e-shop, Wholesale and Accountant.

## Where the rules live

Each module keeps its rules in one file, `domain/permissions.ts`: a table from an action to the roles that may take it, plus rules that need the record, such as an invoice's state. The workspace joins the modules' tables into the one `can` that the kit's `<Can>` and `useCan` ask, so a component never compares roles itself ([permissions](https://meadowstack.com/patterns/permissions/)).

Opening the settings beyond General asks `settings.read`, which only an administrator has. The green coffee and wholesale apis ask the same table before they change anything, and a real backend does the same in Postgres row-level security ([Supabase](https://meadowstack.com/integrations/supabase/)).

## A role decides what the data shows

- **The roaster** sees the cafés' standing orders, because he roasts for them, and nothing else of wholesale: no prices and no balances.
- **The margin** is for the owner and the accountant only.
- **Four eyes**: an invoice is sent by the owner or the accountant, and never by the person who prepared it.
- **No access** shows the page's header and a quiet note instead of the content. On Team and roles it says "Only administrators see the team.", and the demo adds a hint to switch role in its bar at the bottom.

Buttons for actions a role may not take are not drawn at all, so nobody presses a button that the save then refuses.

## What AI coding tools get wrong here

- **Role checks inline**: `if (user.role === "admin")` in a component, with a different list in each file. Here a component asks `can("wholesale.invoice.send", invoice)`, and the answer comes from one table.
- **A hidden button with an open api**: the button disappears and the endpoint still takes the request. Here the green coffee and wholesale apis ask the same table and refuse.
- **A grid of checkboxes the code never reads**: a roles matrix on the settings page, saved to a table the app does not consult. Here the rules are code, reviewed and tested with the app, and the page shows who has which role.
- **Roles that only hide menu items**: the sidebar loses an entry while widgets, search results and notifications still carry prices and margins. The framework's rule asks the role with the record wherever the data appears.

## Who may do what at the roastery

*A selection from the permission tables of the demo roastery's modules (domain/permissions.ts in each) on 2 December 2026, with the roastery's own role names.*

| Permission | What it allows | Who may |
| --- | --- | --- |
| roast.plan | Plan the roast batches | Owner, Roaster |
| roast.cupping | Record a cupping | Owner, Quality and cupping |
| green.request | Request green coffee from a supplier | Owner, Roaster, Operations and e-shop |
| green.eudr | Write a lot's EUDR reference | Owner, Accountant |
| shop.order.pack | Pack an online shop order | Owner, Operations and e-shop |
| shop.order.pay | Mark an order paid | Owner, Accountant |
| wholesale.price.read | See the cafés' prices | Owner, Wholesale, Accountant |
| wholesale.margin.read | See what each coffee costs and earns | Owner, Accountant |
| wholesale.invoice.send | Send an invoice, never one they prepared | Owner, Accountant |
| scenario.toggle | Switch a scenario on or off | Owner, Operations and e-shop |
| scenario.run | Run a test of a scenario | Owner, Operations and e-shop, Wholesale |
| settings.read | Open Team and roles, Connected apps, MCP and AI | Administrators (the owner) |

## Rules that read the invoice itself

*The wholesale module asks its table first, then the invoice: nobody sends a draft they prepared (four eyes), only a sent invoice is marked paid, an overdue café is reminded up to three times, and a draft is deleted by the person who prepared it or by the owner.*

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

```ts
/** Without a subject the answer is "could, for some record" (a button in a list); with one, for that invoice. */
export function canWholesale(person: Who, action: string, subject?: unknown) {
  if (!(rules[action as WholesaleAction] ?? []).includes(person.role)) return false;
  const invoice = subject as InvoiceSubject | undefined;
  if (!invoice || !action.startsWith("wholesale.invoice.")) return true;
  switch (action) {
    case "wholesale.invoice.send":
      // Four eyes: nobody sends an invoice they prepared themselves.
      return invoice.status === "draft" && invoice.createdBy !== person.id;
    case "wholesale.invoice.pay":
      return invoice.status === "sent";
    case "wholesale.invoice.remind":
      return invoice.state === "overdue" && invoice.reminders < MAX_REMINDERS;
    case "wholesale.invoice.delete":
      return invoice.status === "draft" && (person.role === "owner" || invoice.createdBy === person.id);
```

## Questions

### Where are the permissions defined?

In code, one table per module (domain/permissions.ts): each action names the roles that may take it, and some rules also read the record, such as an invoice's state. The screens ask the table through the kit's Can and useCan, and in the demo the green coffee and wholesale apis ask it again before a change.

### Can an administrator change what a role may do on the settings page?

No. The page assigns people to roles and invites new ones. What a role may do is a change to the module's permission table in code, so it is reviewed and tested with the rest of the app.

### What is the four-eyes rule in the demo?

Nobody sends an invoice they prepared themselves. The owner, the accountant and the person in wholesale prepare invoices as drafts, and the owner or the accountant sends one, never their own draft.

### What does a person without access see?

The page's header and a quiet note saying who may open it, such as "Only administrators see the team." Buttons for actions their role may not take are not shown at all.

### Do the rules hold in the database too?

A real backend enforces the same rules, for example with Postgres row-level security on [Supabase](https://meadowstack.com/integrations/supabase/). The demo simulates its api in the browser, where the green coffee and wholesale modules ask the same tables.

## Related

- [An app for a roastery with an online shop](https://meadowstack.com/examples/coffee-roastery-production/) (Example app)
- [Permissions](https://meadowstack.com/patterns/permissions/) (Patterns)
- [A lot record that traces one step back and one step forward](https://meadowstack.com/screens/lot-traceability/) (Screens)
- [A roast plan worked out from the orders](https://meadowstack.com/screens/roast-plan/) (Screens)
- [A workflow canvas where a test run walks the steps](https://meadowstack.com/screens/workflow-canvas/) (Screens)
- [The approval flow](https://meadowstack.com/patterns/approval-flow/) (Patterns)
