# Add groups and members

> Create groups, add and remove their members from either side, and list who's in them.

Create a group, put users in it, and everything the group holds reaches them. Membership is one link, readable and
changeable from the group or from the user, so a user's groups and a group's members never disagree.

## Before you begin

- **Privilege:** `RealmViewer` to list groups and members; `RealmOwner`, or a privilege of your own over groups, to
  create groups and change members. 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/)).
- **The user exists** before you add them to a group.

## How it works

Every group is an object of the `group` type. Its **id** is its name, such as `engineering`, and it carries a
`displayName`, a `description`, and its members.

Membership is a **link** between a group and a user, stored once and readable from both ends: the group lists the
user in `member`, and the user lists the group in `memberOf`. Add or remove a link from either side and both ends
change together, in one step. Membership is a list of links, not a stored copy of names, so a user renamed or
deleted never leaves a stale entry behind.

A group can also hold entitlements of its own in `grants`, each for a window you set, and `effectiveGrants` shows
the ones in effect right now. See [Entitlements](../../../workforce-identity/authorization/entitlements/).

## Set it up

1. Create the group:

   ```sh
   curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/group?id=engineering" \
     -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
     -H "Content-Type: application/json" \
     -d '{ "displayName": "Engineering", "description": "Everyone who builds the product" }'
   ```

   The realm answers `201` with the group and its revision, `rev`, also sent as the `ETag` header.

2. Add a member, naming the user by id:

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

   The realm answers `201` with `{ "_id": "engineering", "property": "member", "targetId": "maya", "rev": <rev> }`.
   The same link made from the user's side, on `/objects/v1/user/maya/relationships/memberOf` with
   `{ "targetId": "engineering" }`, has the same effect.

3. List the members:

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

   The list pages like any other (see [Search and paging](../../users/search/)). To see one user's groups, read the
   user with `?expand=memberOf/*`.

4. Remove a member:

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

   The user loses what the group gave them from their next request.

## Limits and defaults

| What | Default or limit |
|---|---|
| Members of a group | No fixed cap |
| Members listed per page | 20 unless you set `pageSize`, at most 100 |
| Groups or members read inline with `expand` | At most 10,000; past that the read is refused `413`, and you page the list instead |
| The same user linked twice | Not refused: a second link is made. Check before you link |
| Nesting | None: a group's members are users |

## When a change fails

| Answer | Code | What it means | What to do |
|---|---|---|---|
| `400` | `INVALID_REQUEST` | The body has no `targetId`, or the link is one a rule made (see [Rule-based membership](../rule-based-membership/)) | Send `{ "targetId": "{id}" }`; change the rule instead |
| `403` | `FORBIDDEN` | You may not change this group, or read the user you named | Ask for the privilege, or check the user id |
| `404` | `NOT_FOUND` | No such group, or no such link to remove | Check the ids |
| `412` | `REVISION_CONFLICT` | The group changed since the revision you sent, or doesn't exist under `If-Match: *` | Read the group again |

## Audit record

Adding or removing a member is recorded as `UPDATED` on the group, with the link in `edgeChanges` (`added` or
`removed`) and who made it. It's in the group's [change history](../../attributes/identity-model/#every-change-is-recorded):
see [See who changed a group](../../../audit-reports/guides/see-who-changed-a-group/).

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A user is listed twice in a group | The same link was made twice, for example by a load run again | Remove the user (`DELETE` on `…/relationships/member/{id}`), check the list, and add them once |
| A member still has what the group gave | Their app holds a token issued before the change | The next token they get leaves it out; access tokens are short-lived |
| `403` adding a user who exists | You may not read that user | Ask an administrator whose privileges cover users |

## Good to know

- **`If-Match` on a link guards the group.** `If-Match: "<rev>"` refuses the change `412` if the group was changed
  since you read it; `If-Match: *` asks only that the group exists.
- **Linking reads the user.** You can add only a user you're allowed to read; for any other, the realm refuses
  before it says whether that user exists.
- **Leaving every group at once.** `PATCH` the user with `{ "memberOf": [] }`, and each of their groups stops listing
  them in the same write.
- **Deleting a group removes every link to it.** Its members lose what it gave them, and the secrets in its vault go
  with it (see [Deleting an owner](../../../secrets-store/vaults/owner-deletion/)). The users themselves are
  untouched.
- **Groups don't nest.** A group's members are users; a group can't be a member of another group.
- **`subscribable` has no effect yet.** The shipped group carries it, for groups users will join themselves, but
  nothing reads it today. See [What's next for groups](../whats-next/#groups-users-join-themselves).

## Related

- [Get started with Directory & Lifecycle](../../get-started/): a user and a group, end to end.
- [Rule-based membership](../rule-based-membership/): membership worked out from a rule.
- [Share secrets with a team](../../../secrets-store/guides/team-secrets/): a group's own vault.
