Skip to main content

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.managingOrganization matches 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​

  1. Role Check: Confirm the client has the Patient role.
  2. Resource Validation:
    • If the resource is in the patient_compartment, allow access.
    • If the resource is an Organization, allow access if it matches Patient.managingOrganization.
    • If the resource is a Practitioner or PractitionerRole, allow access if linked to the patient’s managing organization.

Example:
A patient can view their managing organization (Organization/123) and practitioners affiliated with it, but not other organizations.

For Practitioner Clients​

  1. Role Check: Confirm the client has the Practitioner role.
  2. Fetch ActiveOrganizations: Query active PractitionerRole entries for the practitioner.
  3. 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.

Example:
A practitioner with ActiveOrganizations = [Organization/123] can:

  • Read Patient resources where Patient.managingOrganization = Organization/123.
  • Read Observation resources 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 params object 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_system must 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​

The rules are platform-defined, not project-defined

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 TypePractitioner callerPatient caller
PatientmanagingOrganization ∈ ActiveOrganizationsOrganization compartment
Observationsubject.managingOrganization ∈ ActiveOrganizationsPatient compartment
Encountersubject.managingOrganization ∈ ActiveOrganizationsPatient compartment
Consentpatient.managingOrganization ∈ ActiveOrganizationsPatient compartment
Organizationits own id ∈ ActiveOrganizationsmust be the patient's generalPractitioner or managingOrganization
Practitionerrole.organization ∈ ActiveOrganizations, or the practitioner is the callermust be the patient's generalPractitioner, or reachable through the organization compartment
PractitionerRoleorganization ∈ ActiveOrganizationsmust be the patient's generalPractitioner, or its organization must be the patient's managingOrganization
Appointmentdenied — no practitioner rule is defined for this typePatient 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
  • Behaviour:
    • A practitioner with a role at Organization/cardiology also reaches Organization/hospital, because inheritance adds the caller's ancestors to their active organizations.
    • A practitioner with a role at Organization/hospital does not thereby reach Organization/cardiology. Inheritance does not descend.
    • A practitioner at Organization/cardiology never reaches Organization/orthopedics; they are siblings, and no number of levels connects them.
Inheritance runs upward here, and downward for devices

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_compartment for simple patient-owned data.
  • Reserve legitimate_interest for 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.active is false,
    • practitioners in a sibling organization,
    • patients requesting an organization they are not linked to,
    • resources whose patient has no managingOrganization set.

  1. PractitionerRole: FHIR R4 Specification
  2. Compartments: FHIR Compartment Guide
  3. 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:

  1. Patient requests Bundle of resources.
  2. legitimate_interest allows:
    • Resources in their patient_compartment.
    • Organization and PractitionerRole linked to their managing organization.
  3. Blocks access to unrelated resources (e.g., another patient’s DiagnosticReport).