【问题标题】:firestore: let users update, delete only their own datafirestore:让用户更新,只删除自己的数据
【发布时间】:2018-06-24 14:57:06
【问题描述】:

这些是我当前的安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
  match /tournaments/{tournament=**} {
    allow update, delete: if request.auth.uid == resource.data.author;
    allow create: if request.auth.uid != null;
    allow read: if true;
  }
  }
}

一切正常,只有更新或删除因“缺少或权限不足”而不起作用

这是我的代码

            mDocRef
                .update(key, score)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "DocumentSnapshot successfully updated!");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.w(TAG, "Error updating document", e);
                    }
                });

mDocRef 引用路径为“tournaments/tournamentID/aColletion/aDocument”的文档

这里是创建 mDocRef 的代码:

            mTournamentDocRef = mTournamentColRef.document();
            mDocRef = mTournamentDocRef.collection("aCollection").document();

注意事项:

  • 用户在整个过程中通过firebase登录
  • 我尝试使用 .where("author", "==", user.uid) 但出现“无法解析方法”错误

感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    我查看了文档,这些是您正在寻找的规则:

    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
    

    以及文档链接作为参考:https://firebase.google.com/docs/firestore/security/rules-conditions?authuser=0

    【讨论】:

      【解决方案2】:

      我将 firebase 用户的 ID 添加到为获得所需结果而创建的每个文档中

       tournamentData.put("author", user.getUid());
       data.put("author", user.getUid());
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-18
        • 1970-01-01
        • 2016-09-13
        • 2021-02-22
        • 1970-01-01
        • 2015-01-04
        • 2019-09-21
        • 1970-01-01
        相关资源
        最近更新 更多