本文介绍了Firebase存储用于uid列表的自定义元数据安全规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个消息iOS应用程序,用户可以将相同的消息发送给多个人。该消息保存在Firebase存储中。我只想让已经发送消息的用户能够从存储中读取它。我已经在我的firebase数据库中实现了这个规则结构。

为了实现这个存储,我在消息文件customMetadata中添加一个uid列表,包括一个fromUid键对于编写消息的人来说。我在我的iOS应用程序中执行以下操作:

$ $ p $ $ $ $ $ $ $ var元数据值= [String:String]()
$ b $在friendSelected.keys {
metadataValues.updateValue(friendUid,forKey:friendUid)//我如何在我的安全规则中访问这些值
}
metadataValues.updateValue(senderUid,forKey :fromUid)//如何在安全规则中访问它
let messageMetadata = FIRStorageMetadata()
messageMetadata.customMetadata = metadataValues

这是我对Firebase存储中消息节点的读写安全规则的尝试,但它不起作用,文档也无济于事。 b
$ b

  match / messages / {messageId} {

允许阅读:if request.auth.uid == resource.metadata。 request.auth.uid; //我希望所有的朋友uid能够读取文件
允许写入:if request.auth.uid == resource.metadata.fromUid; //只有创建消息的人可以访问它


code


$ b

作为旁注,我假设没有限制我添加到customMetadata的密钥数量是多少?

解决方案

所以我设法解决了这个问题。我遇到的问题是,我不熟悉JavaScript,所以我得到的语法错误。在查看Javascript后,我很快就能解决这个问题。



希望我的回答可以帮助其他人,但是如果您处于类似的位置,我会推荐学习Javascript的基础知识。


$ b $另外值得一提的是,要访问文件元数据,在执行写入之前使用request.resource.metadata,在上传之后使用resource.metadata来访问。

  match / messages / {messageId} {

允许写入:if request.resource.metadata ['fromUid'] == request.auth.uid;
允许读取:if resource.metadata [request.auth.uid] == request.auth.uid;
}


I'm developing a messaging iOS app, where a user can send the same message to multiple people. The message is saved in firebase storage. I want to only enable the users who have been sent the message to be able to read it from storage. I am already implementing this rule structure in my firebase database.

In order to implement this for storage I add a list of uids to the message file customMetadata, including a fromUid key for the person who composed the message. I do the following in my iOS app:

    var metadataValues = [String:String]()

    for friendUid in friendsSelected.keys {
        metadataValues.updateValue(friendUid, forKey: friendUid) // how do I access these values in my security rules
    }
    metadataValues.updateValue(senderUid, forKey: "fromUid") // how do I access this in security rules 
    let messageMetadata = FIRStorageMetadata()
    messageMetadata.customMetadata = metadataValues

This was my attempt for my read and write security rules for the messages node in Firebase Storage but it doesn't work and the documentation is of no help.

    match /messages/{messageId} {

        allow read: if request.auth.uid == resource.metadata.request.auth.uid; // I want all friend uids to be able to read file
        allow write: if request.auth.uid == resource.metadata.fromUid; // only the person who create the message can access it

}

My attempt doesn't work. How do I access the customMetadata variable with the keys 'fromUid' and the friends uids 'request.auth.uid' in my security rules??

As a sidenote, I assume there is no limit on how many keys I add to customMetadata?

解决方案

So I have managed to solve it. The problem I had was that I wasn't familiar with Javascript and so I was getting the syntax wrong. After looking into Javascript I was soon able to work it out.

Hope my answer helps anyone else but would recommend learning the basics of Javascript if you are in a similar position to me.

It's also worth noting that to access the file metadata you use request.resource.metadata before the write is executed and resource.metadata to access once uploaded.

    match /messages/{messageId} {

        allow write: if request.resource.metadata['fromUid'] == request.auth.uid; 
        allow read: if resource.metadata[request.auth.uid] == request.auth.uid; 
    } 

这篇关于Firebase存储用于uid列表的自定义元数据安全规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 08:29