# Your own user type

> Sign users in against a type you designed in place of the shipped user, and the realm gives it every field sign-in, tokens and groups need.

If your organization already has a model of a person, with its own name and attributes, you don't have to fit it
into the shipped `user`. Define your own type, point the realm at it, and sign-in, tokens, groups, secrets and agents
all follow it.

## Before you begin

- **Privilege:** `RealmOwner`. The binding and the type it names are both realm configuration. See
  [Who may do what](../identity-model/#who-may-do-what).
- **Where:** the API, with your session in `$CVYUH_SESSION` (see [Call the API](../../../develop/api/)).
- **Do it before you add users,** in a new realm: users already stored don't move.

## How it works

The realm signs users in against whatever type its **identity binding** names. By default that's `user`, found by
`uid`, with its password in `passwordHash` and its groups in `memberOf`. Point the binding at a type you designed,
such as `person`, and every part of the realm reads users from it.

When you bind a type, the realm first gives it every attribute and link the shipped type carries, marked as carried,
so nothing sign-in or tokens read is missing. An attribute you already gave it under one of those names is kept if
its definition matches the shipped one, and the binding is refused if it doesn't.

| Binding field | What it names | Default |
|---|---|---|
| `userType` | The type users are | `user` |
| `groupType` | The type groups are | `group` |
| `agentType` | The type AI agents are | `agent` |
| `namingAttr` | The attribute users sign in with | `uid` |
| `passwordAttr` | The attribute holding the password | `passwordHash` |
| `membershipAttr` | The link from a user to their groups | `memberOf` |

## Set it up

1. Define your type, as [Custom types](../custom-types/) describes, for example `person`.

2. Point the binding at it:

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

3. Read the type back with `GET /types/v1/person`: it now carries every shipped attribute beside yours.

## When a binding fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `POLICY_VIOLATION` | An attribute on your type has a carried name but a different definition | Rename yours, or make its definition match the shipped one |
| `400` | `SCHEMA_VALIDATION_FAILED` | The binding body doesn't read, such as an unknown field | Send only the fields in the table above |
| `412` | `REVISION_CONFLICT` | Your type changed while the carried attributes were being added | Send the binding again |

## Audit record

The change to the binding is recorded as `PATCHED` on `identityBinding`, and the carried attributes added to your type
as `UPDATED` on the type, each with before and after.

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| Users can't sign in after the binding changed | They're stored as `user`, which is no longer the bound type | Point the binding back, or create them in the new type |
| A save of your type is refused `400`, naming an attribute | It's carried, and only its `default` and `description` can change | Leave it as it is |

## Good to know

- **Bind before you add users.** Users already stored as `user` don't move to the new type, and once the binding
  points elsewhere they no longer sign in.
- **A carried attribute stays.** On a bound type, the carried attributes can change only their `default` and
  `description`, as on the shipped `user`.

## Related

- [Custom types](../custom-types/): define the type first.
- [The identity model](../identity-model/): how the shipped user is built.
- [Attributes and validation](../attributes-and-validation/): carried attributes and their rules.
