【问题标题】:Getting .read and .write Security Rules To Work For Groups让 .read 和 .write 安全规则适用于组
【发布时间】:2015-09-09 01:42:21
【问题描述】:

我很难为构建基于团队的协作平台编写安全规则。

  1. ​​当用户注册时,他们应该能够创建一个团队并邀请用户加入该团队。
  2. 项目应归团队所有。​
  3. ​只有该团队中的用户才能查看该项目。​
  4. ​用户只能看到他们所属的团队​。

我如何编写​.read​安全规则,以便用户只能看到他们所在团队的信息?

我应该只列出两个团队,因为我属于他们 github:8272012​.​

​当前的安全规则:​

{
    "rules": {
        ".read": true, 
        "users": {
          "$user": {
            //can add a message if authenticated
            ".write": "auth.uid === $user" 
          }
        }, 
        "teams": {
            "$team": {
                "users": {
                    // can write to the users list only if ADMINISTRATOR
                    "$user": {
                        ".write":"newData.parent().child(auth.uid).val() === 99"
                    }
                }
            }
        },
        "projects": {
          "$team": {
            "$project": { 
                //can add a message if they are a MEMBER
                ".write": "(!data.exists() && newData.exists() && root.child('teams/' + $team + '/users/' + auth.uid).val() >= 10)"
            }
          }
        }
    }
}

我应该只列出两个团队,因为我属于他们github:8272012

【问题讨论】:

标签: firebase angularfire


【解决方案1】:

以下安全规则将仅向项目团队中的用户授予项目的读写访问权限(假设您为每个用户添加一个 /projects 节点以指示该用户有权访问哪些项目):

"rules": {
  "projects": {
    "$project": {
      ".read": "root.child('users').child(auth.uid).child('projects').val().child($project).exists()" ,
      ".write": "root.child('users').child(auth.uid).child('projects').val().child($project).exists()" 
    }
  }
}

我看不到您为每个项目存储了哪些数据,但如果您存储对项目团队的引用,您也可以在安全规则中使用它。

【讨论】:

  • 我非常有信心您之前发布的要点是一个更好的数据结构,所以我正在为此修改我的应用程序,到目前为止一切都很好! :) 一旦我完全解决了这个问题,就可以关闭它。泰!
  • 我希望“关闭这个问题”是指“自己添加一个答案并接受”,对吧? :-)
  • @sara 你介意看看我更新的问题吗?我正在尝试弄清楚如何使用像您的示例这样的重组数据库来仅列出用户现在也列出的团队。
  • 很抱歉,我们整个周末都断电了。 :P
猜你喜欢
  • 2017-03-16
  • 1970-01-01
  • 2021-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 2019-06-30
相关资源
最近更新 更多