Permission Association
RoleAssociation grants a RoleModel to a User inside a specific context — an organization or facility. A role is a named collection of permissions; the association scopes that collection to one user in one context. A user can hold many roles across different contexts, one association per grant.
At Care 3.1 (develop ddfd840), no code reads or writes RoleAssociation outside its initial migration. Care grants roles to users through three other models: OrganizationUser, FacilityOrganizationUser (Organization), and PatientUser. The authorization layer and the permission mixins read those models, not this one.
The Django model (care/security/models/permission_association.py) is the storage layer: the user/role foreign keys, the generic context/context_id pair, and expiry. It has no resource spec of its own and no viewset. Two sets of Pydantic resource specs are related to it: those that govern the role it points at (care/emr/resources/role/spec.py), and the permission-resolution mixins (care/emr/resources/permissions.py) that compute a user's effective permissions from the membership models.
Source:
- Model:
care/security/models/permission_association.py - Role spec (the granted role):
care/emr/resources/role/spec.py - Permission-resolution mixins:
care/emr/resources/permissions.py - Role definitions /
RoleContext:care/security/roles/role.py - Permission contexts:
care/security/permissions/constants.py
Models
| Model | Purpose |
|---|---|
RoleAssociation | Grants a RoleModel to a User within a named context, with optional expiry |
RoleAssociation extends BaseModel — the lightweight Care base providing external_id (UUID), created_date, modified_date, and soft-delete via deleted (the overridden delete() sets deleted=True instead of removing the row). See Base model.
It does not extend EMRBaseModel, so there are no created_by / updated_by audit fields, no history/meta JSON, and no external_id-routed EMR API. There is no RoleAssociationCreateSpec/...ReadSpec, and no viewset, spec, signal, or management command creates or removes these rows.
RoleAssociation fields
Subject and role
| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
user | FK → User | yes | — | Subject receiving the role. on_delete=CASCADE, null=False, blank=False — deleting the user removes the association |
role | FK → RoleModel | yes | — | Role (set of permissions) being granted. on_delete=CASCADE, null=False, blank=False. Read/write through the role specs — see Role |
Context
The context identifies where the role applies. Storing it as a type/id pair rather than a typed foreign key lets one table scope roles to any kind of context — organization, facility, and so on. The pair has no DB-level referential integrity, so nothing stops context_id from pointing at a row that no longer exists.
| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
context | CharField(1024) | yes | — | Context type — the name of the entity the role is scoped to. Free-form string at the model layer, not bound to an enum |
context_id | BigIntegerField | yes | — | Integer primary key of the context entity, not a UUID/external_id |
Lifecycle
| Field | Type | Required | Default | Notes |
|---|---|---|---|---|
expiry | DateTimeField | no | null | null=True, blank=True. A single timestamp after which the grant is meant to lapse — not a PeriodSpec/start–end range. Stored only; no code reads it |
Related models
The granted role resolves to permissions through the models below. None belong to RoleAssociation, but every effective-permission lookup walks through them.
RoleModel
The granted role (Role). A named, flat set of permissions; is_system roles are platform-seeded and cannot be edited through the API.
name → CharField(1024) # unique among non-deleted rows
description → TextField (default "")
is_system → BooleanField (default False)
is_archived → BooleanField (default False)
temp_deleted→ BooleanField (default False)
contexts → ArrayField[CharField(24)] (default []) # bound to RoleContext in specs
RolePermission
Join table linking one RoleModel to one PermissionModel. A role accumulates permissions across many RolePermission rows; lookups exclude temp_deleted=True.
role → FK RoleModel (CASCADE, required)
permission → FK PermissionModel (CASCADE, required)
temp_deleted → BooleanField (default False)
PermissionModel
The atomic permission a role grants (Permission).
slug → CharField(1024) unique, indexed
name → CharField(1024)
description → TextField (default "")
context → CharField(1024) # one of the PermissionContext values
temp_deleted → BooleanField (default False)
Enum / value tables
RoleAssociation stores plain strings and integers, but these enums constrain the role it points at and the permissions that role resolves to.
RoleContext values
Each element of the granted role's contexts array is validated against this enum (care/security/roles/role.py). These are the organizational boundary types a role can apply to — distinct from PermissionContext. No code ties the free-form RoleAssociation.context column to these values.
| Value | Meaning |
|---|---|
FACILITY | Role applies within a facility |
GOVT_ORG | Role applies within a government organization |
ROLE_ORG | Role applies within a role (user-group) organization |
PermissionContext values
PermissionModel.context / Permission.context takes one of these values (care/security/permissions/constants.py). The patient and encounter permission-resolution mixins (below) filter a user's grants by these contexts.
| Value |
|---|
GENERIC |
FACILITY |
PATIENT |
QUESTIONNAIRE |
ORGANIZATION |
FACILITY_ORGANIZATION |
ENCOUNTER |
PermissionEnum (role write field)
When a role is written through RoleCreateSpec, its permissions field is typed against PermissionController.get_enum() — a str enum built at runtime from the member name (the slug) of every registered permission across all permission handlers (for example can_create_patient, can_write_patient, can_list_patients, can_view_clinical_data). The set is deployment-dependent: internal handlers plus any plugin-registered ones, not a fixed list.
Built-in (is_system) roles
Declared in RoleController.internal_roles (care/security/roles/role.py) and written by sync_permissions_roles. Membership rows often bind a user to one of these. They cannot be created, updated, or deleted through the API. The contexts are the values stored on each seeded row.
| Role | Contexts |
|---|---|
Doctor | FACILITY, GOVT_ORG |
Nurse | FACILITY, GOVT_ORG |
Staff | FACILITY, GOVT_ORG |
Volunteer | FACILITY, GOVT_ORG |
Pharmacist | FACILITY |
Administrator | FACILITY, GOVT_ORG |
Facility Admin | FACILITY |
Admin | ROLE_ORG |
Manager | ROLE_ORG |
Member | ROLE_ORG |
The code declares two roles named Admin, but the sync writes them to one row with contexts = ["ROLE_ORG"]. Admin is not a facility role. See Role.
Resource specs (API schema)
RoleAssociation has no dedicated ...CreateSpec/...UpdateSpec/...ListSpec/...RetrieveSpec — it is not exposed as an EMR resource. Two related Pydantic surfaces exist: the specs that define the role an association grants, and the mixins that read the membership models to expose a user's effective permissions on another resource. All extend EMRResource (care/emr/resources/base.py), which provides serialize (DB → Pydantic via perform_extra_serialization) and de_serialize (Pydantic → DB via perform_extra_deserialization).
Specs for the granted role
| Spec class | Role | Fields |
|---|---|---|
RoleBaseSpec | shared base (__exclude__ = ["permissions"]) | id, name, description, is_system, is_archived, contexts |
RoleCreateSpec | write · create & update | base fields + permissions: list[PermissionEnum] (≥ 1, de-duplicated) |
RoleReadSpec | read · detail/list | base fields + permissions: list[PermissionSpec] |
RoleReadMinimalSpec | read · minimal/embedded | base fields only |
PermissionSpec | nested (read) | name, description, slug (SlugType), context |
RoleBaseSpec.contexts is list[RoleContext] — bound to the RoleContext enum at the API layer even though the model column is an open string array. PermissionSpec.slug is a SlugType: str, length 5–50, pattern ^[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]$. Core role validation (RoleCreateSpec.validate_role, mode="after") requires a non-blank name on create and a case-insensitively unique name (name__iexact), rejects is_system=True, rejects updating a system role, and requires ≥ 1 permission. See Role for the full spec breakdown.
Permission-resolution mixins (care/emr/resources/permissions.py)
Three retrieve specs inherit these mixins: PatientRetrieveSpec, EncounterRetrieveSpec, and FacilityRetrieveSpec. When serialize runs with an authenticated user, perform_extra_user_serialization finds the roles the user holds on that object through the membership models. It resolves the roles to permissions and writes them into mapping["permissions"]. None of the mixins read RoleAssociation.
| Mixin | Resolves (from the user's memberships) | Context filter | Extra fields |
|---|---|---|---|
PermissionsMixin | base — adds permissions: list[str] | — | — |
PatientPermissionsMixin | FacilityOrganizationUser roles in the facility orgs of the patient's encounters outside COMPLETED_CHOICES and of their current locations, OrganizationUser roles in the patient's organization_cache, and PatientUser roles | permission__context in ("PATIENT", "FACILITY") | — |
EncounterPermissionsMixin | FacilityOrganizationUser roles in the encounter's facility orgs and its current location's facility orgs | permission__context in ("ENCOUNTER", "PATIENT") | — |
FacilityPermissionsMixin | FacilityOrganizationUser roles in the facility root org and in its team / dept orgs | none (root) / child set excludes can_update_facility | root_org_permissions, child_org_permissions |
Permission slugs are resolved via RolePermission.objects.filter(role_id__in=roles, …).values_list("permission__slug", flat=True). The default manager excludes soft-deleted (deleted=True) rows; the query does not filter on temp_deleted.
Methods & save behaviour
RoleAssociation defines no model methods, validators, or save()/delete() overrides of its own. It inherits soft-delete from BaseModel: calling delete() sets deleted=True and persists with save(update_fields=["deleted"]), and the default manager filters out soft-deleted rows.
A source TODO flags a planned index on user, context, and context_id that does not yet exist. user and role have the default foreign-key indexes; context and context_id have no index.
expiry is a stored timestamp only. Neither the model nor any other code reads it.
RoleAssociation has no perform_extra_serialization / perform_extra_deserialization, since it has no spec. The serialization hooks that matter live on the role specs and the permission mixins above.
API integration notes
RoleAssociationis not exposed through any API: no FHIR resource, Pydantic spec, viewset, or route. No code path in Care creates, reads, or deletes its rows.- To grant a role to a user, use the membership endpoints instead:
organization/<id>/users/—OrganizationUserfacility/<id>/organizations/<id>/users/—FacilityOrganizationUserpatient/<id>/add_user/andpatient/<id>/delete_user/—PatientUser
- The
context/context_idpair is a generic (non-FK) reference.contextis a free-form string, andcontext_idis meant to hold an integer PK, not anexternal_idUUID. Nothing at the database level enforces the pair. expiryis not read by any code; a row does not lapse when the timestamp passes.- The
permissionsmixins compute effective permissions by unioning the permission slugs of the roles the user holds through the membership models. The patient and encounter mixins also filter byPermissionContext. - The granted
roleis written and read through the role specs (RoleCreateSpec/RoleReadSpec), and each role'scontextsis bound to theRoleContextenum rather than an open string at the API layer.
Related
- Reference: Role
- Reference: Permission
- Reference: User
- Reference: Organization
- Reference: Base model
- Source: permission_association.py on GitHub
- Source: role/spec.py on GitHub
- Source: permissions.py on GitHub