Skip to main content

MedBackend Role-Based Access Control System

FHIR-Compliant Authorization Framework with Multi-Tenant Support

B2C Healthcare Model: Single Organization Architecture​

(Direct Patient Care Platforms)

Structural Components​

Resource Relationships

  1. Central Organization

    • Single Organization resource acts as data controller
    • Example Identifier: Organization/med-org-001
    • FHIR Profile: US Core Organization
  2. Patient Resources

    • Contain mandatory reference to central org:
      "managingOrganization": {
      "reference": "Organization/med-org-001"
      }
    • Validation: Patient compartment restricts access to self-owned data
  3. Care Team Structure

    • Practitioners require active PractitionerRole with:
      "organization": {
      "reference": "Organization/med-org-001"
      }

Access Validation Flow​

Recommended Use Cases

  • Direct-to-consumer telehealth platforms
  • Single-clinic EHR systems
  • Mobile health applications with centralized care teams

B2B2C Healthcare Model: Multi-Tenant Architecture​

(Enterprise Health Platforms)

Structural Hierarchy​

Creating a child organization​

A project's root Organization is created with POST /fhir/organizations/bootstrap. Child organizations beneath it are created with a dedicated endpoint:

POST /fhir/organizations/children
X-Project-ID: <project-uid>

{
"name": "Cardiology Department",
"partOf": "<root-organization-id>"
}

partOf is the parent's FHIR resource id, not a Reference object. The endpoint pins the created resource's partOf to Organization/<that id>, and partOf does round-trip when the organization is read back.

This endpoint is the only way to set it. The ordinary Organization create and update endpoints do not accept partOf — it is not among their fields, so sending it there is silently dropped rather than rejected. Building a hierarchy by adding partOf to a normal update appears to succeed and changes nothing.

It requires manage_project_settings, not create_fhir_resources. A child organization is a new authorization anchor rather than an ordinary FHIR resource: where practitioner role inheritance is enabled — that is, legitimate_interest is in use and its role_inheritance_levels is above zero, as the shipped clinical_provider template sets it — the walk runs upward, so a practitioner holding a role in the child gains the parent's data. Creating one can therefore widen access to the parent, which makes it a project-settings action.

The parent must be this project's own root Organization — one level, deliberately. That is a restriction on what this endpoint will build, not a statement about how deep the platform traverses; inheritance can walk further than one level where it is configured to. The restriction exists because a free-choice parent is an authorization input that a typo silently redirects.

ResponseCodeMeaning
409NOT_BOOTSTRAPPEDThe project has no root Organization yet. Call /fhir/organizations/bootstrap first — a child with nothing above it is a second root, not a hierarchy
403PARENT_NOT_HELDpartOf names an organization that is not this project's root
409PARENT_UNRESOLVEDThe project's root Organization did not resolve in the FHIR server. Refused on both "not there" and "could not check" — a FHIR error is not evidence the reference is good
409PROJECT_NOT_READYThe project's status is not active. The body carries project_status
409PROJECT_DECOMMISSIONEDThe project has been decommissioned
A new project is pending, not active

Every organization endpoint refuses a project whose status is not active, with a 409. A project is pending when it is first created and becomes active once provisioning completes. Any flow that chains organization setup onto project creation must wait for that transition rather than issuing the calls back to back.

A dangling partOf is worse than no child at all — it looks like a hierarchy and traverses to nothing, silently. That is why both the "parent missing" and "parent unreadable" cases refuse rather than proceed.

Role Coding System​

A role lives in PractitionerRole.code.coding, and the system half of that coding must name the CodeSystem that defines the code. FHIR R4 defines Coding.system as "the identification of the code system that defines the meaning of the symbol in the code" — so a ValueSet URL, which only selects codes, does not belong there.

HL7 defines six standard practitioner roles, all under http://terminology.hl7.org/CodeSystem/practitioner-role:

CodeDisplay
doctorDoctor
nurseNurse
pharmacistPharmacist
researcherResearcher
teacherTeacher/educator
ictICT professional

HL7's practitioner-role ValueSet selects those six plus 494 SNOMED CT occupation codes — 158965000 "Medical practitioner", for example. Those SNOMED members are legitimate, and their system is http://snomed.info/sct. Use SNOMED only for a code SNOMED actually defines; the six above are not SNOMED codes, and writing one of them under a SNOMED system produces a role that matches nothing.

Do not use a ValueSet URL as the system

Neither http://hl7.org/fhir/ValueSet/practitioner-role (the ValueSet canonical) nor http://hl7.org/fhir/R4/valueset-practitioner-role (its HTML documentation page) identifies a code system. A role or a rule written against either matches no conformant PractitionerRole — and the failure is a silent denial, not an error.

Role Assignment Example

{
"resourceType": "PractitionerRole",
"active": true,
"practitioner": { "reference": "Practitioner/practitioner-001" },
"organization": { "reference": "Organization/med-org-001" },
"code": [
{
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"code": "doctor",
"display": "Doctor"
}
]
}
]
}

active, practitioner, organization and code are all required when a PractitionerRole is created through the management API (POST /fhir/practitioner-roles), where active must also be true. They are required there because active and code are the two fields role gating reads.

The FHIR GraphQL API enforces none of this — FHIR itself marks every one of those fields optional, so a role created through practitionerRoleCreate can carry no code at all. Such a role is invisible to role gating, which reads nothing else, and the resulting denials look exactly like correct enforcement. If you create roles that way, supply code yourself.

Setting active to false later genuinely revokes: a deactivated role contributes no organization membership and satisfies no role requirement.

