# Role

## Description

Role represents a named authorization entity that holds a `permissions` array and can be assigned to users. Roles provide a level of abstraction between permissions and users, simplifying access management by allowing administrators to manage access at the role level rather than per-user. Common examples include "Sales Representative", "Sales Manager", or "Administrator".

Roles follow the principle of least privilege, bundling only the permissions necessary for a specific job function.

## Domain Model Definitions

### Model type

Stateful

#### State Transitions

```mermaid
stateDiagram-v2
    [*] --> Active: createRole
    Active --> Inactive: deactivateRole
    Inactive --> Active: reactivateRole
```

| Operation | From | To | Command |
|-----------|------|----|---------|
| deactivate | ACTIVE | INACTIVE | [deactivateRole](../commands/DeactivateRole.md) |
| reactivate | INACTIVE | ACTIVE | [reactivateRole](../commands/ReactivateRole.md) |

### Command Definitions

- [createRole](../commands/CreateRole.md)
- [updateRole](../commands/UpdateRole.md)
- [deactivateRole](../commands/DeactivateRole.md)
- [reactivateRole](../commands/ReactivateRole.md)
- [grantPermissionToRole](../commands/GrantPermissionToRole.md)
- [revokePermissionFromRole](../commands/RevokePermissionFromRole.md)

### Query Definitions

- [listRoles](../queries/ListRoles.md)
- [listUserRolesByRole](../queries/ListUserRolesByRole.md)

### Models

- Role

### Invariants

- Role name must be unique across all roles
- Role name is required and cannot be empty
- Status must be one of ACTIVE or INACTIVE
- Only ACTIVE roles can be assigned to users
- Only ACTIVE roles can receive permission grants
- Only ACTIVE roles can be updated (name/description)
- Deactivation requires no active UserRole assignments (ROLE_HAS_ACTIVE_ASSIGNMENTS guard)

### Relationships

- **Has Many UserRoles**: Role is assigned to users through UserRole join records
