【问题标题】:Unable to access Cloud Firestore files for specific user无法访问特定用户的 Cloud Firestore 文件
【发布时间】:2017-11-04 13:06:44
【问题描述】:

我正在尝试使用 Cloud Forestore 来保存用户数据。我设置的规则是这样的:

service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userID} {
      allow read: if request.auth.uid == userID;

      match /{lists} {
        allow write: if request.auth.uid == userID;
      }
    }
    match /users/test {
        allow read, write: if true;
    }
  }
}

我试图完成的是用户应该能够阅读 {userID} 中的所有内容,包括子集合和子文档。此外,用户应该能够写入 {lists} 集合以及每个子集合和子文档。

我这样访问 Cloud Firestore:

db.collection("users").document(mAuth.getUid()).collection("lists").document(String.valueOf(document.getDocId()))
                    .set(cloudListInfo)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            error = 0;
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            error = -3;
                            globalVars.logException(TAG, "docInfo:ExportToCloud FAILURE", e);
                        }
                    });
        }

我得到的错误是这样的:

FAILURE:Exception.string:com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.

我可以访问“测试”集合及其值。

我使用允许写入/读取所有内容的规则为我的用户创建了一条记录,就像“测试”匹配中的规则一样。这是在我努力使用更详细的规则保护数据之前完成的。

我不明白为什么这不起作用。这很可能是微不足道的事情,但我几乎盲目地试图找到原因......

【问题讨论】:

    标签: android google-cloud-firestore


    【解决方案1】:

    看起来更像是您的安全规则出了问题。

    试试这个:

    service cloud.firestore {
        match /databases/{database}/documents {
            match /users/{userID} {
                allow read: if request.auth != null;
    
                match /{lists} {
                    allow write: if request.auth != null;
                }
            }
    
            match /users/test {
                allow read, write: if true;
            }
        }
    }
    

    另外,不要忘记您必须登录才能避免出现错误。

    【讨论】:

    • 但是根据您的规则,任何登录的用户都可以访问任何用户的数据。我应用的规则是从文档中复制的,以确保特定于用户的访问。还是我错了?我已经登录了。
    猜你喜欢
    • 2018-09-15
    • 2020-04-26
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2019-02-07
    • 2020-09-29
    • 2021-08-23
    相关资源
    最近更新 更多