为了账号安全,请及时绑定邮箱和手机立即绑定

Firebase 安全规则未显示任何数据

Firebase 安全规则未显示任何数据

慕田峪9158850 2022-12-09 15:38:46
我正在努力保护我的 Firebase。我做了这样的事情来保护我的用户列表。rules_version = '2';service cloud.firestore {match /databases/{database}/documents {     match /about/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /avantages/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /blog/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /customers/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /lowersection/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /middlesection/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }match /topsection/{id} {        allow read: if true;        allow write: if request.auth.uid != null;    }    match /users/{userId} {allow read: if isOwner(userId);allow write: if isOwner(userId);}}function isOwner(userId) {return request.auth.uid == userId; }}但是一旦我这样做了,我的网站上就没有数据显示了。目标是让每个人都可以读取数据(用户集合除外),但数据只能由登录用户写入。根据要求,下面是来自 Angular 应用程序的代码,它允许它获取数据并发回更新后的数据。getTopSectionMain() {return   this.firestore.collection('topsection').doc('content').collection('main').snaps hotChanges();}updateTopSectionMain(dataID: any, data: any) {this.firestore.doc('topsection/content/main/' + dataID).update(data);}这是来自我的内容服务。任何帮助,将不胜感激。
查看完整描述

1 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

您的问题似乎来自您阅读sub-collections 中的文档这一事实。


让我们以集合为例topsection。您有如下安全规则:


match /topsection/{id} {

    allow read: if true;

    allow write: if request.auth.uid != null;

}

但是您查询文档如下:


getTopSectionMain() {

    return   this.firestore.collection('topsection').doc('content').collection('main').snaps hotChanges();

}

这意味着您查询集合中文档的main子集合的content文档topsection。


如文档中所述:“安全规则仅适用于匹配的路径,因此在(父)集合上定义的访问控制不适用于(子)子集合。”


您需要通过添加递归通配符来调整规则

match /topsection/{document=**} {

        allow read: if true;

        allow write: if request.auth.uid != null;

}

或指定每个子集合,如:


match /topsection/{id} {

    match /main/{id} {

        allow read: if true;

        allow write: if request.auth.uid != null;

    }

}

由于您没有使用通配符来声明顶级集合的安全规则,因此您最好使用第二个选项。


查看完整回答
反对 回复 2022-12-09
  • 1 回答
  • 0 关注
  • 140 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号