# Joiners, movers and leavers

> What to change in the directory when someone starts, changes role or leaves, and what the realm does for you at each step.

When someone joins, create them and put them in their groups. When they move, change their attributes and their
groups. When they leave, set them inactive. Each is one or two calls, and the realm turns them into who may sign in,
what their tokens carry and what they can reach, everywhere at once.

There are no approval steps or scheduled tasks yet (see [Coming next](#coming-next)), so a change takes effect when
you, or a script of yours, writes it.

## Before you begin

- **Privilege:** `RealmOwner`, or a privilege of your own over users and groups; ending sessions needs `MODIFY` on
  the realm's sessions, which `RealmOwner` has. 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/)).
- **Attributes you move on,** such as `department`, added to your user type first.

## How it works

### Joiners

Create the user with their profile and put them in their groups. Groups carry everything else: the entitlements a
group holds, the policies that name it, the secrets in its vault and, for administrators, the realms they may
administer. A joiner is ready as soon as their links are made.

For someone who should have access only for a while, such as a contractor or an intern, set `accountExpiry` when you
create them. They can sign in until that moment and not after, with nothing left for you to remember.

To prepare accounts ahead of a start date, create them with `"status": "inactive"` and set them `active` on the day.

### Movers

When someone changes team, change their attributes (`department`, a title, a manager, each an attribute you add) and move their links: remove
them from the groups they're leaving and add them to the ones they're joining. What the old groups gave them stops on
their next request; what the new ones give starts on it.

Access someone needs for a set time, such as cover for a colleague, is an entitlement granted with `validFrom` and
`validTo`. It starts and ends by itself, and nothing has to be removed afterwards. See
[Entitlements](../../../workforce-identity/authorization/entitlements/).

With membership worked out from rules, a change of department is the whole move: the user leaves the sets whose
rules they no longer match and joins the ones they now do, in the same write. See
[Rule-based membership](../../groups/rule-based-membership/).

### Leavers

Set the user's `status` to `inactive`. From that moment they can't sign in, no app can get a new token for them,
their refresh tokens stop working, and their tokens read as inactive to any app that asks the realm. Then end the
sessions they already hold, hand their agents to a new owner, and take back what others shared with them.

An access token issued before you set them inactive stays valid, at an app that checks it itself, until it expires:
five minutes by default. Apps that introspect see it as inactive at once. See
[Turn access off and on](../../users/turn-access-off-and-on/#what-happens-to-tokens-already-issued).

Keep the inactive record for as long as you need it, for an audit or in case they return. Deleting them later removes
the user and every link to them, and their personal vault with it, and leaves the agents they owned without an owner:
hand those over first.

A leaver can also be stopped from outside: your identity provider can tell the realm a user was disabled, and
their access here ends at once. See [Receive signals](../../../workforce-identity/signals/receive-signals/).

## Set it up

Every call is on `https://foobar.cvyuh.com/api/v0/am`:

| Moment | Change | Call |
|---|---|---|
| Join | Create the user | `POST /objects/v1/user?id={id}` |
| Join | Add to a group | `POST /objects/v1/group/{group}/relationships/member` with `{ "targetId": "{id}" }` |
| Join | Access until a date | `PATCH /objects/v1/user/{id}` with `{ "accountExpiry": "<RFC 3339 time>" }` |
| Move | Change attributes | `PATCH /objects/v1/user/{id}` with the fields that changed |
| Move | Leave a group | `DELETE /objects/v1/group/{group}/relationships/member/{id}` |
| Leave | Stop sign-in and tokens | `PATCH /objects/v1/user/{id}` with `{ "status": "inactive" }` |
| Leave | End their sessions | `POST /sessions/v1/end` with a filter on `user` |
| Leave | Hand over their agents | `DELETE` then `POST` on `/objects/v1/agent/{agent}/relationships/businessOwner` |
| Leave | Leave every group | `PATCH /objects/v1/user/{id}` with `{ "memberOf": [] }` |
| Leave | Delete the account | `DELETE /objects/v1/user/{id}` with `If-Match` |

A mover, Ben, from `engineering` to `finance`:

```sh
curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/user/ben \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "department": "Finance" }'

curl -X DELETE https://foobar.cvyuh.com/api/v0/am/objects/v1/group/engineering/relationships/member/ben \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "If-Match: *"

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

`department` is an attribute you add to your user type first: see
[Attributes and validation](../../attributes/attributes-and-validation/). The leaver's steps, in order and with what to
check, are in [Offboard a leaver](../../guides/offboard-a-leaver/).

## Limits and defaults

| What | Default or limit |
|---|---|
| When a change takes effect | At once, for sign-in and new tokens; on their next request, for what a group gives |
| An access token issued before a leaver is set inactive | Valid until it expires at apps that check it themselves: five minutes by default |
| Sessions ended by one `end` call | Up to 10,000; call again if `ended` comes back at that number |
| Scheduled or approved steps | None yet: each step runs when you make it |

## Audit record

Each step is on the trail: the user's `CREATED`, `PATCHED` and `DELETED`; each group's `UPDATED` as members come and
go; a `LOGOUT` for every session you end, naming you as the one who ended it; and each refused sign-in afterwards as
`LOGIN_FAILED`. Read them in the user's [change history](../../attributes/identity-model/#every-change-is-recorded)
and on the [audit trail](../../../audit-reports/audit-trail/what-is-recorded/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A leaver still reaches an app | A session or an access token issued before they were set inactive | End their sessions; the access token ends within its lifetime |
| A mover kept their old group's access | The old membership link is still there | Remove it; check the user with `?expand=memberOf/*` |
| A deleted leaver's agents have no owner | Deleting removed the owner links | Name a new owner for each: [Agent records](../../agents/agent-records/#hand-an-agent-to-a-new-owner) |

## Good to know

- **Inactive is reversible; delete isn't.** Set a returning user `active` and everything they had is still there. A
  deleted user is gone, with their links and their personal secrets.
- **Changing status doesn't end sessions.** Sign-in and token issue stop at once, but a session already open stays
  until it ends or you end it.
- **Expiry is checked at the moment of use.** Nothing runs at the expiry time; each sign-in and each token request
  compares it with the clock.
- **A leaver's agents need a decision.** An agent acting for an inactive user gets no new token for them. An agent
  they own has an identity of its own, which goes on until you stop it or name a new owner. Hand it over before you
  delete the user: deleting removes the owner link and leaves the agent with none. See
  [Agent records](../../agents/agent-records/#hand-an-agent-to-a-new-owner).

## Coming next

Workflows, lifecycle hooks, access reviews, invitations and personal data export are planned. See
[What's next for lifecycle](../whats-next/).

## Related

- [Offboard a leaver](../../guides/offboard-a-leaver/): the leaver's steps, start to finish.
- [Add users and groups](../../guides/add-users-and-groups/): a joiner's first steps.
- [Turn access off and on](../../users/turn-access-off-and-on/): what `status` and `accountExpiry` stop.
- [Offboard someone](../../../secrets-store/guides/offboard-someone/): what happens to their secrets and the grants
  made to them.
