# Concepts

A Strig decision answers one question: may this subject do this permission to this resource? This page takes the question apart, so you can write your first policy. Every answer on it is real output from the engine. Pick a command, and the bench shows the case.

## The question

A request has three parts.

- **Subject.** Who asks: an id, roles (each one optionally limited to a place), principals and attributes. You build it from what you know about the caller. Strig does not look anyone up.
- **Permission.** A dotted key from the catalog, such as `document.view`.
- **Resource.** What is touched, written as a path of steps from the most specific to the broadest: `document:spec`, `project:apollo`, `org:acme`. A `global` step ends every path without being written.

The answer is a `Decision`: `effect` (`allow` or `deny`), `reason`, `matched` (every grant that applied) and `warnings`. When nothing applies, the answer is `deny`.

Ana has a grant for `document.view`. Bob has none.

The same program asks every question on this page. It reads the policy file, finds the subject and builds the resource path from its arguments.

## Permissions

A permission is a dotted key that the catalog declares. The line that declares it also says where a grant of it can sit: `permission document.view on project, document`. A permission the catalog does not declare is denied, with the reason `unknown_permission`.

`implied by` lets a broader permission carry a narrower one. `document.invoice.export` is implied by `document.export`, so a grant of `document.export` holds for it. Ana has that grant and asks for the narrower permission:

`reason.permission` names the permission the grant was written for, not the one asked. The implication does not run the other way:

## Targets and broad targets

A grant has a target: the step of the path it sits on. `on project` matches whichever project is on the path, and `on document:spec` matches that one document. The position of the matching step is the rank, counted from the resource itself.

```text title="Path and rank"
document:spec   project:apollo   org:acme   global
rank 0          rank 1           rank 2     rank 3
```

`kinds` lists the kinds of step a grant can sit on. `broad org` adds a kind that any permission accepts, such as an organization or a tenant. `global` is always broad. A grant on any other kind is refused when the file loads.

`in org:acme` is the scope, not the target. It says the resource path must pass through `org:acme` for the grant to apply.

`g-view` sits on `org`, a broad target, so it reaches `document.view` even though that permission lists only `document`. Dan asks about a document in Acme, then in another organization:

## Roles

A role is a name that grants can address. `role editor inherits viewer` gives editors whatever viewers hold. A subject holds a role everywhere or only inside a scope: `user:ana has role editor in project:apollo` holds for resources whose path goes through `project:apollo`.

A grant names who it is for with one of five matchers: `anyone`, `role <name>`, `user <id>`, `principal <id>` and `relation <name>`. A relation grant applies to whoever holds that relation on the step the target matched, read from stored tuples. This page does not follow relations further.

Ana is an editor in Apollo. The same question, asked about Apollo and about Zephyr:

In Apollo the answer is `allow` only because `editor` inherits `viewer`: `document.edit` needs `document.view`, and `g-view` is written for `viewer`. In Zephyr, Ana holds no role, so no grant applies.

