firestore:让用户仅更新,删除自己的数据

时间:2018-06-24 14:57:06

标签: 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),但收到“无法解析方法”错误

谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我将Firebase用户的ID添加到创建的每个文档中,以获取所需的结果

var async = require('async');

// custom imports
var User = require('../models/user');
var Article = require('../models/article');

var List1Objects = User.find({});
var List2Objects = Article.find({});
var resourcesStack = {
    usersList: List1Objects.exec.bind(List1Objects),
    articlesList: List2Objects.exec.bind(List2Objects),
};

async.parallel(resourcesStack, function (error, resultSet){
    if (error) {
        res.status(500).send(error);
        return;
    }
    res.render('home', resultSet);
});

答案 1 :(得分:0)

我检查了文档,这些是您要寻找的规则:

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