这是我写的;
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /reservedUsernames/{username} {
allow update: if false;
allow create: if request.auth != null;
}
}
}
我已经添加了一个 ID 为 sam
的文档和一个字段 userId
= 122
。如果我对该文档运行更新,请参阅下面的内容,它会成功!如何允许创建但不允许更新?
db.collection("reservedUsernames")
.document(searchableUsername)
.setData(["userId": userId])
最佳答案
我设法通过使用安全规则来做到这一点:
rules_version = '2'
service cloud.firestore {
match /databases/{database}/documents {
match /reservedUsernames/{documentId} {
allow create: if request.auth != null && existingData(resource) == null
}
function incomingData(request) {
return request == null || request.resource == null || request.resource.data == null ? null : request.resource.data
}
function existingData(resource) {
return resource == null ? null : resource.data
}
}
}
通过这种方式,我可以检查是否正在更新现有文档,并且只有在没有更新的情况下才会通过!
关于ios - Firestore 规则 - 如何允许创建但不允许更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73505778/