On this page

Access Policies

Planning access control?

Use the Access Control Decision Guide to define roles, permissions, and administration. For a multi-tenant application, first choose the project and Patient boundaries in the Tenancy Design Decision Guide.

Introduction​

This document describes the core concepts of OpenOrchid Access Controls. Security and access controls are notoriously difficult. Complex business and regulatory requirements often lead to a mess of incomprehensible rules. OpenOrchid strives to create a simple and understandable model, which is flexible enough to handle all unique security needs.

Core Model​

Resources belong to a Project, a top-level container. An application can represent multiple customer organizations or service lines within one project, with access policies enforcing their permissions. Self-hosted applications can also use a project per customer.

Every user account can have one or more "Project Memberships". A project membership represents access to resources within a project. The user can either be granted access to all resources within the project, or limited access through policy criteria, including criteria that match Patient or tenant references in meta.compartment.

Access Policies​

Users within a Project can be assigned Access Policies. Access Policies are an advanced method of restricting access to certain resource types or even certain fields within a resource type.

Access policies allow you to:

  • Block access to any resource type
  • Grant read only access to any resource type
  • Grant read/write access to any resource type
  • Grant read only access to any property
  • Grant read/write access to any property

Access policies can grant access to resources in a Patient's FHIR compartment, or to resources with an approved tenant reference in meta.compartment.

Examples​

Resource Type​

The following access policy grants read/write access to only the "Patient" resource type:

{
  "resourceType": "AccessPolicy",
  "name": "Patient Example",
  "resource": [
    {
      "resourceType": "Patient"
    }
  ]
}

Criteria-based Access Control​

You can narrow the set of resources the user has access to by using the criteria field. The following policy uses a FHIR Search Query to grant access only to Patient resources who live in California.

See the Search documentation for more information on the types of filtering available.

{
  "resourceType": "AccessPolicy",
  "name": "Criteria Based Access Policy",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?address-state=CA"
    }
  ]
}
Limitations

While OpenOrchid AccessPolicies use the FHIR search syntax, it does not implement the full search specification. Specifically, the criteria syntax has the following limitations:

Read-only Access​

The following access policy grants read-only access to the Patient resource type:

{
  "resourceType": "AccessPolicy",
  "name": "Patient Example",
  "resource": [
    {
      "resourceType": "Patient",
      "readonly": true
    }
  ]
}

Similarly, the following access policy grants read/write access to the Patient resource, and also grants only read access to all other resources:

{
  "resourceType": "AccessPolicy",
  "name": "Read Only Example",
  "resource": [
    {
      "resourceType": "Patient"
    },
    {
      "resourceType": "*",
      "readonly": true
    }
  ]
}

Attempting to modify a read-only resource will result in an HTTP 403 Forbidden response.

FHIR Interactions​

The above read-only access is special case of the more general ability to control which interactions are allowed for a set of resources. The AccessPolicy.resource.interaction field can specify a subset of FHIR interactions that can be performed on resources of the given type. The following are equivalent:

{
  "resourceType": "AccessPolicy",
  "name": "Read-only modes",
  "resource": [
    // Legacy shorthand
    {
      "resourceType": "*",
      "readonly": true,
    },
    // Explicit list of interactions
    {
      "resourceType": "*",
      "interaction": ["read", "search", "history", "vread"]
    }
  ]
}

The interaction array can contain any of the supported interaction types:

  • create: Write new resources
  • read: Read a specific resource by ID
  • update: Update an existing resource, including patch operations
  • delete: Soft-delete resources
  • search: Search for resources of the given type
  • history: View the list of previous versions of a resource
  • vread: View a specific previous version of a resource
Related interactions

Some FHIR interactions are used in concert by the server, and so should be considered together:

  • Upserts require search to identify existing resources, in addition to both create and update
  • search access may be required as part of a create or update if the resource contains any Conditional references, in order to resolve references to individual resources
  • read access to referenced resource types is required in create and update interactions, if Project.checkReferencesOnWrite is set
  • history and vread are often specified together, to grant access to the entire version history of resources

For example, this policy allows creating new resources and viewing any resources for which the ID is known, but will prevent searching for or modifying existing resources:

