# Add and change users

> Create the users of your realm, read them back, change only the attributes that changed, and delete them when you're done.

Every user in your realm is written through one API: create them with a profile and a first password, read them
back, change any attribute without resending the rest, and delete them. Every write is checked against the user
type's rules, and a revision on every record stops two administrators from overwriting each other.

## Before you begin

- **Privilege:** `RealmViewer` to read users; `RealmOwner`, or a privilege of your own over users, to create, change
  and delete them. See [Who may do what](../../attributes/identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).
- **Your own attributes first.** To keep an attribute the shipped user doesn't have, such as `employeeId`, add it to
  your user type before you write it, so it's checked and searchable: see [Attributes and validation](../../attributes/attributes-and-validation/).

## How it works

Every user is an object of the `user` type. Its **id** is the name you give it when you create it, such as `maya`,
and the realm uses it in every address for that user. Its **`uid`** is the name the user signs in with. Use the same
value for both, so the address and the sign-in name never disagree.

| Attribute | What it holds |
|---|---|
| `uid` | The sign-in name. Required, and unique in the realm |
| `displayName`, `firstName`, `lastName` | How the user is named |
| `email`, `telephoneNumber` | How to reach them. `email` can be looked up directly, like `uid` |
| `locale`, `timeZone`, `language` | Their preferences, for apps to read |
| `status` | `active` or `inactive`. Filled `active` when you leave it out |
| `accountExpiry` | When their access ends, as an RFC 3339 time |
| `passwordHash` | Their password. You write it; no read ever returns it |
| `memberOf` | Their groups. See [Add groups and members](../../groups/add-groups-and-members/) |

Other parts of the realm add attributes of their own to the same user: the entitlements they hold in `grants` (see
[Entitlements](../../../workforce-identity/authorization/entitlements/)), their `riskClass`,
their second factors in `devices`, the agents they own in `ownedAgents`, and per-user session limits
(`maxSessionMinutes`, `maxIdleMinutes`, `sessionQuota`). You can add your own as well: see
[Attributes and validation](../../attributes/attributes-and-validation/).

**Passwords are hashed before they are stored.** Write the password as given in `passwordHash`; the realm stores a
bcrypt hash of it. A value that is already a bcrypt hash is stored as it is, which is how users brought from another
system keep their passwords (see [Bring users in](../../lifecycle/bring-users-in/)).

**Every write is checked.** A write that breaks a rule of the type, such as a missing or duplicate `uid`, is refused
with `400` and a list of every failure at once, so you fix them in one pass.

## Set it up

