我在firebase数据库中添加安全规则时遇到问题。如果经过身份验证的用户等于投票文档中的uid属性,我想只允许删除投票。除了我在match / votes / {voteId}上创建的删除规则之外,一切正常。
我尝试用resource.data.uid做这个,但模拟器抱怨,我得到错误“错误运行模拟 - 错误:simulator.rules空值错误”
service cloud.firestore {
match /databases/{database}/documents {
match /polls/{pollId} {
allow read;
allow delete: if getUserData().roles.keys().hasAny(['admin']);
allow create: if isSignedIn();
}
match /users/{userId} {
allow read, write: if isOwner(userId);
}
match /votes/{voteId} {
allow read;
allow create: if isSignedIn();
allow delete: if request.auth.uid == resource.data.uid;
}
}
/// Functions ///
function isSignedIn() {
return request.auth != null
}
function isOwner(userId) {
return request.auth.uid == userId
}
}
更新
我也尝试使用/ {document = **}通配符,它给了我相同的Null值错误
match /votes/{document=**} {
allow read;
allow create: if isSignedIn();
allow delete: if request.auth.uid == resource.data.uid;
}
我也尝试使用get()函数但收到错误“找不到函数错误:名称:[get]
match /votes/{voteId} {
allow read;
allow create: if isSignedIn();
allow delete: if get(/databases/$(database)/documents/votes/$(voteId)).data.uid == request.auth.uid
}
您是否创建了一个名为uid
的文档?像这样。
firebase.firestore().collections("votes").add({uid: firebase.auth().currentUser.uid});
resource
是一个火灾文件。
resource.data
是文档数据的地图。
firebase控制台上的模拟器使用项目中存在的真实firestore数据。
而且我认为根据以下规则改变规则会更好。
...
match /votes/{voteId} {
allow read;
allow create: if isSignedIn() && request.auth.uid == request.resource.data.uid;
allow delete: if isSignedIn() && request.auth.uid == resource.data.uid;
}
...
function isSignedIn() {
return request.auth.uid != null
}
...
看到: