【问题标题】:Firebase data structure & securityFirebase 数据结构和安全性
【发布时间】:2014-11-10 21:37:02
【问题描述】:

所以我对使用 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"
        }
    }
}

【问题讨论】:

标签: database-design firebase firebase-security


【解决方案1】:

您需要将数据非规范化为多个“表”,这些表以某种方式相互“链接”。例如:每个用户都会得到一个 client_ids 列表,每个客户端都会得到一个 user_ids 列表(假设你要以两种方式访问​​数据)。需要保持同步的数据更多,但是一旦它是这样的扁平结构,您也会发现设置权限会容易得多。

你现在拥有的这种嵌套结构对于像 Firebase 这样的 NoSQL 后端来说效率极低,因为当你为用户获取数据时,你必须检索所有这些数据,而且你没有办法告诉你。哪些客户端属于哪个用户,而不需要遍历所有客户端。

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

【讨论】:

    猜你喜欢
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-03
    • 2013-05-14
    • 2018-09-21
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多