What a role code grants​

Nothing, on its own. Access is decided by your project's validation rules, and a rule opts in to role gating by carrying both required_role_system and required_role_code:

{
"client_role": "Practitioner",
"entity_name": "ServiceRequest",
"operation": "create",
"validator": "allowed",
"required_role_system": "http://terminology.hl7.org/CodeSystem/practitioner-role",
"required_role_code": "doctor"
}

Any practitioner holding the doctor role may create a ServiceRequest; any other practitioner is denied.

  • The gate does not replace the validator — both must pass. allowed performs no further check, so the rule above turns entirely on the role. Naming a compartment validator instead narrows it further, to resources inside that caller's compartment.
  • Both halves are compared exactly against an entry in the caller's PractitionerRole.code.coding. A wrong system or a misspelled code does not loosen the rule — it denies every caller, indistinguishably from a rule that is correctly strict.
  • Gating is strictly opt-in. A rule carrying neither key behaves exactly as it did before the feature existed: granted without consulting PractitionerRole at all, and with no extra FHIR round-trip.
  • Role gating applies to Practitioner callers only. On a Patient, Device or RelatedPerson rule the two keys are silently ignored — see Requiring a Practitioner Role.
A role requirement is not scoped to an organization

The role check matches on the caller's PractitionerRole and its code.coding alone; the role's organization is not consulted. A practitioner holding doctor in any organization in the project satisfies a doctor requirement everywhere in that project.

Where the grant must also stay inside an organization, express that in the validator — pair the role requirement with organization_compartment or legitimate_interest, which do read the caller's organizations.

Publishing a project configuration refuses a required_role_system that names a ValueSet, refuses a code the HL7 CodeSystem does not define, and refuses a rule carrying only one of the two keys. Codes under http://snomed.info/sct or under a CodeSystem of your own are accepted — their membership cannot be enumerated, so they are not second-guessed.

None of the shipped RBAC templates use role gating; every role-gated rule is one you add yourself.

Organization Inheritance​

The organizations a practitioner is active in come from their PractitionerRole.organization references. That set can be extended with the organizations' ancestors, walking Organization.partOf upward. It is configured under the legitimate_interest block, and applies wherever a rule resolves the caller's active organizations:

{
"rbac": {
"default_access": "Forbidden",
"validators": {
"legitimate_interest": {
"enabled": true,
"role_inheritance_levels": 1
}
}
}
}
  • A practitioner whose role sits on Cardiology Department is, with one level of inheritance, also active in its parent Hospital.
  • Inheritance only ever adds organizations; it never removes one resolved directly.
  • Omitting role_inheritance_levels means no inheritance. The shipped configuration templates set it to 1.
  • enabled: true on legitimate_interest is required for inheritance to be consulted at all.

Custom Organization Compartment​

Extended FHIR Access Control Mechanism

Implementation Details​

Compartment Definition

{
"extension": [
{
"url": "https://medbackend.com/fhir/Extensions/organization-compartment",
"valueReference": {
"reference": "Organization/tenant-org-002"
}
}
]
}

Use Case Example - Shared Library Resource

{
"resourceType": "Library",
"extension": [
{
"url": "https://medbackend.com/fhir/Extensions/organization-compartment",
"valueReference": {
"reference": "Organization/research-org-005"
}
}
],
"status": "active",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/library-type",
"code": "logic-library"
}
]
}
}

The extension is read on reads only. On a create, update or delete the compartment is decided from the caller's own organizations, never from a tag in the submitted payload — otherwise a caller could tag a resource with an organization they belong to and write anything in the project.

Choosing a validator​

There is no fixed table of which compartment applies to which resource type: each rule in your configuration names the validator it uses. The validators available are:

ValidatorGrants when
patient_compartmentthe resource is in the calling patient's compartment
practitioner_compartmentthe resource is in the calling practitioner's compartment
relatedperson_compartmentthe resource is in the calling related person's compartment
device_compartmentthe resource is in the calling device's compartment
organization_compartmentthe resource carries the organization-compartment extension for one of the caller's organizations, or references it
organization_consentthe above, AND an active Consent naming the caller or their organization. Practitioner caller only
general_practitionercaller and patient are linked by Patient.generalPractitioner — never grants create
legitimate_interestthe platform-shipped legitimate-interest rule for this resource type and caller role is satisfied — usually that the resource's organization is one of the caller's active organizations. You configure only enabled and role_inheritance_levels; the rules themselves are not tenant-authored, and several entities deny outright
allowedalways — a blanket grant with no further check
forbiddennever

A rule naming a validator that is not in this list is denied, not skipped.


Security Best Practices​

  1. Keep default_access at "Forbidden"

    • A resource/operation pair with no matching rule is then denied rather than granted
    • Grant access by adding rules, never by widening the default
  2. Grant the narrowest validator that works

    • allowed performs no check at all — it is a blanket grant, not a light one
    • Where two rules match the same role/resource/operation triple, both must pass; adding a rule narrows access, it never widens it
  3. Test role gates against a caller who lacks the role

    • A misconfigured gate is inert rather than erroring, so a test that only presents a caller who holds the role passes either way
    • See Requiring a Practitioner Role for the three conditions a gate must satisfy

Implementation References​

  1. FHIR Compartments: HL7 Compartment Definition
  2. Practitioner role codes: HL7 practitioner-role CodeSystem — the six standard codes and their canonical URL
  3. Wider role vocabulary: HL7 practitioner-role ValueSet — the six above plus the SNOMED CT occupation codes
  4. Requiring a Practitioner Role — writing and verifying a role-gated rule