【问题标题】:How to check if users owns the object in Firebase bolt compiler如何检查用户是否拥有 Firebase 螺栓编译器中的对象
【发布时间】:2016-05-30 08:17:16
【问题描述】:

假设我有一个包含两个集合用户和帖子的简单架构。每个帖子对象都有一个键值对(ownerId:userId)来找出哪些用户拥有帖子对象。

users/{1,2,3...}
posts/{a,b,c...}/ownerId:userId

我正在尝试编写用户只能读/写他的用户数据和他的帖子的规则。

为此,用户的螺栓规则非常简单:

isUser(uid) = auth != null && auth.uid == uid;
path /users/$uid {
    read() = isUser($uid);
    write() = isUser($uid);
}

我的问题是如何确保帖子集合只能由用户访问。我可以在规则中检查帖子集合的 ownderId 属性吗?如果是这样,如何,如果不是,我该如何构建我的架构来做到这一点?

编辑

我正在尝试像这样保护帖子路径:

path /posts/$pid {
  read() = isUser(this.ownerId);
  write() = isUser(this.ownerId);
}

这是正确的做法吗?

【问题讨论】:

    标签: firebase firebase-security firebase-realtime-database


    【解决方案1】:

    我们可以通过向集合中添加所有者 ID 属性然后检查用户是否通过身份验证来做到这一点。

    path /posts/$pid {
      read() = isUser(this.ownerId);
      write() = isUser(this.ownerId);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2015-10-23
      • 2019-05-07
      相关资源
      最近更新 更多