1. Create the user, naming it with `id`:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/user?id=maya" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "uid": "maya", "firstName": "Maya", "lastName": "Chen", "displayName": "Maya Chen",
           "email": "maya@example.com", "passwordHash": "<her first password>" }'
   ```

   The realm answers `201` with the user: its `objectId`, its `attributes` (without the password), and its
   revision, `rev`, also sent as the `ETag` header.

2. Read it back. Add `?expand=memberOf/*` to see the user's groups too:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya?expand=memberOf/*" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION"
   ```

3. Change only what changed, naming the revision you read:

   ```sh
   curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -H 'If-Match: "<rev>"' \
     -d '{ "telephoneNumber": "+44 20 7946 0000", "locale": "en-GB" }'
   ```

   A JSON object is a merge patch (RFC 7396): the fields you send are set, a field sent as `null` is removed, and
   the rest are left alone. A JSON array is a list of JSON Patch (RFC 6902) operations.

4. To delete the user, send `DELETE` to the same address with `If-Match`:

   ```sh
   curl -X DELETE https://foobar.cvyuh.com/api/v0/am/objects/v1/user/maya \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H 'If-Match: "<rev>"'
   ```

   The realm answers `{ "status": "deleted" }`.

## Limits and defaults

| What | Default or limit |
|---|---|
| `uid` | Required, and unique in the realm ignoring case: `Maya` and `maya` are the same sign-in name |
| `email` | Unique in the realm ignoring case, when set |
| `status` | `active` when left out |
| `id` | The `?id=` you give, or one the realm makes; can't change |
| A request body | About 2 MB; a larger one is refused `400` |
| Password rules | The realm's password policy, checked on every write of `passwordHash`: see [Password rules](../../../workforce-identity/authentication/password-rules/) |

## When a write fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `POLICY_VIOLATION` | A rule of the type failed, such as a duplicate `uid` or a password too weak; `detail` lists every failure | Fix each one named and send again |
| `400` | `SCHEMA_VALIDATION_FAILED` | A value has the wrong kind or isn't one of its allowed values, such as a `status` other than `active` or `inactive` | Check the type with `GET /types/v1/user` |
| `403` | `FORBIDDEN` | Your privileges don't cover this user or this change | Ask an administrator holding `RealmOwner` |
| `404` | `NOT_FOUND` | No user with that id | Check the id; ids are the `objectId`, not `uid` |
| `409` | `ALREADY_EXISTS` | The id you gave is taken | Pick another id, or `PATCH` the user that's there |
| `412` | `REVISION_CONFLICT` | The user changed since you read it | Read it again, and reapply your change |

## Audit record

Every write is on the audit trail and in the user's [change history](../../attributes/identity-model/#every-change-is-recorded):
`CREATED` for a create, `PATCHED` for a `PATCH`, `UPDATED` for a `PUT` or a change to their links, `DELETED` for a
delete, each with who made it and the values before and after. The password and every other value no read returns
are left out.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A new user can't sign in | Their `status` is `inactive`, their `accountExpiry` has passed, or they have no `passwordHash` | Read the user and check all three; see [Turn access off and on](../turn-access-off-and-on/) |
| A change you made is gone | Someone else wrote after you without `If-Match`, or a `PUT` replaced the attributes | Send `If-Match` with the revision you read; use `PATCH` for partial changes |
| An attribute you wrote can't be searched (`400`, unknown filter field) | It isn't on your user type: an attribute the type doesn't define is stored as sent, but it isn't checked or searchable | Add it to the type ([Attributes and validation](../../attributes/attributes-and-validation/)), and check its spelling |

## Good to know

- **`If-Match` stops lost updates.** With it, a write made after someone else's change is refused `412`, and you
  read the user again before retrying. `If-Match: *` writes whatever the revision. Without the header, a `PATCH`
  writes over any change made since you read.
- **A replace is whole.** `PUT` on the user's address replaces every attribute you can see with the body. Use
  `PATCH` unless you mean to clear what you leave out. Values no read returns, such as the password, are kept.
- **Creating with an id that's taken is refused.** Leave out `?id=` and the realm makes an id for you, but you'll
  want ids you can read.
- **An attribute the type doesn't define is stored as sent.** It isn't refused, so a misspelled attribute name is
  kept beside the right one, unchecked and not searchable. Define every attribute you write on the type.
- **The id can't change.** To change a sign-in name, change `uid`; the id in the address stays as it was.
- **Deleting is final.** It removes the user and every link to them: their group memberships, the grants made to
  them, and their personal vault (see [Deleting an owner](../../../secrets-store/vaults/owner-deletion/)). To take
  access away and keep the record, set them inactive instead: see [Turn access off and on](../turn-access-off-and-on/).
- **Deleting a user leaves their agents without an owner.** Each agent's `businessOwner` link goes with the user, and
  nothing requires an agent to have one. Before you delete a user, list their agents with `?expand=ownedAgents/*` and
  hand each to a new owner: see [Agent records](../../agents/agent-records/#hand-an-agent-to-a-new-owner).

## Related

- [Turn access off and on](../turn-access-off-and-on/): `status` and `accountExpiry`.
- [Search and paging](../search/): find users by any attribute.
- [Add users and groups](../../guides/add-users-and-groups/): your first users, start to finish.
- [The identity model](../../attributes/identity-model/): the object shape every type shares.