{
  "resourceType": "AccessPolicy",
  "name": "Write-only Sandbox",
  "resource": [
    {
      "resourceType": "*",
      "interaction": ["create", "read"]
    }
  ]
}

Read-only Elements​

The following access policy grants read-only access to the Patient.name and Patient.address elements:

{
  "resourceType": "AccessPolicy",
  "name": "Patient Example",
  "resource": [
    {
      "resourceType": "Patient",
      "readonlyFields": ["name", "address"]
    }
  ]
}

Hidden Elements​

The following access policy grants read-only access to the "Patient" resource type, but hides "name" and "address":

{
  "resourceType": "AccessPolicy",
  "name": "Patient Example",
  "resource": [
    {
      "resourceType": "Patient",
      "hiddenFields": ["name", "address"]
    }
  ]
}

Write Constraints​

Constraints on writes to a resource can also be specified using FHIRPath expressions in the AccessPolicy.resource.writeConstraint field. These expressions may contain the special variable %before to refer to the resource as it existed before the write. Any property accesses will by default refer to the resource as it exists with updates applied, but the %after variable is also provided for convenience.

TIP

In case of a resource being created, %before will be undefined, so any expressions that refer to %before must account for this case. To select only updates or only creates, prefix the criteria with %before.exists() implies or %before.exists().not() implies respectively.

For example, an access policy with write constraints could be used to manage state transitions by prohibiting changing the status once a resource is marked as final, and ensure that a subject is set when the resource is finalized:

{
  "resourceType": "AccessPolicy",
  "name": "Write Constraints Access Policy",
  "resource": [
    {
      "resourceType": "Observation",
      "writeConstraint": [
        {
          "language": "text/fhirpath",
          "expression": "%before.exists() implies %before.status != 'final'"
        },
        {
          "language": "text/fhirpath",
          "expression": "status = 'final' implies subject.exists()"
        }
      ]
    }
  ]
}

FHIR compartments and meta.compartment​

A FHIR compartment groups resources related to an anchor resource. A resource can belong to several FHIR compartments. OpenOrchid determines FHIR Patient compartment membership from defined references, such as an Observation's subject. See the FHIR Patient CompartmentDefinition.

Using _compartment in an application search narrows that request's results; using it in policy criteria restricts permitted access. UI filtering can also use ordinary search parameters, such as owner or status where supported, without tenant or care-team assignments in meta.compartment. Choose policies when the restriction must apply to direct API access.

For application tenants or finer access groups within a project, OpenOrchid also includes explicit meta.accounts references in its computed meta.compartment. Assign them with $set-accounts, then grant access through matching policy criteria. Organization, HealthcareService, and CareTeam can represent these groups. A resource can have several tenant or care-team references in meta.compartment; those references do not create an automatic hierarchy. See Layering tenant and care-team access. Patient-wide assignment is appropriate when the Patient and its clinical records share the intended audience.

You can use the Patient's FHIR compartment to create an AccessPolicy for related resources, such as Observations and Tasks.

The AccessPolicy below grants access to all Observation resources that belong to Patient/xyz. The example takes advantage of the _compartment search parameter. To reuse one policy across patients, write %patient in the criteria and set the Patient on the user's membership. See Caregiver access.

{
  "resourceType": "AccessPolicy",
  "name": "Write Constraints Access Policy",
  "resource": [
    {
      "resourceType": "Observation",
      "criteria": "Observation?_compartment=Patient/xyz"
    }
  ]
}

Binaries​

The FHIR Binary resource is a special case. It is used to store arbitrary binary data, such as images or documents. Unlike other resources, Binary resources are not searchable, and therefore do not have search parameters, and therefore cannot use the criteria field in an AccessPolicy.

To control access to Binary resources, you must use the securityContext field. This field is a reference to another resource, such as a Patient, Practitioner, or Organization, that defines the context in which the binary data is relevant.

See Binary Security Context for more information on how to use the securityContext field to control access to Binary resources.

Parameterized Policies​

For more advanced access control configurations, You can use % variables to parameterize the access policy.

OpenOrchid provides the following built-in variables:

