R S*_*R S 3 javascript firebase-security google-cloud-firestore
我正在运行以下 Firestore 事务以在accounts集合中创建一个新文档并将其新创建的 ID 添加到用户的accounts. (相反,members发送到帐户的对象包含用户 ID。)
db.runTransaction(transaction => {
return transaction.get(db.collection('accounts').doc())
.then(res => {
const accountId = res.id;
transaction.set(db.collection('accounts').doc(accountId), {name, numberOfEmployees, businessTypes, industry, members})
transaction.set(db.collection('users').doc(uid), {accounts: {[accountId]: true}}, {merge: true})
return accountId;
}, error => error)
})
Run Code Online (Sandbox Code Playgroud)
.collection('accounts').doc()我正在使用没有路径返回新文档的事实,但我遇到了Missing or insufficient permissions.错误。我的规则如下:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
match /accounts/{accountId} {
// allow read if user is a member or if the document does not exist
allow read : if resource.data.members[request.auth.uid] == true || !exists(/databases/{database}/documents/accounts/$(accountId));
// allow update, delete if user is a member
allow update, delete: if resource.data.members[request.auth.uid] == true;
// allow creation of any authenticated user
allow create: if request.auth != null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经将其范围缩小到了read规则/accounts/{accountId}(如果我将其设置为if true;,则它有效)。我应该将其设置为什么以允许访问尚不“存在”的文档?
您可以在安全规则中使用getAfter()来获取文档状态,因为它将在事务结束时(但在提交之前)。
因此resource == null,您可以使用以下命令来代替 ,这对于阅读规则的人来说可能会很奇怪:
allow read, update, delete: if request.auth != null && getAfter(/databases/$(database)/documents/accounts/$(accountId)).data.members[request.auth.uid] == true;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1095 次 |
| 最近记录: |