如何构建Firebase数据和用户之间共享数据的规则? [英] How to structure Firebase data and rules for sharing data between users?

查看:120
本文介绍了如何构建Firebase数据和用户之间共享数据的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个简单的待办事项应用程序,用户可以创建一个列表,然后勾选项目完成。数据结构如下:'/ acc_'+ USER_ID +'/ done'和'/ acc_'+ USER_ID +'/ todo'。当项目被完成时,它们被移动到完成列表中。

我希望用户能够与另一个已认证的用户共享列表。



这些规则目前看起来像下面的样子。

  {
rules:{
$ userPath:{
.read:auth!= null,
.write:auth!= null,
.validate:$ userPath ==='acc_'+ auth.id
}
}
}

或者我可以在根目录下创建一个列表,只有创建者和共享用户可以访问它。



任何指导?

解决方案

由于用户决定与谁共享列表,我会将这些信息存储在用户数据本身。例如:
$ b

{
rules:{
$ userPath:{
.write:$ userPath =='acc_'+ auth.id,
.read:$ userPath =='acc_'+ auth.id || hasChild(auth.id)

}
}

然后将共享数据的用户列表存储在 acc_userid / shared /


Currently I have a simple todo app whereby users can create a list then tick off items as done. The data is structured as follows: '/acc_' + USER_ID +'/done' and '/acc_' + USER_ID +'/todo'. As items are "done" they are moved to the done list.

What I'd like is for a user to be able to share a list with another authenticated user.

The rules currently look like the following which seem to work.

{
  "rules": {
        "$userPath": {
            ".read": "auth != null",
            ".write": "auth != null",
            ".validate": "$userPath === 'acc_' + auth.id"
        }
    }
}

What I think I need to do is have each user having an array of approved email addresses have somehow hook that into the security rules.

Alternatively I could make a list in the root and have only the creator and shared users access it.

Any guidance?

解决方案

Since the user decides who'd they like to share the list with, I'd store that information in the user data itself. For example:

{
  "rules": {
    "$userPath": {
      ".write": "$userPath == 'acc_' + auth.id",
      ".read": "$userPath == 'acc_' + auth.id || root.child($userPath).child('shared').hasChild(auth.id)"
    }
  }
}

And then store the list of users the data is shared with in acc_userid/shared/

这篇关于如何构建Firebase数据和用户之间共享数据的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