# Delegated administration

> Make a group the administrators of a realm and every realm beneath it, or let it only look, with reach that descends and never climbs.

Delegated administration decides who may read, change or hand on a realm's configuration. You give that right to a
group, and its members administer your realm and the realms beneath it, and nothing else. It is how you hand a team
its own realm: a business unit, a partner or a customer of yours runs it, while you keep oversight from above.

Administration is its own plane, separate from the policies that protect your apps. It answers one question: **may
this user read, change or delegate this realm's configuration?**

## Before you begin

- **Privilege:** `DELEGATE` on the realm's delegation configuration, which `RealmOwner` gives. You can grant only what
  you hold yourself.
- **Where:** the API, at `https://foobar.cvyuh.com/api/v0/am/privilege/v1`.
- **First:** the group you'll grant to must exist in the realm: see
  [Add groups and members](/directory/groups/add-groups-and-members/). Your session is in `$CVYUH_SESSION`
  ([Call the API](/develop/api/#get-a-session)).

## How it works

A **privilege** bundles **permission templates**. A template names part of a realm's configuration, where `*REALM`
stands for the realm the privilege is held in, and the actions it allows: `READ`, `MODIFY` and `DELEGATE`. Every realm
holds two privileges from the start:

| Privilege | Template | Actions | Gives |
|---|---|---|---|
| `RealmOwner` | `*REALM/*` | `READ`, `MODIFY`, `DELEGATE` | Full administration, including granting it to others |
| `RealmViewer` | `*REALM/*` | `READ` | Every setting and object to read, and no change |

A privilege is held by groups, never by a user directly. To make someone an administrator, put them in a group and
grant the group the privilege. A privilege may instead be held by every signed-in user of the realm
(`allAuthenticated`).

**Reach descends.** A group holding `RealmOwner` in `foobar` administers `foobar` and `engineering` beneath it; one
holding it in `engineering` can't reach `foobar`. Nothing is copied into a realm beneath, so a grant made today
reaches realms created tomorrow.

## Set it up

Grant a privilege to one of the realm's groups, naming the privilege every time. Here the `finance` group may read
everything in `foobar` and change nothing:

```sh
curl https://foobar.cvyuh.com/api/v0/am/privilege/v1 \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "group": "finance", "privilege": "RealmViewer" }'
```

The realm answers `201`, naming the group and the privilege. To grant in a realm beneath yours, make the same call on
that realm's host name, to one of its own groups.

<Aside type="caution" title="A missing privilege grants RealmOwner">
  A grant that leaves out `privilege` grants `RealmOwner`: full administration, including the right to grant it to
  others. Always send `privilege`, and send `RealmViewer` when the group should only look.
</Aside>

List each privilege and the groups that hold it:

```sh
curl https://foobar.cvyuh.com/api/v0/am/privilege/v1 \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

```json
{ "privileges": [
    { "name": "RealmOwner", "groups": ["foobar_admins"], "allAuthenticated": false },
    { "name": "RealmViewer", "groups": ["finance"], "allAuthenticated": false } ] }
```

Take one back, naming the privilege here too: left out, it means `RealmOwner`. The realm answers `200`.

```sh
curl -X DELETE "https://foobar.cvyuh.com/api/v0/am/privilege/v1/finance?privilege=RealmViewer" \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
```

### Roles of your own

When `RealmOwner` gives too much and `RealmViewer` too little, write a privilege of your own. Templates live at
`https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPermission`, privileges at
`https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPrivilege`. Reading them needs `READ` on the realm's
delegation configuration, and writing them `MODIFY`.

A template's `resource` names what it covers. Objects you change through `/api/v0/am/objects/v1/<type>` are
`*REALM/entity/<type>/*`: the type, and every object of it. `*` matches anything, `/` included, so `*REALM/*` is
everything.

Here, a privilege that lets the `engineering` group run sign-in for `foobar` (its sign-in settings and chains) and
nothing else:

<Steps>

1. **Write a template for each type it covers.**

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPermission?id=signInSettings" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "resource": "*REALM/entity/authSettings/*", "actions": ["READ", "MODIFY"] }'

   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPermission?id=signInChains" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "resource": "*REALM/entity/authChain/*", "actions": ["READ", "MODIFY"] }'
   ```

2. **Write the privilege, and link the templates to it.**

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPrivilege?id=SignInAdmin" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "allAuthenticated": false }'

   curl https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPrivilege/SignInAdmin/relationships/permissions \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H "If-Match: *" \
     -d '{ "targetId": "signInSettings" }'
   ```

   Link `signInChains` the same way.

3. **Grant it** through `privilege/v1`, as above: `{ "group": "engineering", "privilege": "SignInAdmin" }`. Who
   holds a privilege is only ever written there.

</Steps>

## What happens when it fails

| Answer | Cause | What to do |
|---|---|---|
| `403`, "not permitted on …" | Your rights don't cover the change; the message names what was refused | Ask for a privilege that covers it, in this realm or one above |
| `403` on a grant | You'd grant something you don't hold yourself, or you're granting in a realm beside yours | Grant only what you hold, from a realm above the target |
| `404` on a grant | No such group, or no such privilege, in this realm | Create the group first; check the privilege's name |
| `404` on a take-back | The group doesn't hold that privilege | Name the privilege it holds |

An administrator whose privilege is taken back keeps their session, and is refused their next change.

## Limits and defaults

| What | Default | Can it change |
|---|---|---|
| Privileges every realm starts with | `RealmOwner`, `RealmViewer` | Add your own; the two stay |
| `privilege` left out of a grant or take-back | `RealmOwner` | No: always name it |
| Templates that reach realms beneath | Only `*REALM/*` | No |
| Who holds a privilege | Groups, or every signed-in user (`allAuthenticated`) | Never a user directly |

## Troubleshooting

- **A role of your own works here but not in a realm beneath.** Only `*REALM/*` descends. A narrower template, like
  `*REALM/entity/authSettings/*`, applies only in the realm that holds the privilege. Hold the privilege in each realm
  where it should apply.
- **A user in the group is still refused.** Rights are checked on every request, so check the user really is a
  member, with `GET https://foobar.cvyuh.com/api/v0/am/objects/v1/user/<user>?expand=memberOf`, and that they signed
  in to this realm or one above it, not to a realm beside it.
- **Everyone can read the configuration.** A privilege has `allAuthenticated` set. List the privileges, and set it to
  `false` on any that shouldn't reach every user.

## Audit record

A grant or take-back is a change to the privilege: an `UPDATED` record naming who made it and the link added or
removed. Read it as the privilege's [change history](/audit-reports/audit-trail/change-history/), such as
`GET https://foobar.cvyuh.com/api/v0/am/objects/v1/delegationPrivilege/RealmOwner/history`. Templates and privileges
of your own have their history at their own addresses the same way. A refused admin call changes nothing, and isn't on
the audit trail.

## Good to know

- **Where you stand is the realm you signed in to.** Nothing in a request moves it. You may call another realm on its
  host name; whether you may act there is decided from where you stand.
- **Beneath means beneath.** A realm named `sales-2` beside `sales` is not beneath it, and `sales`'s administrators
  don't reach it.
- **`RealmViewer` reads everything and is refused every change.**
- **No second check for sensitive changes.** A session that holds `RealmOwner` can make any change for as long as it
  lasts; no admin change asks for a fresh sign-in. Keep administrators' sessions short with the realm's
  [Sessions](/workforce-identity/authentication/sessions/) settings.

## Related

- [Realms and sub-realms](/platform/realms/sub-realms/): create the realm you hand over.
- [Get started with Platform](/platform/get-started/): a team's realm, end to end.
- [Entitlements](/workforce-identity/authorization/entitlements/): the policies that protect your apps, a separate
  plane.
