Smo*_*oth 4 firebase firebase-security google-cloud-firestore
我在我的 firebase 数据库中添加安全规则时遇到问题。如果经过身份验证的用户等于投票文档上的 uid 属性,我只想允许删除投票。除了我在匹配 /votes/{voteId} 上创建的删除规则之外,一切正常。
我试图用 resource.data.uid 来做到这一点,但模拟器抱怨,我收到错误“运行模拟时出错 - 错误:simulator.rules Null 值错误”
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
}
}
Run Code Online (Sandbox Code Playgroud)
更新
我也尝试使用 /{document=**} 通配符,它给了我同样的空值错误
match /votes/{document=**} {
allow read;
allow create: if isSignedIn();
allow delete: if request.auth.uid == resource.data.uid;
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用 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
}
Run Code Online (Sandbox Code Playgroud)
您是否创建了一个名为 字段的文档uid?像这样。
firebase.firestore().collections("votes").add({uid: firebase.auth().currentUser.uid});
resource 是一个firestore文件。
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
}
...
Run Code Online (Sandbox Code Playgroud)
看:
| 归档时间: |
|
| 查看次数: |
1188 次 |
| 最近记录: |