【问题标题】:Firebase Rules: Problem with role based authentication in groups and .where()Firebase 规则:组和 .where() 中基于角色的身份验证存在问题
【发布时间】:2019-07-22 10:27:33
【问题描述】:

我正在尝试保护用户可以管理其乐队的应用。用户可以是多个乐队的一部分,因此可以根据他们所在的乐队拥有不同的角色。角色存储在乐队文档的地图字段中。

我目前有以下 firebase/firestore 规则设置:

service cloud.firestore {
match /databases/{database}/documents {
    function isSignedIn() {
        return request.auth != null;
    }

    match /bands/{bandID}{
        function isBandAdmin(userID){ 
            // Determine if user is a band admin
            return get(/databases/$(database)/documents/bands/$(bandID)).data.members[userID].role == 'Admin';
        }

        allow read, update, create: if isSignedIn() && isBandAdmin(request.auth.uid);

        // Rules for a band's concerts
        match /events/{event}{
            allow read, update, create: if isSignedIn() && (isBandAdmin(request.auth.uid));
        }
    }
}

现在,如果我在 firebase 控制台提供的模拟器中执行 get 请求,这将完美运行。但在我的项目中,我正在尝试执行以下请求:

database.collection("bands").where("name" ,"==", "AC/DC").get().then((docs) => {...});

这给了我一个权限错误。我怎样才能做到这一点?

PS:我已经发布了一个类似的问题,但这是一个重要的更新。

【问题讨论】:

  • 用户似乎没有登录,或者不是管理员。鉴于您提供的信息,很难说它是哪一个。要确定用户是否已登录,请在运行查询之前登录 firebase.auth().currentUser
  • 这不是问题所在。用户已登录,也是管理员。下面的答案是正确的。

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


【解决方案1】:

您似乎希望安全规则应该过滤查询中与某些用户角色匹配的文档。这是行不通的,因为security rules are not filters。请仔细阅读链接文档。

当您对多个文档执行查询时,安全规则应该绝对允许获取所有这些文档。它不会单独查看每个文档以确定是否允许阅读。如果规则系统确定可能不允许任何一个文档,则整个查询将失败。

【讨论】:

  • 好的!我期待这样的答案。非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2019-04-04
  • 2020-07-14
  • 2018-07-12
  • 2021-09-28
  • 2016-01-03
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
相关资源
最近更新 更多