Firebase如何为树中深度为2级随机id的子项编写安全规则

时间:2018-03-10 23:56:45

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

我有一个名为(people)的数据库节点,如下所示:

    people
    |
    |
    -------UserID1 //which is a random id
    |       |
    |       |
    |        ----UserId2 //which is a random id
    |            |
    |            |
    |            name:"some_name"
    |            id:"UserId2"
    |            image:"image_url" 
    |
    | 
    |
    -------UserId2
            |
            |
             ----UserId3
                 |
                 |
                 name:"some_name"
                 id:"UserId3"
                 image:"image_url"  

如果我们查看(people / UserID1 / UserId2)节点

由于UserId1和UserId2是2个随机ID,如果我们要将规则写入UserId2,我们会注意到它是2个随机id级别。

我想要的是在这条指定路径上写一条规则:

1)people / UserId1:可以由(UserID1)和(UserId2)编写。

2)people / UserId1:可以通过(UserID1)和(UserId2)读取。

3)people / UserId1 / UserId2:必须以一个具有(name,id,image)的newData结束。

我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:1)

由于Firebase实时数据库将cascade规则化为更深的密钥,因此不建议允许 rasDialFileName = Path.Combine(WinDir, "rasdial.exe"); try { string args = $"{connectionName} {userName} {passWord}"; ProcessStartInfo myProcess = new ProcessStartInfo(rasDialFileName, args); myProcess.CreateNoWindow = true; myProcess.UseShellExecute = false; Process.Start(myProcess); } catch (Exception Ex) { Debug.Assert(false, Ex.ToString()); } 写入people/UserId1,因为这将允许UserId2进行写访问存储在UserId2下的其他用户的数据,例如people/UserId1

但是,利用此特征,我们可以在数据结构更深入的情况下“添加”具有读写权限的用户。

所以新的条件是:

  • people/UserId1/UserId3-UserId1具有读写访问权限
  • people/UserId1-UserId2具有读写访问权限
  • people/UserId1/UserId2-必须始终包含“名称”,“ id”和“图像”键
  • people/UserId1/UserId2-UserId2无法读取/写入
people/UserId1/UserId3

最后,如果还要求{ "rules": { "people": { "$userId1": { "$userId2": { ".read": "auth.uid == $userId2", // add $userId2 to those granted read permission, cascades into deeper keys ".write": "auth.uid == $userId2", // add $userId2 to those granted write permission, cascades into deeper keys ".validate": "newData.hasChildren(['name', 'id', 'image'])" // any new data must have 'name', 'id' and 'image' fields. }, ".read": "auth.uid == $userId1", // add $userId1 to those granted read permission, cascades into deeper keys ".write": "auth.uid == $userId1" // add $userId1 to those granted write permission, cascades into deeper keys } } } 等于people/UserId1/UserId2/id,则可以更改UserId2规则以强制执行此操作:

".validate"