VariableDescription
%profileReference to the current user's profile resource (e.g., Practitioner/123 or Patient/123)
%profile.idThe ID portion of the user's profile resource (e.g., 123)
%patientThe patient this policy is scoped to. Defaults to the user's profile. A membership parameter named patient overrides it. See Caregiver access.
Example: General Practitioner Access Policy

The following access policy grants access to all Patient resources where the current user (a Practitioner) is listed as the general practitioner:

{
  "resourceType": "AccessPolicy",
  "name": "GP Access Policy",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?general-practitioner=%profile"
    }
  ]
}

You can also set custom variables for access policies on the user's ProjectMembership resource. This policy then acts like a template, that can be instantiated, potentially multiple times, with different values for each variable.

Example: Custom Access Policy Variables

In this example, the user with the parameterized policy shown above will only have access to Patient and DiagnosticReport resources, filtered by the relevant organizations.

{
  "resourceType": "AccessPolicy",
  "id": "123",
  "name": "Parameterized Access Policy",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?organization=%provider_organization"
    },
    {
      "resourceType": "DiagnosticReport",
      "criteria": "DiagnosticReport?performer=%provider_organization"
    }
  ]
}

We can set these variables on the user's ProjectMembership resource, as follows

{
  "resourceType": "ProjectMembership",
  "access": [
    // Provide access to Patients and Diagnostic Reports in Organization/abc
    {
      "policy": { "reference": "AccessPolicy/123" },
      "parameter": [
        {
          "name": "provider_organization",
          "valueReference": { "reference": "Organization/abc" }
        }
      ]
    },
    // Provide access to Patients and Diagnostic Reports in Organization/def
    {
      "policy": { "reference": "AccessPolicy/123" },
      "parameter": [
        {
          "name": "provider_organization",
          "valueReference": { "reference": "Organization/def" }
        }
      ]
    }
  ]
}

Some common use cases for parameterized policies include:

  • Granting access to parents of pediatric patients
  • Granting clinicians access to patients in a specific state, based on the clinician's state license

See this video demo for an illustration.

See this Github Discussion for more examples of access scenarios that can be created using these policies.

Parameterized policies are also the recommended pattern for temporary operational access, such as emergency patient access or temporary tenant access.

Emergency Access​

Emergency access, sometimes called Break Glass access, is a controlled workflow for granting temporary access to protected health information when normal access paths are not sufficient. This pattern supports ONC criterion d6 Emergency Access.

When implementing emergency access, keep the grant narrow, time-bound, and auditable:

  • Require an explicit reason before access is granted.
  • Scope access to the smallest useful resource set, such as one patient or one tenant.
  • Prefer short-lived sessions or a temporary ProjectMembership entry instead of standing broad access.
  • Record who requested access, who approved it, the reason, the patient or tenant scope, and the revocation time.
  • Review AuditEvent records after the access window closes.

For patient-specific escalation, one pattern is to add the support user or clinician's Practitioner profile to the patient's generalPractitioner list, then use an access policy scoped to that relationship:

{
  "resourceType": "AccessPolicy",
  "name": "Emergency Patient Access",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?general-practitioner=%profile"
    }
  ]
}

After the emergency window, remove the temporary generalPractitioner reference and confirm that the user can no longer read the patient record.

Temporary tenant access​

For support workflows, use a parameterized policy to grant access to one tenant for a limited operational window. Add a temporary ProjectMembership.access entry with the tenant reference as the parameter value, capture the reason and approval in your support system, and remove the entry when the work is complete.

{
  "resourceType": "ProjectMembership",
  "access": [
    {
      "policy": { "reference": "AccessPolicy/tenant-support-readonly" },
      "parameter": [
        {
          "name": "organization",
          "valueReference": { "reference": "Organization/clinic-a" }
        }
      ]
    }
  ]
}

Migrating from AccessPolicy.compartment​

Older access policies may use the top-level AccessPolicy.compartment field for account assignment. For new policies, prefer resource-level criteria with parameterized values. This keeps each resource rule explicit and lets the same policy template be reused with different ProjectMembership.access.parameter values.

