# Panel: Access rules

[Guide index](../v1.md)

Access rules decide who may reach what. They are enforced by Determesh's code, never by the AI, and every decision is recorded in the audit log.

API: Access rules section (`/access-rules`), see the [API reference](api-reference.md).

## The basics

- **Nothing is open until a rule opens it.** A new resource is reachable by nobody until a rule allows it.
- **Inside a rule, every condition must hold. Between rules, any one is enough.**
- **Permissions are separate:** a rule allows **Read**, **Write** and/or **Delete**, and each method is one of those. A user allowed to read payslips cannot apply for leave unless a rule also allows write on that method.
- **Rules on a folder can cover everything inside it.**
- **A rule on just one method also lets the user find the resource;** they can call only the methods their rules allow.
- Resources that decide access themselves (see below) are only *advised* by the rules.
- Verifiers and the fallback resource are open to everyone.

## Adding a rule

**Add rule**:

| Field | Meaning |
|---|---|
| **Applies to** | **Every resource**, or **One resource or folder**. For one resource: **Only this method** or **All of its methods**; for a folder: **Also everything inside it** or the folder alone. |
| **Allows** | Read, Write, Delete (any of them). |
| **When the user's attributes match** | Conditions on the user's attributes: `attribute` `operator` `value`. Operators: is, is not, contains, is more than, is at least, is less than, is at most. No conditions: anyone. |
| **Also verified by** | A verifier. The rule then allows only while the user holds a current verification by it, and a file or folder asks the user to verify first. |
| **A verification counts for (hours)** | For this rule; empty uses the company's default (24 hours). |
| **Only for one user** | An identity ID, for a rule just for one person. Empty for everyone matching. |
| **From** / **Until** | Dates the rule is valid between. |
| **Note** | For you, such as "managers see the HR folder". |

**Save rule**. The list shows each rule in words; **Edit** and **Delete** change it.

### Rules and folder notes

When a folder keeps notes beside it, a change to the folder's rules asks whether to **apply the same change to the notes** or **leave the notes alone**. The notes' rules started as a copy of the folder's and can also be changed on their own.

## Examples

| You want | Rule |
|---|---|
| Anyone can ask the help centre | Help centre folder, everything inside, Read, no conditions. |
| Only HR staff read the HR folder | HR folder, everything inside, Read, `department` is `HR`. |
| HR staff reach payslips after verifying by email | Payslips resource, Read, `department` is `HR`, also verified by "email verifier", 1 hour. (Rules decide who may use a resource; to return only each person's own records, fill the ID parameter by Determesh: see [Keep each user to their own data](recipes.md#keep-each-user-to-their-own-data).) |
| Managers approve leave | Leave resource, method `approve_leave`, Write, `role` is `manager`. |
| A contractor for March only | Projects folder, Read, only for one user (their identity ID), from 1 March until 31 March. |

## Who decides access: the rules, or the resource itself

When creating an API or code resource you choose:

- **Determesh, from the access rules** (the usual choice): the rules decide.
- **The resource itself:** before using it for a user, Determesh asks it. An API resource gets a POST on its **access check path**; a code resource's `checkAccess(request)` is called. Both receive the user's identity ID, channel IDs and attributes, the method and permission asked for, and `suggestedByPolicy` (what your rules would say). The answer must be `{"allowed": true}` or `{"allowed": false}`; anything else denies that one call. To ask for verification first: `{"allowed": false, "verify": "what it needs, such as the user's work email"}`.

Answers are cached per user so the resource is not asked every time. A refusal is caught at the next call. When your system later **allows** someone it had refused, it must tell Determesh: `POST /v1/access/grant` (`resourceId`, `identityId`, `allowed`) or `POST /v1/access/evict` (`resourceId`, optional `identityId`) to forget cached answers. Changing a user's attributes or a rule clears the affected cache by itself.

## Verification

Some data needs proof of who the user is: a Telegram account says nothing about which employee it is.

- A **verifier** is an API or code resource with the purpose **Verifier**, for example one that emails a one-time code to the user's work email and checks it. When the user is proved, its method answers `{"verified": {"channel": "EMAIL", "externalId": "user@company.com"}}`, or your system calls `POST /v1/access/verified` (`resourceId`, `identityId`, `channel`, `externalId`).
- When verified, the email (or other ID) is **linked** to the user. If another identity already had it (say the same person on email), the two become one identity, keeping both conversations.
- Two ways to require it:
  - **A verification group** (Resources page): every resource in the group is unlocked across the group once the group's verifier proves the user. Used when a resource itself answers "verify first".
  - **A rule's "Also verified by"**: works for anything, including files and folders.
- A verification lasts the rule's hours, or the company's default (24 hours, on the Company page). After it, the user verifies again on their next request.

In chat the assistant asks for what the verifier needs (such as the email, then the code), and only then answers.
