如何让一个子文件夹在 firebase 中只写入一次?

How do I let a subfolder be written to EXACTLY one time in firebase?

为了 link 群组创建者和群组成员,我希望群组创建者提交一个连接字符串,然后群组成员可以使用该连接字符串加入群组。

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

在我的 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
         }
     });
}

但这并不能阻止恶意黑客进入 his/her 控制台并输入

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

哪个会delete/take超过群。

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

'/connectionStrings/'+desiredConnectionString+'members'

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

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

提前致谢。

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

//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