legitimate_interest Validator Documentation
legitimate_interest Validator Documentation
Advanced Policy-Driven Access Control for FHIR Resources
Intent
The legitimate_interest validator enforces granular, context-aware access rules to comply with regulatory frameworks (e.g., GDPR) or organizational policies. Unlike static compartment validators, it dynamically evaluates relationships between users, resources, and organizations to determine if a "legitimate interest" exists for accessing data.
Why Use This Validator?
- Multi-Tenant Systems: Manage access across organizations sharing a single FHIR repository.
- Regulatory Compliance: Enforce GDPR-like rules where access requires explicit justification.
- Hierarchical Access: Support nested organizations (e.g., hospitals with parent/child units).
Core Concepts
1. ActiveOrganizations
A practitioner’s ActiveOrganizations are derived from their active PractitionerRole resources:
ActiveOrganizations = PractitionerRole.where(
practitioner == Practitioner.id &&
active == true
).organization
Example: A doctor with roles at General Hospital and Cardiology Unit can access both compartments.
2. Organization Compartment
For practitioners, this includes:
- All patients where
Patient.managingOrganizationmatches an active organization. - All resources in those patients’ compartments (e.g., Observations, Conditions, Encounters). Not
Appointment: it resolves to no organization at all under this validator and therefore denies — see the validation strategy table.
3. Role Inheritance
Configure role_inheritance_levels to widen a practitioner's active organizations up the hierarchy — a role in a child organization also grants its ancestors.
- Level 0: no inheritance (default).
- Level 2: a practitioner in a ward also reaches its department and the hospital above it.
It does not work in the other direction: a role at the hospital does not reach into wards. See Role Inheritance Deep Dive below.
Detailed Workflow
For Patient Clients
- Role Check: Confirm the client has the
Patientrole. - Resource Validation:
- If the resource is in the
patient_compartment, allow access. - If the resource is an
Organization, allow access if it matchesPatient.managingOrganization. - If the resource is a
PractitionerorPractitionerRole, allow access if linked to the patient’s managing organization.
- If the resource is in the
Example:
A patient can view their managing organization (Organization/123) and practitioners affiliated with it, but not other organizations.
For Practitioner Clients
- Role Check: Confirm the client has the
Practitionerrole. - Fetch ActiveOrganizations: Query active
PractitionerRoleentries for the practitioner. - Resource Validation:
- Direct Access: Resource references an
ActiveOrganization(e.g.,Organization/123). - Indirect Access: Resource belongs to a patient in the practitioner’s organization compartment.
- Direct Access: Resource references an
Example:
A practitioner with ActiveOrganizations = [Organization/123] can:
- Read
Patientresources wherePatient.managingOrganization = Organization/123. - Read
Observationresources for those patients.
Advanced Configuration
1. Role-Specific Access
Require specific practitioner roles using required_role_system and required_role_code:
{
"client_role": "Practitioner",
"entity_name": "Patient",
"operation": "update",
"validator": "legitimate_interest",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "ict"
}
Explanation: only practitioners with a PractitionerRole whose code.coding carries that exact system and code may update patient records. Everyone else is denied before the validator runs at all. Only active roles count: the lookup filters on active=true, so deactivating a PractitionerRole is a working revocation and it does not have to be deleted.
Three things about these two keys are easy to get wrong:
- They are top-level keys on the rule. Nesting them inside a
paramsobject silently disables the gate — nothing reads them there, and the rule then grants whatever it would have granted without them. - They only work as a pair. A rule carrying one without the other denies every practitioner — it is unusable, not ungated. (This reversed in 2026; older documentation says such a rule is ignored.)
required_role_systemmust be the CodeSystem canonical, not the ValueSet. See Basic Validation Concepts for why a ValueSet URL denies every conformant caller with no signal.
2. Freshness
Organization and patient lookups are resolved fresh per request; results are reused within a single request but never across requests. A change to a PractitionerRole or a patient's managingOrganization therefore takes effect on the caller's very next request — there is no cache to wait out and none to invalidate.
Validation Strategy Table
This validator does not take its policy from your settings.json. MedBackend ships a table keyed by resource type × caller role, and a rule naming legitimate_interest opts that triple into whatever the table says. Your rule chooses whether the policy applies; the table decides what it is.
That has one consequence worth knowing in advance: for some resource types the table is deliberately blank, and a blank entry is an explicit denial. Adding a rule for such a type will not grant anything, and no amount of configuration will change it. The list of blanks is not stable across releases, so if a rule denies unexpectedly, treat this as the first thing to check rather than the last.
The only part you configure is role_inheritance_levels, below.
Entity-specific rules (a representative sample, not the whole table)
| Resource Type | Practitioner caller | Patient caller |
|---|---|---|
| Patient | managingOrganization ∈ ActiveOrganizations | Organization compartment |
| Observation | subject.managingOrganization ∈ ActiveOrganizations | Patient compartment |
| Encounter | subject.managingOrganization ∈ ActiveOrganizations | Patient compartment |
| Consent | patient.managingOrganization ∈ ActiveOrganizations | Patient compartment |
| Organization | its own id ∈ ActiveOrganizations | must be the patient's generalPractitioner or managingOrganization |
| Practitioner | role.organization ∈ ActiveOrganizations, or the practitioner is the caller | must be the patient's generalPractitioner, or reachable through the organization compartment |
| PractitionerRole | organization ∈ ActiveOrganizations | must be the patient's generalPractitioner, or its organization must be the patient's managingOrganization |
| Appointment | denied — no practitioner rule is defined for this type | Patient compartment |
That last row is not a typo, and it is the one that surprises people building scheduling features: Appointment grants nothing to a practitioner under legitimate_interest, however their organizations are configured. Scope practitioner access to appointments with a different validator.
Role Inheritance Deep Dive
Configuration
In the project's settings.json:
{
"rbac": {
"validators": {
"legitimate_interest": {
"role_inheritance_levels": 2
}
}
}
}
Use Case: Hospital Hierarchy
- Structure:
- Root:
Organization/hospital - Children:
Organization/cardiology,Organization/orthopedics
- Root:
- Behaviour:
- A practitioner with a role at
Organization/cardiologyalso reachesOrganization/hospital, because inheritance adds the caller's ancestors to their active organizations. - A practitioner with a role at
Organization/hospitaldoes not thereby reachOrganization/cardiology. Inheritance does not descend. - A practitioner at
Organization/cardiologynever reachesOrganization/orthopedics; they are siblings, and no number of levels connects them.
- A practitioner with a role at
For practitioners, role_inheritance_levels walks up the hierarchy: a role in a child organization grants the parent's resources as well. If you want a head-office practitioner to see every department, give them a role at each department — or scope by something other than inheritance.
The device_compartment validator's own inheritance_levels setting runs the other way, downward, so a device owned by a parent organization reaches its children. The two settings are separate and they are not mirror images of each other by accident; they answer different questions.
Performance Impact
- Network requests: each inheritance level adds a round-trip to resolve the next tier of the hierarchy.
- Recommendation: keep it to one or two levels. Deep hierarchies cost latency on every request, not just the first.
- A partial walk narrows; it never denies. If the hierarchy cannot be walked completely — during a FHIR outage, say — the organizations resolved so far are kept and used, and the incompleteness is recorded and logged rather than turned into a refusal. Inheritance only ever adds organizations, so a truncated walk can only turn a grant into a denial for a resource whose organization sat in the unreached tier. The practical consequence: during an outage a caller may lose access they normally have, and the denial will look exactly like an ordinary policy denial. If access disappears for inherited organizations only, check the logs for a truncated ancestor walk before re-reading your rules.
Best Practices
1. Avoid Overcomplication
- Use
patient_compartmentfor simple patient-owned data. - Reserve
legitimate_interestfor multi-organization or regulatory scenarios. - Check the platform table for your resource type before designing around this validator. A blank entry denies, and no configuration overrides it.
2. Diagnosing a denial
Work down this list, in order:
- Is there an entry in the platform table for this resource type and caller role, or is it blank?
- Does the caller have an active
PractitionerRole? Organization resolution reads only roles marked active, so an inactive one contributes nothing and a practitioner with no active role has no active organizations at all. - Does the resource actually resolve to an organization? Patient-linked types need the referenced patient to carry a
managingOrganization. - If the rule carries
required_role_system/required_role_code: are they top-level keys, both present, and is the system the CodeSystem canonical?
3. Testing
- Unit tests: validate rules against mock FHIR resources.
- Edge cases: test access denials for
- practitioners whose
PractitionerRole.activeis false, - practitioners in a sibling organization,
- patients requesting an organization they are not linked to,
- resources whose patient has no
managingOrganizationset.
- practitioners whose
FHIR Resource Links
- PractitionerRole: FHIR R4 Specification
- Compartments: FHIR Compartment Guide
- GDPR & Legitimate Interest: GDPR Article 6(1)(f)
Example: GDPR-Compliant Access
Scenario: A patient requests access to all data shared with their managing organization.
Validator Workflow:
- Patient requests
Bundleof resources. legitimate_interestallows:- Resources in their
patient_compartment. OrganizationandPractitionerRolelinked to their managing organization.
- Resources in their
- Blocks access to unrelated resources (e.g., another patient’s
DiagnosticReport).