On this page
- Partition-Based Rules and Permissions
- Partition-Based Sync Rule Behavior
- Partition-Based Sync Permissions
- Key Concepts
- Read Permissions
- Write Permissions
- Permission Strategies
- Global Permissions
- Permissions for Specific Partitions
- Permissions for Specific Users
- Permissions Based on User Data
- Function Rules
- Flexible Sync Session Roles and Rules
- Available Expansions in Flexible Sync Rules Quick Reference
- Default Roles
- Flexible Sync Rules
- Read Permissions
- Write Permissions
Atlas App Services enforces data access rules for all requests to a synced cluster. Sync rules are dynamic JSON expressions that specify a given user's ability to view and modify synced data.
This page describes data access rules for synced clusters. Non-synced cluster use a different rules model that sync rules override. If sync is enabled for a cluster, any non-sync rules defined for the cluster do not apply.
If your app does not use sync, check out MongoDB Collection Rules for more information on rules for non-synced clusters.
Whenever a user opens a synced realm from a client app, App Services evaluates your app's rules and determines if the user has read and write permissions for the partition. Users must have read permission to sync and read data in a realm and must have write permission to create, modify, or delete objects. Write permission implies read permission, so if a user has write permission then they also have read permission.
Sync rules apply to specific partitions and are coupled to your app's data model by the partition key. Consider the following behavior when designing your schemas to ensure that you have appropriate data access granularity and don't accidentally leak sensitive information.
- Sync rules apply dynamically based on the user. One user may have full read & write access to a partition while another user has only read permissions or is unable to access the partition entirely. You control these permissions by defining JSON expressions.
- Sync rules apply equally to all objects in a partition. If a user has read or write permission for a given partition then they can read or modify all synced fields of any object in the partition.
- Write permissions require read permissions, so a user with write access to a partition also has read access regardless of the defined read permission rules.
App Services enforces dynamic, user-specific read and write permissions to secure the data in each partition. You define permissions with JSON rule expressions that control whether or not a given user has read or write access to the data in a given partition. App Services evaluates a user's permissions every time they open a synced realm.
A user with read permissions for a given partition can view all fields of any object in the corresponding synced realm. Read permissions do not permit a user to modify data.
A user with write permissions for a given partition can modify the value of any field of any object in the corresponding synced realm. Write permissions require read permissions, so any user that can modify data can also view that data before and after it's modified.
You can structure your read and write permission expressions as a set of permission strategies that apply to your partition strategy. The following strategies outline common approaches that you might take to define sync read and write permissions for your app.
You can define global permissions that apply to all users for all partitions. This is, in essence, a choice to not implement user-specific permissions in favor of universal read or write permissions that apply to all users.
To define a global read or write permission, specify a boolean value or a JSON expression that always evaluates to the same boolean value.
This expression always evaluates to
You can define permissions that apply to a specific partition or a groups of partitions by explicitly specifying their partition values.
This expression means that all users have the given access permissions for data with a partition value of
This expression means that all users have the given access permissions for data with any of the specified partition values.
You can define permissions that apply to a specific user or a group of users by explicitly specifying their ID values.
This expression means that the user with id
This expression means that any user with one of the specified user ID values has the given access permissions for data in any partition.
You can define permissions that apply to users based on specific data defined in their custom user data document, metadata fields, or other data from an authentication provider.
This expression means that a user has read access to a partition if the partition value is listed in the
This expression means that a user has write access to a partition if the partition value is listed in the
You can define complex dynamic permissions by evaluating a function that returns a boolean value. This is useful for permission schemes that require you to access external systems or other custom logic that you cannot express solely in JSON expressions.
This expression calls the
In flexible sync, a session role determines which permissions apply for the duration of a sync session.
A session role consists of:
You define Flexible Sync session roles on a per-collection basis. Each "collection" when using Atlas Device Sync corresponds to a Realm Object type.
When a user begins a session by opening a synced realm, App Services determines which session role applies for the user for each collection in play. App Services considers session roles in the order that you defined them in the configuration. It evaluates each session role's "apply when" expression until one evaluates to true. If no session role applies, the user has no permissions for that collection.
Your session role "apply when" expressions can use JSON expansions to refer to user metadata and custom user data and even call functions to determine the given user's role. However, because App Services only evaluates session roles at the start of a session -- that is, before any query for specific documents -- you can't refer to a document or its field values to determine whether the session role applies.
The session role stays assigned for the duration of the session. If you make changes to an applied session role while a user is in the middle of a session, App Services does not evaluate the updated session role until the next time the user starts a session. Likewise, if something changes that would qualify the user for a different session role, the user's current session role does not change until the next session.
App Services triggers a client reset if anything about the session role changed since the previous session.
At the start of a session, App Services expands all JSON expansions in the "apply when", read, and write expressions and stores the result. This has the following implications:
- If the value changes during a session, App Services continues to use the value as it was at the time of session start.
- On the next session, if the value is different from what it was at the start of this session, App Services triggers a client reset.
- You cannot use the
%functionoperator in read and write rules. Functions would not operate on a per-document basis.
- You cannot store permissions information (such as "which document IDs may this user access?") in the user object.
Can Use in "Apply When"?
Can Use in Read & Write Rules?
Yes with an important caveat
Yes with an important caveat
No. These expansions refer to the document. App Services evaluates "apply when" expressions at session start, so there's no document to evaluate.
No. These expansions might access non-queryable fields of the document, which is not possible.
No. App Services expands the expansions at the start of the session, so the function would not operate on a per-document basis.
Yes. However, note that you cannot currently have an array field as a queryable field on a document.
You can create one or more default session roles that apply across all collections. If a collection does not have any custom session roles defined, session role resolution reverts to default session roles.
Because session roles apply only at the session level and not on a per-document level, most apps only need one (default) session role with per-document logic in that session role's read and write rules. With Flexible Sync, session roles can be thought of as a way to group read and write rule expressions to organize your code.
You define a read and write rule expression pair for every session role.
Rule expressions can refer to the queryable fields of your data model.
In response to a Flexible Sync subscription query, App Services evaluates the read and write rule expressions for each document that matches the query. The client only receives documents where the rule expression evaluates to "true".
Consider the following behavior when designing your schemas to ensure that you have appropriate data access granularity and don't accidentally leak sensitive information.
- Sync rules apply dynamically based on the value of the queryable field or user metadata. One user may have full read & write access to a document while another user has only read permissions or is unable to access the document. You control these permissions by defining JSON expressions.
- There are no field-level permissions. Sync rules apply to all documents in a query on a per-collection basis. If a user has read or write permission for a given collection, then they can read or modify all synced fields of any document that matches the sync query in the collection.
- Write permissions require read permissions, so a user with write access to a collection also has read access regardless of the defined (or undefined) read permission rule.
For a guide to setting up flexible sync with common permissions models, see Flexible Sync Permissions Guide.
A user with read permissions for a given collection can view all fields of any object matching the client-side query. Read permissions do not permit a user to modify data.
A user with write permissions for a given collection can modify the value of any field of any object matching the client-side query. Write permissions require read permissions, so any user that can modify data can also view that data before and after it's modified.