AccessPolicy.compartment can stamp account references in meta.accounts on new resources; OpenOrchid includes those references in meta.compartment. Resource-level criteria only restrict access; preserve the intended assignment behavior through a controlled $set-accounts workflow when migrating.

For example, define the tenant filter on each resource that should be visible:

{
  "resourceType": "AccessPolicy",
  "name": "Organization Tenant Access",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?_compartment=%organization"
    },
    {
      "resourceType": "Observation",
      "criteria": "Observation?_compartment=%organization"
    },
    {
      "resourceType": "DiagnosticReport",
      "criteria": "DiagnosticReport?_compartment=%organization"
    }
  ]
}

Then set the concrete tenant value on each membership:

{
  "resourceType": "ProjectMembership",
  "access": [
    {
      "policy": { "reference": "AccessPolicy/organization-tenant-access" },
      "parameter": [
        {
          "name": "organization",
          "valueReference": { "reference": "Organization/clinic-a" }
        }
      ]
    }
  ]
}

A patient or caregiver policy uses the same two-part setup. The criteria refer to %patient, and the Patient reference lives on the membership parameter. See Caregiver access.

Example Access Policies​

Healthcare Partnerships​

A lab provider may need read access to Patients assigned to a specific client organization. This example uses an Organization tenant within one project:

  1. Create the Organization representing the client.
  2. Assign the intended Patients to it using $set-accounts. The operation sets meta.accounts; OpenOrchid includes those tenant references in meta.compartment.
  3. Grant the lab's membership the read-only policy below, replacing <organization-id> with the stored Organization ID.
{
  "resourceType": "AccessPolicy",
  "name": "Client Organization Patients",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?_compartment=Organization/<organization-id>",
      "readonly": true
    }
  ]
}

The rule grants Patient reads. Grant access to selected clinical resource types separately if required. For reuse across clients, replace the Organization reference with a membership policy parameter.

Older implementations may use the singular meta.account field and top-level AccessPolicy.compartment. Use meta.accounts, $set-accounts, and resource-level criteria for the patterns in this guide; see Migrating from AccessPolicy.compartment.

Patient Access​

If you are building a patient-facing application (such as FooMedical), a common requirement is to restrict each patient's access to only their own data. Use a templated access policy whose criteria are limited to that Patient's FHIR compartment.

Open Patient Registration

Open patient self-registration is disabled by default. You can invite patients without enabling it. See Open Patient Registration to configure self-registration and the required default policy.

For an example Patient Access Policy template, see your project's Default Access Policy.

The default Patient policy does not set AccessPolicy.compartment. Each resource entry limits what the patient can read with criteria that contain %patient, and login fills %patient from the Patient profile. A RelatedPerson's profile is not the patient, so that reference has to be supplied on the membership. See Caregiver access.

Binary Security Context

Binary access follows the permissions on its securityContext resource. Use a Patient reference when the file should be available to readers authorized for that Patient, as in this example:

{
  "resourceType": "Binary",
  "contentType": "application/pdf",
  "securityContext": {
    "reference": "Patient/homer-simpson"
  }
}

For a file restricted to one tenant or care team, choose a context with that same audience. A shared Patient may be too broad even when the file's clinical record has narrower permissions. See Binary Security Context for details.

You can configure your project to support open registration for patients, therefore it is crucial that you setup a Default Access Policy similar to the one above.

Caregiver Access​

A caregiver, such as a parent or guardian, logs in as a RelatedPerson. RelatedPerson.patient points at the patient whose record they should see. The RelatedPerson is the user's profile, so a policy cannot assume %profile is the patient.

Limit that user with resource criteria and a parameter on their ProjectMembership. The policy names the patient as %patient. The membership supplies the Patient reference. One policy template then works for every caregiver; only the parameter changes. The project's default RelatedPerson access policy is this template.

AccessPolicy.compartment is a different mechanism. When it is set, OpenOrchid adds that reference to meta.accounts on resources the user creates. It does not decide which existing records the user can read. The server already places a resource in the FHIR Patient compartment from the patient references on the resource. Criteria such as Observation?_compartment=%patient match that FHIR Patient compartment. The default Patient and RelatedPerson policies leave compartment unset and rely on those criteria.

