【问题标题】:Firestore Firebase Database Rule, enabling admin to writeFirestore Firebase 数据库规则,允许管理员编写
【发布时间】:2021-06-24 11:48:41
【问题描述】:

大家好,我需要帮助以使用户能够阅读所有内容但不能写作。并允许管理员读写所有内容。

我有带有文档的用户集合,每个文档都有角色,如果它是管理员帐户,它会说角色 = 'admin' 否则它会说'常规'

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if true;
      allow write: if get(/databases/users/documents/users/$(request.auth.uid)).data.role == 'admin';
    }
  }
}

我写了这个,但它没有解决任何问题

---------更新----------

这对我来说是一个修复,谢谢。

get(/databases/$(database)/documents/users/$(request.auth.uid))

但我仍然无法从客户端做任何事情。 我用游乐场模式检查了控制台,规则实际上很好,如果它的管理员 uid 则返回 true。我猜这是客户端问题,因为它可能不读取 uid。知道如何解决吗?

【问题讨论】:

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


    【解决方案1】:

    这对我有用。 (Official Firestore Security Docs)

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
        match /{document=**} {
          allow read: if true;
          allow write: if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.role == 'admin';
        }
      }
    }
    

    【讨论】:

    • 由于某种原因它仍然不适合我,我早些时候尝试过。你知道还有什么问题吗?阅读很好,但即使是管理员也不会写。
    【解决方案2】:

    您的规则中有错字:

    get(/databases/users/documents/users/$(request.auth.uid))
    

    应该是

    get(/databases/$(database)/documents/users/$(request.auth.uid))
    

    【讨论】:

    • 仍然无法为我工作,知道可能是什么问题吗?
    • @MilanNesovic 您要执行的查询/写入是什么?请注意,根据您当前的规则,用户无法编写自己的用户数据。
    猜你喜欢
    • 1970-01-01
    • 2020-11-22
    • 2017-06-05
    • 2018-12-26
    • 2020-02-24
    • 2021-08-26
    • 2021-02-11
    • 2020-12-24
    • 1970-01-01
    相关资源
    最近更新 更多