# Bring users from another system

> Move your users and groups from the system you use today into the realm, passwords included, and switch over without anyone resetting anything.

You're moving from another directory to the realm, and you want the switch to be invisible: the same sign-in names,
the same groups, the same passwords. In this guide you load users inactive from your export, link their groups,
check the load, and turn everyone on at once on the day you switch.

**Before you start:** you hold `RealmOwner` in the realm. You have an export of your users (sign-in name, profile,
groups and, ideally, bcrypt password hashes), and a script that can send requests with a realm administrator's
session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)). If you keep attributes the shipped user
doesn't have, add them first: see [Add your own attribute to users](../add-an-attribute/).

<Aside type="note" title="One-way, once">
  The realm loads what you send and keeps nothing in step with the old system afterwards. Changes made there after the
  export have to be sent again. Ongoing synchronization is planned: see
  [What's next for lifecycle](../../lifecycle/whats-next/).
</Aside>

## Load them

<Steps>

1. For each user in the export, create them inactive, with their old password hash:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/user?id=ben" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "uid": "ben", "firstName": "Ben", "lastName": "Okafor", "email": "ben@example.com",
           "passwordHash": "$2b$12$Ez6…", "status": "inactive" }'
   ```

   A hash starting `$2a$`, `$2b$` or `$2y$` is stored as it is, so the password still works. Leave out
   `passwordHash` for a user whose old hash is in another scheme. They can't set one themselves (there's no
   self-service password reset yet), so before the switch, set a first password for each and give it to them.

   Each answers `201`. Write down the ones refused, with the reasons the realm gives (`400` lists every failed rule).

2. Create each group:

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

3. Link each member:

   ```sh
   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" }'
   ```

4. Fix the refused users in your export and run the load again. Users already created are refused as existing
   (`409`), not changed, so a second run only adds the missing users. Links are different: linking a user who's
   already a member makes a second link, so on a second run link only the members a group's list doesn't have yet.

</Steps>

## Check it worked

Count what arrived, and compare it with your export:

```sh
curl -G https://foobar.cvyuh.com/api/v0/am/objects/v1/user \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  --data-urlencode 'pageSize=1' \
  --data-urlencode 'filter={"filter":{"field":"status","op":"=","value":"inactive"}}'
```

`totalCount` is the number of users loaded. For a group, list its members on
`/objects/v1/group/finance/relationships/member`.

## Switch over

On the day, set each loaded user active:

```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 '{ "status": "active" }'
```

They sign in with the sign-in name and password they had.

## Undo it

Until the switch, the loaded users can't sign in, so nothing is lost by starting again: delete them with `DELETE` on
`/objects/v1/user/{id}`, and the groups the same way.

## Related

- [Bring users in](../../lifecycle/bring-users-in/): what's carried over, and what's planned.
- [Add and change users](../../users/add-and-change-users/): every attribute a user has.
- [Search and paging](../../users/search/): find the users a load refused or missed.
