[英]Firestore Rules: Making documents in a collection and all subcollections follow the same rule
[英]How do I write the follow rule in firestore?
我想弄清楚我应该如何编写我的 firestore 规则,以便Only the matching uid in the key of the document's key-value pair
才能具有写入权限。
进一步来说:
我的 firestore 结构是这样的:
'Items LA' -> 'Pasadena' -> {'awjij53dHh3dnYAh': {itemInformation}}
collection -> document -> fields
其中'awjij53dHh3dnYAh'
是卖家 ID。
我想写一个规则,它允许awjij53dHh3dnYAh
在该字段中写入。
目前我有以下内容:
match /Items%20LA/{cityName}/{data}{
allow read: if request.auth != null;
allow write: if request.auth.uid == data.key; // This is where I am not sure how to write what I want to accomplish
}
谢谢
你想要的可能是
allow write: if request.auth.uid == $(data)
//or this
allow write: if request.auth.uid == resource.id
首先 - 规则的匹配路径仅包括文档的路径组件(集合和文档 ID),而不包括字段。 此外,它不需要任何 URL escaping。只需匹配“/Items LA/{cityName}”。
其次,如果你想在规则表达式中引用文档中的字段名称,你应该知道request.resource.data
是所有传入字段的Map,你将不得不使用它的API来处理那些键/值对。
第三,如果要找出文档的哪些字段发生了变化,则需要使用MapDiff API 比较更改前后文档的 state。
将所有这些放在一起,您将需要检查是否只有更改的字段等于用户的 UID:
match /Items LA/{cityName} {
allow write: if request.resource.data.diff(resource.data).affectedKeys().hasOnly([request.auth.uid]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.