【问题标题】:How do I let a subfolder be written to EXACTLY one time in firebase?如何让一个子文件夹在firebase中被写入一次?
【发布时间】:2016-06-03 11:50:42
【问题描述】:

要将组创建者与组成员链接在一起,我希望组创建者提交一个连接字符串,然后组成员可以使用该连接字符串加入组。

然而,一旦创建了连接字符串,我不希望除了创建者之外的任何人删除或覆盖字符串或字符串下的数据。

在我的 javascript 中,我可以轻松地编写一个检查函数来查看它是否存在:

function tryToAddConnectionString(uid, desiredConnectionString)
{
    firebase.database().ref('/connectionStrings/' + desiredConnectionString).once('value').then(function(snapshot){
         if(snapshot.val()==null)
         {
            //There is no data therefore there is no connection string, so create one.
            firebase.database().ref('connectionStrings/' + desiredConnectionString).set({
              owner: uid });
         }
         else
         {
           //Connection String already exists. Throw "already exists" message
         }
     });
}

但这并不能阻止恶意黑客进入他/她的控制台并输入

firebase.database().ref('connectionStrings/' + desiredConnectionString).set(null);

这将删除/接管该组。

我可以设置一个规则来解决这个问题吗?另外,在

'/connectionStrings/'+desiredConnectionString+'members'

我希望人们能够将自己添加为成员。

回顾一下:我正在寻找一个允许任何人创建连接字符串的规则,但只有创建者可以删除连接字符串或更改所有者子文件夹,但任何人都可以读/写成员文件夹。

提前致谢。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    您可以使用安全规则轻松做到这一点:

    //add a condition to verify that the node doesn't exist or the
    //You must add to your node the aid of the creator
    ".write":"!data.exist() || auth.uid === data.child('creator')"
    

    您可以在此处查看您还可以配置什么 Security Rules Firebase

    【讨论】:

    • 如果你把你的数据库结构放在这会有帮助
    • 谢谢,这正是我正在寻找的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-03
    • 1970-01-01
    • 2020-06-05
    • 2019-11-24
    • 2019-11-24
    相关资源
    最近更新 更多