Product Overview
Features and Strengths
Use Cases
System Limits
json to describe security rules. Here is a configuration example:{"read": "auth.uid==doc.uid","write": "doc.name=='zzz'"}
key of the json configuration indicates the user's operation type, while the value is an expression. When the result of executing the expression is true, the user's operation is allowed; otherwise, the operation is not allowed.Operation Type | Description | Default Value |
read | Read a document | false |
write | Write a document, subdivided into create, update, and delete. | false |
create | Create a document | N/A |
update | Update a document. | N/A |
delete | Delete a document. | N/A |
write rule is read by default. For example, the developer's security rule configuration is as follows:{"read": ".....","write": "....","create": "...."}
create, while an update operation will read the rule under write.Field Name | Type | Description |
data | object | Request data exists during create and update operations, representing a request for data. |
Field Name | Type | Description |
loginType | string | Login method. Requests from WeChat Mini Programs do not have this value. It is only available for web logins. |
uid | string | User unique ID. Requests from WeChat Mini Programs do not have this value. |
openid | string | User openid. This value only exists in WeChat login mode. |
Enumeration Value | Login Method Description |
WECHAT_PUBLIC | WeChat official account |
WECHAT_OPEN | Weixin Open Platform |
ANONYMOUS | Anonymous Login |
EMAIL | Email Login |
CUSTOM | Custom Login |
Operators | Description | Example | Example Explanation (Collection Query) |
== | Equal to | auth.uid == 'zzz' | The user's uid is zzz. |
!= | Not equal to | auth.uid != 'zzz' | The user's uid is not zzz. |
> | Greater than | doc.age>10 | The age attribute of the query condition is greater than 10. |
>= | Greater than or equal to | doc.age>=10 | The age attribute of the query condition is greater than or equal to 10. |
< | Less than | doc.age>10 | The age attribute of the query condition is less than 10. |
<= | Less than or equal to | doc.age>=10 | The age attribute of the query condition is less than or equal to 10. |
in | Exists in the collection | auth.uid in ['zzz','aaa'] | The user's uid is one of ['zzz','aaa']. |
!(xx in []) | Not exists in collection, described using in method !(a in [1,2,3]) | !(auth.uid in ['zzz','aaa']) | The user's uid is not any of ['zzz','aaa']. |
&& | And | auth.uid == 'zzz' && doc.age>10 | The user's uid is zzz, and the age attribute of the query condition is greater than 10. |
|| | Or | auth.uid == 'zzz' || doc.age>10 | The user's uid is zzz, or the age attribute of the query condition is greater than 10. |
. | Object element accessor | auth.uid | User's uid. |
[] | Array accessor attribute | get('database.collection_a.user')[auth.uid] == 'zzz' | In collection_a, document with id as user, where the attribute value whose key is user uid is zzz. |
database.collection name.document id. Determine whether user operations conform to security rules by accessing data from other documents.get(path: string): Document | null | undefinedInput Parameter | Type | Description |
path | string | Non-empty, a string formatted as database.collection name.document id. Its value can be obtained through multiple calculation methods, such as using a string template for concatenation: (database.${doc.collction}.${doc._id}). |
{"read": "get('database.test.123')[auth.uid] in [1,2,3]","delete": "get('xxxx')[auth.uid] == 1 && doc.user in ['ersed','sfsdf'] "}
{"read:": "auth.openid in get(`database.B.${doc.shopId}`).owner"}
3 get functions and can access up to 10 different documents.get(get(path)).get produces one read operation. When using variables, each get and each variable value produces one read operation. For example: If the rule is get(database.collection.${doc._id}).test and the query is _.or([{_id:1},{_id:2},{_id:3},{_id:4},{_id:5}])}, 5 reads will be produced. The system will cache reads for the same doc and the same field.doc condition.where conditions or a query restricted by aggregated search match. If there is an aggregated search, only the first match restriction is matched.key is _openid and value is {openid}, or key is uid and value is {uid}, the server will automatically replace the value with the actual one.doc refers to query conditions. This subset means all possible subsets of the rules, rather than subsets of actual data.read, update, and delete.// Security rule configuration for colleciton_a.{"read":"doc.age>10"}// Complies with security rules.let queryRes = db.collection('colleciton_a').where({age:_.gt(10)}).get()// Not complies with security rules.let queryRes = db.collection('colleciton_a').where({age:_.gt(8)}).get// Complies with security rules.let res = await db.collection('collection_a').aggregate().match({age: _.gt(10)}).project({age: 1}).end()// Not complies with security rules.let res = await db.collection('collection_a').aggregate().match({age: _.gt(8)}).project({age: 1}).end()
ID (Migration Modification Required)read, update, delete, and create.create, the system will verify whether the data written conforms to the security rule restrictions.read, update, or delete, and the security rule contains doc restrictions, the system will first read the document data from the db once, then determine whether it complies with the security rule.update operations, the system only verifies the existing document data in the database and does not validate written data. It does not guarantee the atomicity of this operation.doc operations (such as doc.get and doc.set) are performed only by specifying _id and their query conditions only include {_id:"xxx"}, therefore, in most cases, they do not meet the subset requirement of security rules (unless performing a read operation under "read": true or a write operation under "write": true). Hence, they need to be converted into equivalent forms where the query conditions include the security rules or their subsets.// Security rule configuration for colleciton_a.{"read":"doc._openid == auth.openid"}// Data for document id='ccc' is{age:12}// The security rule is not met, and subset requirements are not satisfied.let queryRes = db.collection('colleciton_a').doc('ccc').get()// The security rule is met and rewritten as where query.let queryRes = db.collection('colleciton_a').where({_id: "ccc", _openid: "{openid}"}).get()
command | description |
or | || Logical OR |
and | && Logical AND |
command | description |
eq | == |
ne/neq | != |
gt | > |
gte | >= |
lt | < |
lte | <= |
in | in |
nin | !(in []) |
command | description |
set | Overwrite, {key: set(object)} |
remove | Delete field, {key: remove()} |
id will produce one data access.{"read": true,"write": "doc._openid == auth.openid" // Login method is WeChat."write": "doc._openid == auth.uid" // Login method is not WeChat.}
{"read": "doc._openid == auth.openid" //Login method is WeChat."read": "doc._openid == auth.uid" // Login method is not WeChat."write": "doc._openid == auth.openid" //Login method is WeChat."write": "doc._openid == auth.uid" // Login method is not WeChat.}
{"read": true,"write": false}
{"read": false,"write": false}
owner and can be shared with the writer.writer has all access permissions that a commenter has and can edit the story content as well.owner can edit any part of the story and control other users' access permission.stories where each document represents a story, a collection of comments where each document represents a comment on that story, and a collection of roles where each document contains a user's role for a story. To keep track of access roles, you need to add a roles field to map user IDs to roles:story document data is as follows:{"id": `storyid`,"title": "A Great Story","content": "Once upon a time ..."}
role document data is as follows:{"id": `storyid`,"roles": {"alice": "owner","bob": "writer","david": "writer"// ...}}
comment document data is as follows. The comment contains only three fields: the commenter's user ID, content, and storyid.{"id": `commentId`,"storyid": `storyid`,"user": "alice","content": "I think this is a great story!"}
{"write": "doc.roles[auth.uid] === 'owner' ","read": "doc.roles[auth.uid] in ['owner', 'writer']"}
{"read": true,"write": "get(`database.roles.${doc.id}`).roles[auth.uid] in ['owner', 'writer'] "}
{"read": true,"create": true,"update": "doc.user == auth.uid","delete": "doc.user == auth.uid "}
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback