【问题标题】:Firebase database rules for group-shared data群组共享数据的 Firebase 数据库规则
【发布时间】:2019-11-03 07:46:48
【问题描述】:

我的 webapp 和 99% 的应用程序一样,在 db 中有一个用户集合。我可以在 /user/ 访问用户,因此我设置规则以允许每个用户仅访问其数据。

但我需要每个用户都加入一个群组

/user/34029380432
  - name: realtebo
  - group_id: 123
  - isAdmin: true


/group/123
   - a lot of shared data, all members can erad, only isAdmin can write

如何设置一个规则,只允许每个组的成员读取它,并且只允许管理员成员写入组数据?!

当然,我可以交换边,movin 成员列表,作为数组,在每个组和设置下,在每个组内,一个或多个管理员 ID。

但我无法编写 javascript 规则。

【问题讨论】:

  • 假设您使用的是实时数据库,请使用数据库的 JSON 更新您的问题(作为文本,没有屏幕截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。然后还添加您想要允许或想要拒绝的读/写操作的代码。最后,请附上您已经编写的一些规则。

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


【解决方案1】:

我认为您可以做的是在您的组对象中拥有一个消息和用户节点。您可以根据安全规则访问该数据节点的父对象,并通过使用 exists() 查看该用户是否是用户对象节点的成员

/group/123
   - messages // a lot of shared data, all members can read, only isAdmin can write
   - users // list of user uids that are added to this group
   - admins // list of admin uids

"group": {
  "$groupId": {
    "messages": {
      ".read": "data.parent().child('users').child(auth.uid).exists()",
      ".write": "data.parent().child('admins').child(auth.uid).exists()"
    }
  }
}

【讨论】:

    猜你喜欢
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    相关资源
    最近更新 更多