【问题标题】:Firestore rules map for object对象的 Firestore 规则映射
【发布时间】:2018-04-04 02:47:43
【问题描述】:

我正在收藏。每个文档都有一个对象 reader,它将存储可以访问文档的人的 uid。可以是一个或多个可以访问文档的用户

我正在使用 Angular Firebase

constructor(private afAuth: AngularFireAuth, private afs: AngularFirestore) { 
    this.afAuth.authState.subscribe(auth => {
      this.users = afs.collection<User>('users', ref => ref.where('readers.' + auth.uid, '==', true)).valueChanges();
    });
  }

如果我使用规则允许所有阅读,我的页面将显示正确的条目

match /users/{userId=**} {
      allow read, write;
    }

如果我添加规则以使用 uid 进行过滤,则会收到错误缺失或权限不足。

match /users/{userId=**} {
      allow read, write: if resource.data.readers[request.auth.uid] == true ||
                        resource.readers[request.auth.uid] == true;
    }

感谢任何有关我在规则上做错了什么的帮助。 提前致谢。

【问题讨论】:

  • 我认为您需要像这样在if resource.data.readers.[request.auth.uid] == true 中添加.
  • 那将是一个语法错误。
  • 在他们使用$(request.auth.uid) 获取价值的文档中可以。试试if resource.data.readers[$(request.auth.uid)] == true
  • resource.data.readers[$(request.auth.uid)] 返回语法错误 梳理文档我看到它应该是resource.data.readers[(request.auth.uid)]。试过了,它也不起作用。 if resource.data.readers[(request.auth.uid)] == trueif resource.data.readers[(request.auth.uid)] != null
  • 看起来resource.readers[request.auth.uid] == true 正在工作

标签: firebase firebase-security google-cloud-firestore


【解决方案1】:

您是否尝试过将数据库规则的规则更改为 auth=null?

{
  "rules": {
    ".read": "auth == null",
    ".write": "auth == null"
  }
}

【讨论】:

  • 这不起作用,因为集合将有多个文档,每个文档将由不同的用户访问。
猜你喜欢
  • 2019-01-09
  • 1970-01-01
  • 2018-05-25
  • 2019-02-19
  • 2021-09-03
  • 1970-01-01
  • 2020-04-23
  • 2022-08-04
  • 1970-01-01
相关资源
最近更新 更多