Edw*_*ard 6 firebase firebase-security firebase-storage
我正在开发一个消息传递iOS应用程序,用户可以向多个人发送相同的消息.该消息保存在firebase存储中.我想只启用已发送邮件的用户能够从存储中读取它.我已经在我的firebase数据库中实现了这个规则结构.
为了实现存储,我将iid列表添加到消息文件customMetadata中,包括组成消息的人员的fromUid键.我在iOS应用程序中执行以下操作:
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
Run Code Online (Sandbox Code Playgroud)
这是我尝试为Firebase存储中的消息节点读取和写入安全规则,但它不起作用,文档没有任何帮助.
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
}
Run Code Online (Sandbox Code Playgroud)
我的尝试不起作用.如何在我的安全规则中使用密钥'fromUid'和朋友uids'request.auth.uid'访问customMetadata变量?
作为旁注,我假设我添加到customMetadata的密钥数量没有限制?
所以我设法解决了。我遇到的问题是我不熟悉Javascript,因此我弄错了语法。研究了Javascript之后,我很快就能解决了。
希望我的回答对其他人有帮助,但如果您的身份与我相似,建议您学习Java语言的基础知识。
还值得注意的是,在执行写入操作之前,您可以使用request.resource.metadata访问文件元数据,并在上传后使用resource.metadata访问文件元数据。
match /messages/{messageId} {
allow write: if request.resource.metadata['fromUid'] == request.auth.uid;
allow read: if resource.metadata[request.auth.uid] == request.auth.uid;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1131 次 |
| 最近记录: |