# How access is decided
Source: https://sovaria.in/docs/access-model

Who a request acts as, the order Sovaria checks access in, what happens when a rule allows, denies or nothing decides, and how Postgres row-level security fits in.

Every request to your project is checked on the server before it touches data. Two things decide it: your **access policies** (rules in Sovaria's policy language, managed on the **Policies** page) and, for tables, **Postgres row-level security**. Nothing in the browser or app can skip either.

(Diagram: The order a request is checked in — Each step can stop the request. A DENY always wins over an ALLOW; when nothing decides, the answer is no.)

## Who a request acts as

| Caller | What it sends | Acts as | Row security |
|---|---|---|---|
| Anonymous visitor | The **anon key** only | `ctx.actor.is_anonymous == True`; `auth.role()` is `anon`, `auth.uid()` is `null` | Applies |
| Signed-in app user | The user's access token (with or without the anon key) | `ctx.actor.user_id` and `auth.uid()` are the user; `auth.role()` is `authenticated`; their app roles are in `ctx.actor.roles` | Applies |
| Anonymous app user | The access token from an anonymous sign-in ([Authentication settings reference](https://sovaria.in/docs/auth-settings)) | A signed-in user (`auth.uid()` set, `auth.role()` is `authenticated`) that is still anonymous: `ctx.actor.is_anonymous == True` and `auth.jwt()->>'is_anonymous'` is `true` | Applies |
| Custom API key | `apikey: <custom key>` from your server | Its role in `ctx.actor.roles` (`ADMIN`, `EDITOR` or `VIEWER`; a viewer key only reads unless one of your rules allows more); `auth.role()` is `anon` | Applies |
| Service role key | `apikey: <service role key>` from your server or CI | Full access to the project; `auth.role()` is `service_role` | Bypassed where the policy allows it (see below) |
| Project team member | A console session, or a token from `sov login` | Their member role in the organization | Bypassed where the policy allows it (see below) |

For the service role key and team sessions Sovaria sets `app.rls_bypass` to `on`. Row-level security policies that include `current_setting('app.rls_bypass', true) = 'on'` — the console's presets do — let them see every row; policies without it filter them like any other caller.

> **Warning:** The service role key is allowed by default and bypasses row-level security where policies allow it; only a DENY rule you write can stop it. Keep it on servers; never put it in a browser, a mobile app or a repository.

App users and team members are different people: **app users** sign up to the app you build ([Authentication overview](https://sovaria.in/docs/auth-overview)); **team members** belong to your organization and use the console ([Organizations, members and roles](https://sovaria.in/docs/organizations)). An app role such as `ADMIN` that you give an app user never grants console or management access.

## The order rules are checked in

1. **Organization limits** — Limits Sovaria staff set on an organization (for example a suspended service) are checked first and can't be overridden by your rules.
2. **DENY rules** — Every active `DENY` rule is checked. If any matches, the request is refused with `403` and the reason `POLICY_DENY`. A `DENY` rule that reads a field the request doesn't carry also refuses (`DENY_CONTEXT_MISSING`) — a deny is never skipped because of missing information. Saving a rule that names a `ctx.actor.*` or `ctx.resource.*` field that never exists (`ctx.actor.id` for `ctx.actor.user_id`) answers with `warnings` naming the field meant.
3. **ALLOW rules** — If no `DENY` matched, the first `ALLOW` rule that matches lets the request through (`POLICY_ALLOW`). An `ALLOW` rule that reads a missing field simply doesn't match.
4. **Nothing decided** — If no rule decided, the platform's built-in defaults are checked, and otherwise the request is refused (`NO_POLICY_DEFINED`). For tables with row-level security **enabled and forced**, Postgres decides instead — see below.
5. **Rows** — For tables, an allowed request still runs inside Postgres row-level security, so the caller sees and changes only the rows your Postgres policies let them.

Rules live at two levels: rules created in a project apply to that project (and its environments, which can override them — see [Environments, staging and deploying to production](https://sovaria.in/docs/environments-and-staging)); a new organization also gets one baseline rule, `ALLOW IF True`, so its **team members** can work straight away. That baseline never admits app users or the anon key: they always need a rule of yours.

## Tables: access policies and row-level security together

| Access policies say | Row-level security | Result |
|---|---|---|
| DENY matches | any | `403` — the query never runs. |
| ALLOW matches | enabled | The query runs; Postgres returns or changes only the rows its policies allow. |
| ALLOW matches | off | The query runs on every row the statement names. Fine for public catalogues; dangerous for user data. |
| nothing decided | enabled **and forced** | Postgres decides alone (the Supabase model). |
| nothing decided | off, or enabled but not forced | `403 NO_POLICY_DEFINED`. |

Which database role your app's queries run as is described in [Row-level security and access](https://sovaria.in/docs/row-level-security) — it is why **force** matters today.

## Documents, storage, realtime and functions

| Service | Actions in rules (`ctx.action`) | Resource (`ctx.resource.name`) | More in |
|---|---|---|---|
| Tables | `SELECT`, `INSERT`, `UPDATE`, `DELETE` (or `sql:select`, `sql:insert`… if your rule uses that spelling). An upsert also needs `UPDATE`, and returning rows (`RETURNING`, `Prefer: return=representation`) or reading another table in a write needs `SELECT`. | The table | [Row-level security and access](https://sovaria.in/docs/row-level-security) |
| Documents | `doc:read` (gets, queries, listeners), `doc:write`, `doc:delete` | The collection (the full path, `users/u1/orders`, for nested ones) | [Collections in the console: tree, documents, fields and rules](https://sovaria.in/docs/collections-in-the-console) |
| Storage | `READ`, `LIST`, `WRITE`, `DELETE` | The bucket; the object path is `ctx.storage.path` | [Storage](https://sovaria.in/docs/storage-overview) |
| Realtime | `SUBSCRIBE`, `PUBLISH` | The channel (`ctx.realtime.in_own_channel` is true for the caller's `user:<user id>…` channels) | [Realtime](https://sovaria.in/docs/realtime-overview) |
| Functions | A function's own caller setting: public, signed-in or service key only | The function | [Functions](https://sovaria.in/docs/functions-overview) |

Managing a bucket (creating, making it public, changing limits, deleting) is project administration, not file access: app users need the `admin` app role for it even if a rule lets them upload files.

## See why a request was allowed or refused

- **Simulation** sends real requests as each user type and shows the rule that decided each one — [Simulate access before you ship](https://sovaria.in/docs/simulation).
- **Policies › Check** (or `sov policy check --action sql:select --resource orders`) evaluates your rules for one action without running anything.
- **Users › Test session** opens a session as a real user so you can run queries as them.
- A refused request's response says why; the request log shows the same reason with a trace id — [Logs and log drains](https://sovaria.in/docs/logs-and-log-drains).
