android - firestore : let users update, 只删除自己的数据

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

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

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) 但出现“无法解析方法”错误

感谢您的帮助!

最佳答案

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

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

关于android - firestore : let users update, 只删除自己的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51011082/

相关文章:

android - 如何在 Android 上使用带有 V8 的 Chrome 调试器

android - Facebook FriendPickerFragment 显示没有 friend

firebase - 如何在Flutter上使用Singleton Firebase服务?

javascript - Firebase Web 注销不会发送任何 XHR

ios - 无需注册的 Firebase 唯一 ID

java - ActionBar 图标中的共享按钮不是默认的。怎么改?

java - RxJava 全局处理程序和 Android Vitals

java - 异常java.lang.IllegalStateException: Activity 已被破坏

android - 如何减少firebase中的数据库大小?

firebase - 是否仍然可以在 Firebase 3 中对 token 进行服务器端验证?