Firebase数据结构&安全

时间:2014-09-17 00:45:42

标签: database-design firebase firebase-security

所以我使用Firebase相对较新,我试图找出如何构建我的数据以使其安全和规范化。

我的数据需要基于用户的安全性,在概念上看起来像这样:

{
    "users": {
        "simplelogin:1": {
            "email": "test@test.com",
            "id": "1",
            "name": "test",
            "provider": "password",
            "clients": {
                "client1": {
                    "name": "testClient",
                    "projects": {
                        "project1": {
                            "name": "testProject",
                            "sites": {
                                "site1": {
                                    "name": "testWobsite",
                                    "hits": {
                                        "hit1": {},
                                        "hit2": {},
                                        "hit3": {}
                                    }
                                },
                                "site2": {}
                            }
                        },
                        "project2": {}
                    }
                },
                "client2": {
                    "name": "test",
                    "projects": {}
                },
            }
        },
        "simplelogin:2": {
            "email": "test2@test2.com",
            "id": "2",
            "name": "test2",
            "provider": "password",
            "clients": {}
        }
    }
}

如您所见,此处的数据结构非常嵌套...
用户有客户,客户有项目,项目有网站,网站有点击...
所以这是我的主要问题之一 - 我不完全确定如何构造这些数据,以便它不是那么嵌套。

我遇到的另一个问题是弄清楚如何使用Firebase安全规则 本质上,我希望用户能够创建,更新和删除他们拥有的所有数据(客户,项目,站点和点击)
命中应该是可公开写入的,但只能通过拥有它的用户读取 用户应该能够注册和登录,但不能读取或写入任何其他人的数据。

如果有人对此或任何提示或指示有任何想法,任何建议将不胜感激! 谢谢!

修改

这是我尝试对数据进行规范化......任何想法......?

{
    "users": {
        "simplelogin:1": {
            "email": "test@test.com",
            "id": "1",
            "name": "test",
            "provider": "password",
            "clients": {
                "testClient": "client1",
                "test": "client2"
            }
        },
        "simplelogin:2": {
            "email": "test2@test2.com",
            "id": "2",
            "name": "test2",
            "provider": "password",
            "clients": {}
        }
    },
    "clients": {
        "client1": {
            "owner": "simplelogin:1",
            "parent": "",
            "name": "testClient",
            "projects": {
                "testProject": "project1",
                "testProject_2": "project2"
            }
        },
        "client2": {
            "owner": "simplelogin:1",
            "parent": "",
            "name": "test",
            "projects": {}
        }
    },
    "projects": {
        "project1": {
            "owner": "simplelogin:1",
            "parent": "client1",
            "name": "testProject",
            "sites": {
                "testWebsite": "site1",
                "testWebsite2": "site2"
            }
        },
        "project2": {
            "owner": "simplelogin:1",
            "parent": "client1",
            "name": "testProject_2",
            "sites": {}
        }
    },
    "sites": {
        "site1": {
            "owner": "simplelogin:1",
            "parent": "project1",
            "name": "testWebsite",
            "hits": {
                "firstHit": "hit1",
                "secondHit": "hit2",
                "thirdHit": "hit3"
            }
        },
        "site2": {
            "owner": "simplelogin:1",
            "parent": "project1",
            "name": "testWebsite2",
            "hits": {}
        }
    },
    "hits": {
        "hit1": {
            "owner": "simplelogin:1",
            "parent": "site1",
            "name": "firstHit"
        },
        "hit2": {
            "owner": "simplelogin:1",
            "parent": "site1",
            "name": "secondHit"
        },
        "hit3": {
            "owner": "simplelogin:1",
            "parent": "site1",
            "name": "thirdHit"
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您需要将数据非规范化为多个表格,其中表格为#34; link"以某种方式相互对待。例如:每个用户都会得到一个client_id列表,每个客户端都会获得一个user_id列表(假设您将以两种方式访问​​数据)。这是更多数据保持同步,但一旦它是一个扁平结构,你也会发现它将更容易设置你的权限。

现在使用这种嵌套结构对于像Firebase这样的NoSQL后端是非常低效的,因为你必须在获取用户数据时检索所有数据,而你无法告诉他们哪些客户端属于哪个用户而没有遍历所有用户。

请务必阅读https://www.firebase.com/blog/2013-04-12-denormalizing-is-normal.html