API version: 0.3.0

---

# Roles and capabilities

Access to elements is controlled by the [access model](/reference/access-model.md). Roles and capabilities are
independent of it: they control who may use instance-wide functionality, e.g. handing out roles. A capability does not
grant access to elements, with one explicit exception: [`user.manage` and `group.manage`](#reading-users-and-groups)
make users and groups readable.

## Capabilities

A capability is a named permission which endpoints check. The following capabilities exist:

| Capability         | Meaning                                                                                                                                                                                                                 |
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `role.assign`      | Assign, remove and list the roles of users.                                                                                                                                                                             |
| `user.manage`      | Read and search all users, see [reading users and groups](#reading-users-and-groups).                                                                                                                                   |
| `group.manage`     | Read and search all groups, see [reading users and groups](#reading-users-and-groups).                                                                                                                                  |
| `index.manage`     | Reserved, no endpoint checks it yet. Indices of schemas are managed with `schema.manage`.                                                                                                                               |
| `schema.manage`    | Create, change and reset schemas (`PUT`/`PATCH`/`DELETE /schema/<type>`), plan and run migrations, which create the indices of schemas, read the internal data of schema nodes and revisions, and grant access to them. |
| `embedding.create` | Run the search step [`text-embedding`](/reference/search/text-embedding.md), which sends the text of the search to an embedding provider. Vectors of embeddings which the API calculates for elements do not need it.   |
| `*`                | Wildcard, stands for every capability.                                                                                                                                                                                  |

Custom capabilities and partial wildcards like `index.*` are not supported.

## Reading users and groups

Administrators can't list or find users and groups through the graph, as nobody owns them. The capabilities
`user.manage` and `group.manage` are therefore the explicit exception to "capabilities never grant access to elements":

- With `user.manage` on the token, every `User` node can be read with [`GET /<uuid>`](/api/get-element.md) and found by
  every search step (Elasticsearch, Cypher path queries, element hydration). With `group.manage`, the same applies to
  every `Group` node.
- Only reading: changes and deletions of users and groups still need access through the graph, otherwise they answer
  with `403 Forbidden`.
- Only the nodes themselves: tokens stay visible to their user only, and the children of a user or a group, i.e. the
  data they own, are not covered.
- Internal properties like `_passwordHash` and `_roles` stay hidden, like for every other element.
- Relations follow the normal rule, they are readable once both of their nodes are. **With both capabilities, the
  memberships (`IS_IN_GROUP`) between users and groups become readable**: who belongs to which group, i.e. who shares
  data with whom, without access to the data itself. This is intended for administrators; hand out both capabilities
  together only to whom may know it.
- The capabilities only extend what the token of the current request may read, never the access of other users, e.g.
  in the [access list](/api/get-element-access.md) of an element.

## Roles

A role is a named set of capabilities, defined in the [instance configuration](/reference/configuration.md):

```yaml
ember_nexus:
  roles:
    admin:
      capabilities: ['*']
    role-manager:
      capabilities: ['role.assign']
```

By default only the role `admin` with the capability `*` exists. Defining `roles` replaces the default completely, so
a configuration without `admin` has no admin role. Role names consist of letters, digits, `_` and `-`.

## Role assignments

Roles are assigned to users. Every assignment records the role, its issuer and the time of the assignment. The issuer
is `cli` for assignments through the CLI, `api:user:<uuid>` for assignments through the API and `register` for the
[default roles of the registration](#default-roles-of-the-registration). The assignments are
stored in the internal property `_roles` of the user, which is neither readable nor writable through the element
endpoints. Like all properties they are part of backups: loading a backup restores the privileges it contains, so the
`backup:load` command warns if the anonymous user holds roles. The anonymous user never has capabilities.

Roles can be handled through the CLI ([`user:create --role`](/command/user-create.md),
[`user:role:add`](/command/user-role-add.md), [`user:role:remove`](/command/user-role-remove.md),
[`user:role:list`](/command/user-role-list.md)) and through the endpoints
[`GET`](/api/get-user-roles.md)/[`POST`](/api/post-user-role.md) `/user/<uuid>/roles` and
[`DELETE /user/<uuid>/roles/<role>`](/api/delete-user-role.md).

An actor may only assign or remove roles whose capabilities the actor holds itself. The check uses the capabilities
of the actor's user, i.e. it does not matter which capabilities the used token was restricted to.

Roles which are no longer defined in the configuration grant no capabilities. They stay assigned and can be removed.

## Default roles of the registration

Instances which allow self-registration can hand out roles to every new user, e.g. a `member` role:

```yaml
ember_nexus:
  roles:
    admin:
      capabilities: ['*']
    member:
      capabilities: []
  register:
    defaultRoles: ['member']   # default: []
```

The roles are assigned by [`POST /register`](/api/post-register.md) with the issuer `register`, once, when the account is
created. Changing the configuration later does not affect existing users. [`user:create`](/command/user-create.md) does
not assign them, it only assigns the roles given via `--role`.

Every default role must be defined in `roles`, otherwise the API refuses to start. If a default role contains `*` or
`role.assign`, `cache:warmup` and `cache:clear` print a warning, which is part of the output of the container start:
everyone who registers would get admin rights, even though tokens still have to name the capabilities explicitly.

## Token capabilities

Tokens carry a list of capabilities, which is empty by default. It can be set when the token is created, through
[`POST /token`](/api/post-token.md) or [`token:create --capability`](/command/token-create.md). The list is write once:
it can not be changed afterwards. The wildcard can not be requested, and a token can only be limited to capabilities
the user holds at the time of creation.

The effective capabilities of a request are the intersection of the token's capabilities and the **current**
capabilities of the user, evaluated on every request. Removing a role from a user therefore takes effect immediately,
for all tokens of the user. Because tokens have no capabilities by default, a leaked ordinary token of an admin can
not be used to hand out roles.

---

Home: [Ember Nexus API docs](/index.md)

Full site map: [llms.txt](/llms.txt)

Previous: [Anonymous user](/reference/access-model/anonymous-user.md)

Next: [Elements](/reference/elements.md)
