firebase - 如何匹配 Flutter Firestore 查询和 Firestore 安全规则?
问题描述
我从他们的主页上找到了以下 Firestore 示例。设置规则的代码对我来说很有意义,但我不知道如何在我的 Flutter Firestore 查询中实现这个逻辑。谁能帮我 ?
https://firebase.google.com/docs/firestore/solutions/role-based-access
service cloud.firestore {
match /databases/{database}/documents {
match /stories/{story} {
function isSignedIn() {
return request.auth != null;
}
function getRole(rsc) {
return rsc.data.roles[request.auth.uid];
}
function isOneOfRoles(rsc, array) {
return isSignedIn() && (getRole(rsc) in array);
}
function isValidNewStory() {
return request.resource.data.roles[request.auth.uid] == 'owner';
}
function onlyContentChanged() {
// Ensure that title and roles are unchanged and that no new
// fields are added to the document.
return request.resource.data.title == resource.data.title
&& request.resource.data.roles == resource.data.roles
&& request.resource.data.keys() == resource.data.keys();
}
// Split writing into creation, deletion, and updating. Only an
// owner can create or delete a story but a writer can update
// story content.
allow create: if isValidNewStory();
allow delete: if isOneOfRoles(resource, ['owner']);
allow update: if isOneOfRoles(resource, ['owner'])
|| (isOneOfRoles(resource, ['writer']) && onlyContentChanged());
allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);
match /comments/{comment} {
allow read: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter', 'reader']);
allow create: if isOneOfRoles(get(/databases/$(database)/documents/stories/$(story)),
['owner', 'writer', 'commenter'])
&& request.resource.data.user == request.auth.uid;
}
}
}
}
解决方案
推荐阅读
- json - 如何将 prop 的值存储在变量中,然后在反应中访问它?
- google-chrome-extension - Chrome 扩展中的 Bootrstrap 4 模态
- javascript - 如果 AJAX 请求失败,如何将列表顺序恢复到上次更改之前的状态?
- angular - 使用清晰度设计的自定义验证不起作用
- linux - Linux Alpine:xfce4 和不可满足的约束
- office-js - 在由 Outlook REST API 更新后刷新当前电子邮件项目视图
- git - 我怎样才能退出我所做但尚未在我的 GITHUB 分支中提交的一些更改
- python - 如何获得分组大小的百分比
- node.js - 有没有办法限制nodejs电子应用程序只能访问某些网址
- java - Spring JPA - 序列缓存给出了意想不到的行为。使用分配大小 = 1 是可以的