【问题标题】:Allow update only from uid in the document (firestore security rules)?只允许从文档中的 uid 更新(firestore 安全规则)?
【发布时间】:2021-07-28 16:42:34
【问题描述】:

假设我有一个“报价”文档,其中保存了原始创建者的 uid 作为一个名为“uid”的字段。

如何修改以下规则以仅允许 request.auth.uid 与文档字段中匹配的用户更新文档?

这是我现在的规则

function signedInOrPublic() {
  return request.auth.uid != null;     
}     

match /offer/{offerDcoument} {       
  allow read: if request.auth.uid != null 
  allow create: if signedInOrPublic() 
  allow update: <ONLY ALLOW IF THE UID FIELD MATCHES request.auth.uid CONDITION>
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    您可以使用resource 对象访问该字段:

    match /offer/{offerDcoument} {       
      allow read: if request.auth.uid != null 
      allow create: if signedInOrPublic() 
      allow update: if request.auth != null && resource.data.uid == request.auth.uid;
                                                          // ^^^ fieldname
    }
    
    

    【讨论】:

    • 像魅力一样工作??
    猜你喜欢
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-26
    • 1970-01-01
    相关资源
    最近更新 更多