【问题标题】:Firebase Cloud Database Rules - how to retrieve data only where the user id matches a key in the object?Firebase 云数据库规则 - 如何仅在用户 id 与对象中的键匹配的情况下检索数据?
【发布时间】:2020-09-09 00:18:44
【问题描述】:

有没有办法使用 firebase 规则来检索具有特定“userID”值的所有项目。我尝试阅读所有规则并提出了这条规则,但它们不起作用: 我只想阅读数据库中与 auth.uid == userID 匹配的文档。

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {

    // Allow public read access, but only content owners can write
    match /debtors/{userID}/{documents=**} {

      allow read: if request.auth.uid != null && request.auth.uid == userID;
      allow create: if request.auth.uid != null;
      allow update, delete: if request.auth.uid != null && request.auth.uid == userID;;
    }
  }
}

我在集合中有以下对象:

{firstname: "Aacis"
relation: "friend"
userID: "7ScK2T0T3SMUR1NJxjiCiRzTnm62"}

在flutter中获取流:

stream: Firestore.instance
            .collection("debtors")
            .where("userID", isEqualTo: user.uid)
            .snapshots()

【问题讨论】:

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


    【解决方案1】:

    您是否将auth.uid 设置为文档ID,并将对象键命名为userID

    Firestore.instance.collection("debtors").doc("7ScK2T0T3SMUR1NJxjiCiRzTnm62").set({
        firstname: "Aacis",
        relation: "friend",
        userID: "7ScK2T0T3SMUR1NJxjiCiRzTnm62"
    })
    

    如果您只想允许 onwer 读取访问权限,则应设置allow read: if request.auth.uid != null && request.auth.uid == userID;

    如果你想读取匹配 auth.uid == resource.data.userID 的文档,那么你应该设置allow read: if request.auth.uid != null && request.auth.uid = resource.data.userID;

    如果你想允许公众阅读,那么你应该设置allow read: if true;

    此外,读取规则可以分为 get 和 list。

    service cloud.firestore {
      match /databases/{database}/documents {
        // A read rule can be divided into get and list rules
        match /cities/{city} {
          // Applies to single document read requests
          allow get: if <condition>;
    
          // Applies to queries and collection read requests
          allow list: if <condition>;
        }
    
        // A write rule can be divided into create, update, and delete rules
        match /cities/{city} {
          // Applies to writes to nonexistent documents
          allow create: if <condition>;
    
          // Applies to writes to existing documents
          allow update: if <condition>;
    
          // Applies to delete operations
          allow delete: if <condition>;
        }
      }
    }
    

    见:

    更新

    下面代码中的userID表示文档id为auth.uid,只允许onwer读取权限。

        match /debtors/{userID}/{documents=**} {
    
          allow read: if request.auth.uid != null && request.auth.uid == userID;
    

    所以你不要将auth.uid设置为文档id,并且文档中名为userID的存储值匹配auth.uid,你应该使用resource.data.userIDrequest.resource.data.userID

    请尝试以下代码。

    rules_version = '2';
    service cloud.firestore {
      match /databases/{database}/documents {
    
        match /debtors/{document=**} {
    
          allow read: if request.auth.uid != null && request.auth.uid == resource.data.userID;
          allow create: if request.auth.uid != null && request.auth.uid == request.resource.data.userID;
          allow update, delete: if request.auth.uid != null && request.auth.uid == resource.data.userID;
        }
      }
    }
    

    资源变量引用请求的文档,resource.data 是存储在文档中的所有字段和值的映射。有关资源变量的更多信息,请参阅参考文档。

    写入数据时,您可能希望将传入数据与现有数据进行比较。在这种情况下,如果您的规则集允许挂起写入,则 request.resource 变量包含文档的未来状态。对于只修改文档字段子集的更新操作,request.resource 变量将包含操作后的待处理文档状态。您可以检查 request.resource 中的字段值以防止不需要或不一致的数据更新:

    见:

    【讨论】:

    • 我没有将 auth.uid 设置为文档 ID,因为我需要多个文档。我只设置了 userID: as auth.uid
    猜你喜欢
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2018-05-29
    • 1970-01-01
    • 2020-07-09
    • 2017-12-14
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多