1. Write criteria that use %patient​

{
  "resourceType": "AccessPolicy",
  "name": "Caregiver Access",
  "resource": [
    {
      "resourceType": "Patient",
      "criteria": "Patient?_id=%patient.id"
    },
    {
      "resourceType": "Observation",
      "criteria": "Observation?_compartment=%patient"
    },
    {
      "resourceType": "Condition",
      "criteria": "Condition?_compartment=%patient"
    }
  ]
}

The default Patient and RelatedPerson policies follow this shape for the clinical resource types in the FHIR Patient compartment. Patient is matched by id. Communication is matched on sender and recipient, because those are the references that place it in the FHIR Patient compartment. The other clinical types are matched with _compartment=%patient, as Observation and Condition are above.

RelatedPerson records are read-only in both policies, so a patient or caregiver cannot add a contact or change which Patient one points at. The one exception is that a caregiver can update their own RelatedPerson profile, as long as its patient stays the same.

2. Put the Patient on the membership​

At login, OpenOrchid replaces every %patient in the policy. A parameter named patient on ProjectMembership.access supplies the reference. %patient.id is the id portion of that reference.

{
  "resourceType": "ProjectMembership",
  "profile": { "reference": "RelatedPerson/marge-bart" },
  "access": [
    {
      "policy": { "reference": "AccessPolicy/caregiver-access" },
      "parameter": [
        {
          "name": "patient",
          "valueReference": { "reference": "Patient/bart-simpson" }
        }
      ]
    }
  ]
}

If that parameter is missing, %patient falls back to the user's profile. That is the right value for a Patient, whose profile is the patient, so the default Patient policy needs no parameter. It is the wrong value for a RelatedPerson: %patient would be the RelatedPerson, and the criteria would not match the patient's compartment.

A caregiver of more than one patient gets one access entry per patient, each with its own patient parameter and the same policy. Each caregiver has their own membership, so several caregivers of one patient are several memberships.

3. Pass the Patient when you invite​

The Patient reference has to be on the membership before the caregiver logs in. When you invite a RelatedPerson and do not set an access policy, OpenOrchid does this for you: it applies the project's default RelatedPerson policy and copies RelatedPerson.patient onto the membership as the patient parameter.

In the invite form, choose the RelatedPerson role, select the Patient, and leave Access Policy empty.

With the Invite API, pass patient and omit membership.accessPolicy and membership.access:

await medplum.post('admin/projects/:projectId/invite', {
  resourceType: 'RelatedPerson',
  firstName: 'Marge',
  lastName: 'Simpson',
  email: 'marge@example.com',
  patient: { reference: 'Patient/bart-simpson' },
});

The resulting membership has the default policy and the parameter:

{
  "resourceType": "ProjectMembership",
  "profile": { "reference": "RelatedPerson/marge-bart" },
  "access": [
    {
      "policy": { "reference": "AccessPolicy/<project-default-related-person>" },
      "parameter": [
        {
          "name": "patient",
          "valueReference": { "reference": "Patient/bart-simpson" }
        }
      ]
    }
  ]
}

If the invite sets an access policy (accessPolicy, access, membership.accessPolicy, or membership.access), OpenOrchid uses what you sent and does not add the parameter. The invite form's Access Policy field sends accessPolicy, so choosing a policy there skips this step. Put the policy on membership.access yourself, including the patient parameter, or add the parameter to the membership after the invite. The same applies when the project has no RelatedPerson entry in Project.defaultAccessPolicies.

WebSocket Subscriptions (useSubscription Hook)​

To use the useSubscription hook, users need two things in their access policy:

  1. Permission to create Subscription resources — The hook creates an in-memory Subscription on behalf of the user. Use a criteria scoped to websocket subscriptions owned by the current user so that users can only manage their own subscriptions:

    Subscription?type=websocket&author=%profile
    
  2. Read access to each subscribed resource type — Users must also have read access for every resource type they intend to subscribe to. For example, if your app calls useSubscription('Communication?...'), the policy must grant at least read on Communication.