The Actors syntax writes the same policy grouped by who acts. The three syntaxes, Blocks, Rules and Actors, describe one policy, and the [policy language](https://strig.vercel.app/docs/policy-language.md) shows them side by side.

## Conditions

A grant can end with `when`, followed by a condition over `subject.*`, `resource.*` and `env.*` attributes. The grant applies only when the condition holds. Conditions compare values, test membership, combine with `and`, `or` and `not`, and can test text, lists, addresses and the time of day. The [policy language](https://strig.vercel.app/docs/policy-language.md) lists them.

| User | `subject.dept` | Verdict | `reason.kind` |
| --- | --- | --- | --- |
| `ana` | `"legal"` | allow | `grant`, by `g-legal` |
| `bob` | `"sales"` | deny | `no_applicable_grant` |
| `eve` | none | deny | `no_applicable_grant`, with a warning |

Bob's condition is false, so `g-legal` does not apply. Eve has no `dept` at all, and the engine cannot tell:

A condition that cannot be evaluated never opens access. An `allow` that depends on it does not apply, a `deny` that depends on it still does, and the cause goes to `warnings`.

## Time zones

*Added in 0.7.0.*

Office hours are local. `time_between` and `weekday_in` read UTC unless you give them an IANA zone with `tz`. The same condition, in the three syntaxes and in JSON:

```text title="Rules"
g-hours: allow role editor to document.edit on document when time_between "09:00" "18:00" tz "America/Sao_Paulo"
g-monday: allow role editor to document.view on document when weekday_in ["mon"] tz "Europe/Lisbon"
```

```json title="JSON"
{ "op": "time_between", "from": "09:00", "until": "18:00", "tz": "America/Sao_Paulo" }
```

- **The instant decides the local time.** Daylight saving time follows the zone's rules. A window inside the hour that a spring-forward jump skips never holds that day. The hour that a fall-back repeats holds on both passes.
- **Midnight and the date are local.** A window that crosses midnight (`"22:00"` to `"06:00"`) and the weekday both follow the local date.
- **An unknown name is refused when the policy loads**, with position and hint. A condition built without validation fails when evaluated, and an `allow` that errors never applies.
- **The trace shows the local time it used**, for example `12:30 America/Sao_Paulo (UTC-03:00)`.
- **Without `tz` nothing changes.** A policy that uses `tz` exports its JSON with `"schema": 3`, so a reader of schema 2 refuses it. See [Upgrading](https://strig.vercel.app/docs/upgrading.md).

The zone database is the default feature `tz` of the `strig` crate. It adds about 0.9 MB to the `.wasm` (2.70 MB to 3.58 MB, 657 KB to 785 KB gzipped). Build with `default-features = false, features = ["audit"]` to leave it out; a `tz` in a policy is then refused at load.

## Prerequisites

`depends_on` makes one permission require others: holding it means holding each listed permission on the same resource. `implied by` widens what a grant reaches. `depends_on` narrows what a grant is worth.

`document.edit` depends on `document.view`. Bob has a grant for `document.edit` and none for `document.view`:

`g-edit-bob` applied, and it is not enough. Ana holds both:

The catalog refuses a prerequisite that cannot be granted wherever the permission that needs it can, and it refuses a cycle.

## Fields

A grant can cover some fields of the resource. The catalog declares the fields a permission can be narrowed to, and a grant lists the ones it covers:

```text title="Rules"
permission document.edit on document fields title, body, status

g-edit: allow role editor to document.edit on document
g-freeze: deny role editor to document.edit on document fields status
```

Ana is an editor. Asked about `document.edit` on `title`, the answer is `allow`; on `status`, `deny`, by `g-freeze`. Asked about the whole document, the answer is `deny`, because editing the whole resource needs every field. `g-edit` has no `fields`, so it covers all of them, exactly as before. A grant that names a field the permission does not declare is refused when the policy loads, with the declared fields as the hint.

[Field permissions](https://strig.vercel.app/docs/modeling/field-permissions.md) covers how fields combine with `implied_by`, `depends_on` and the strategies.

## Which grant decides

Several grants can apply to one question. The strategy picks the one that decides. The default, `most_specific`, lets the grant on the closest step decide, and a `deny` beats an `allow` on the same step. An `allow` for one document and a `deny` for the whole project:

`g-edit` is on `document:spec`, at rank 0, so it beats the freeze for that one document. For `document:notes` only `g-freeze` applies.

Add `strategy deny_overrides` after `broad org` and ask about `document:spec` again.

The same two grants matched, and the verdict flipped. The strategy is one line of the policy:

| Strategy | The deciding grant |
| --- | --- |
| `most_specific` | On the closest step that has any grant; a `deny` first on that step. The default. |
| `deny_overrides` | A `deny` at any step. |
| `permit_overrides` | An `allow` at any step. |
| `first_applicable` | The first applicable grant in source order. |

## Reading the answer

The engine reaches a verdict in four steps:

1. An unknown permission is denied.
2. Overrides run in order, and the first one that answers decides.
3. A grant is a candidate when all of these hold:
   - its permission is the one asked, or one that implies it;
   - the resource path passes through its scope;
   - its target matches a step of the path;
   - its subject matches and its condition is true.
4. The strategy picks the deciding grant. With no candidate, the answer is `deny`. Then `depends_on` is checked, and a missing prerequisite turns an `allow` into a `deny`.

`reason.kind` says which step decided:

| `reason.kind` | It means |
| --- | --- |
| `grant` | A grant decided: `grant_id` (`grantId` in Node), its `permission`, its `rank` and the `strategy` that picked it. |
| `no_applicable_grant` | Nothing applied. The answer is `deny`. |
| `unknown_permission` | The permission is not in the catalog. The answer is `deny`. |
| `missing_prerequisite` | The permission would be allowed, but one it `depends_on` is not. |
| `override` | An override answered before the grants, with its `name`. |

`matched` lists every grant that applied, in source order, not only the one that decided.

## Separation of duties

Some permissions must not meet in one person. The catalog declares which, and governance refuses the change that would put them together:

```text title="Rules"
mutually-exclusive payment_duties of roles members [maker, checker] anchor org
conflicting-actions four_eyes first [payment.create] then [payment.approve] history trail distinct_roles true
always separation-of-duties
```

- `payment_duties`: nobody holds `maker` and `checker` in the same org, whether directly, by inheritance, by a grant or by an elevation.
- `four_eyes`: whoever did `payment.create` on a resource cannot do `payment.approve` on it. The engine reads the history from the `trail` attribute of the resource, a list such as `["payment.create:ana"]`, in every decision. A history it cannot read denies.
- `always separation-of-duties`: an invariant, so governance refuses a grant that would create a clash.

A policy that declares them exports `"schema": 5`. See [Separation of duties](https://strig.vercel.app/docs/governance/separation-of-duties.md).

## Reading your own writes

Every accepted change gets a revision for its data scope, a text like `tenant@42`. A decision can name the revision it must not be older than:

```js
await saveGrant(grant);                       // returns the revision, 'tenant@42'
client.requireRevision('tenant@42');          // until the grants reach it, every check denies
```

A reader behind the revision denies instead of answering from older grants. [Consistency](https://strig.vercel.app/docs/storage/consistency.md) covers the stores, the server header `x-strig-min-revision` and the `revision_not_reached` error.

## What Strig leaves to you

- **Who the subject is.** Strig reads the subject you pass and decides for it. In a browser, `PermissionClient` decides locally to shape the interface, and the server still decides on every operation.
- **Where grants live.** The engine holds configuration, and the grants arrive with each call. Governance returns the new grant set when a change passes its invariants, and you persist it. Removing expired grants from storage is a job of yours.
- **The clock.** You supply the instant of the decision. Without it, a grant with a validity window or a time condition never allows.
- **Anything it has no condition for.** There is no regular-expression condition. Custom condition functions and overrides are code, not policy text: you register them in Rust, JavaScript or Python, and an exception in one denies.
- **IO.** The core does no IO and needs no async runtime. Real time, transports and the HTTP server are separate crates.

Three more parts of the model act on the same grants.

| Part | What it does |
| --- | --- |
| Overrides | Decide before the grants: an owner, a suspended account, a maintenance window. |
| Governance | A change to the grants is applied only if every invariant passes: no escalation, no lockout, your own rules. [Impact](https://strig.vercel.app/docs/governance/impact.md) says who gains and who loses access first. |
| Approvals | A change can wait for an approver, or hold for a limited time as an [elevation](https://strig.vercel.app/docs/governance/elevation.md). |
| Audit | With the `audit` feature, each change, applied or refused, becomes a record. [Verify it](https://strig.vercel.app/docs/audit.md) without trusting the writer. |

## Next steps

- [Policy language](https://strig.vercel.app/docs/policy-language.md): The same policy in Blocks, Rules and Actors.

- [Reference](https://strig.vercel.app/docs/reference.md): Types, fields and every reason the engine can give.