{
  "resourceType": "AccessPolicy",
  "name": "WebSocket Subscription Policy",
  "resource": [
    {
      "resourceType": "Subscription",
      "criteria": "Subscription?type=websocket&author=%profile"
    },
    {
      "resourceType": "Communication"
    }
  ]
}

Add one entry per resource type your application subscribes to.

Streamlined linkage and RBAC Control with AccessPolicy.basedOn​

In OpenOrchid, users can attach one or more parameterized AccessPolicy resources to their ProjectMembership. During runtime, the OpenOrchid server consolidates these resources into a single enforceable AccessPolicy.
The recent enhancements introduce the AccessPolicy.basedOn element, which enhances traceability and linkage of access policies. These improvements are made accessible to the front end via the /auth/me endpoint, enabling seamless role-based access control (RBAC) for users. For more information, see the PR detailing the changes.

This update allows for more granular control and visibility in RBAC implementations, where users have varying access levels based on their group or policy.

  • Endpoint: /auth/me

    • This endpoint provides access to the user's AccessPolicy information, including the basedOn element.
  • AccessPolicy.basedOn

    • This element is an array containing references to original AccessPolicy resources.
    • It provides traceability by linking the resolved AccessPolicy back to its source policies.
    • At runtime, OpenOrchid resolves all nested and parameterized policies to build the basedOn array.
  • Client-Side

    • The introduction of AccessPolicy.basedOn allows for client-side changes based on the user's group/policy.
    • This is especially useful for Role-Based Access Control (RBAC) implementations where different users may have varying access levels.

Default Access Policies​

New projects are created with a set of default access policies, one per profile type. These are stored on the Project.defaultAccessPolicies element, which maps a profileType to the AccessPolicy applied when a user of that type is invited without an explicit policy:

Profile typeDefault policy grants
PractitionerRead/write access to all resource types, except read-only access to a curated set of knowledge, terminology, and conformance resource types, plus read-only Bot and ClientApplication with ClientApplication secrets hidden, and create-only Binary.
AdminFull read/write access to all resource types (equivalent to having no access policy).
PatientAccess scoped to the Patient's own FHIR compartment. %patient is the Patient profile. RelatedPerson records are read-only. See Patient access.
RelatedPersonAccess scoped to the associated Patient's FHIR compartment. The invite stores that Patient on the membership. RelatedPerson records are read-only, except the caregiver's own profile. See Caregiver access.
BotRead/write access to all resource types, except read-only access to AccessPolicy, Bot, and ClientApplication, create-only Binary, and no access to ClientApplication secrets.
ClientApplicationRead/write access to all resource types, except read-only access to AccessPolicy, Bot, and ClientApplication, create-only Binary, and no access to ClientApplication secrets.

When you invite a user without specifying an access policy, OpenOrchid applies the matching default for that profile type. Practitioners invited with the admin flag receive the Admin default; all other practitioners receive the Practitioner default. Likewise, when you create a Bot or create a ClientApplication without specifying an access policy, it receives the Bot or ClientApplication default. This includes the default client created with each new project.

Deploying Bots with a ClientApplication

The ClientApplication default access policy makes Bot read-only, so clients with that policy, including the default client in new projects, cannot save or deploy Bots. Saving requires write access to Bot. The $deploy operation additionally requires project-administrator or super-administrator status. For CLI or CI/CD deployments, set admin: true on the deployment client's ProjectMembership and assign it an AccessPolicy that grants write access to Bot.

Create-only Binary

The Practitioner, Bot, and ClientApplication defaults can create and read Binary resources, but not update or delete them, or request an upload URL for an existing one (Binary/$presigned-url?upload=true). To change a file, upload a new Binary and update the reference to it. If your application replaces Binary content in place, give those users an access policy that grants write access to Binary.

Switching a user to (or from) admin​

The admin flag does not bypass a user's access policy — it only grants access to project administration resource types (such as ProjectMembership and User) on top of the existing policy. Changing the admin flag never changes the membership's accessPolicy.

When you promote a user to admin, also set their accessPolicy to the access they need, such as the project's Admin default. Otherwise they remain restricted by their current policy, for example the read-only rules of the Practitioner default. Likewise, when you demote an admin, replace the Admin default with a non-admin policy such as the Practitioner default.