android + firestore update:PERMISSION_DENIED:缺少权限或权限不足

时间:2018-01-13 21:15:06

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

我想让每个用户只能更改名称和照片。而且我还想阻止任何其他数据输入。这些是我使用的rules

service cloud.firestore {
    match /databases/{database}/documents {
        match /users/{user} {
            allow read: if request.auth.uid == user;
            allow update: if request.auth.uid == user &&
                   request.resource.data.keys().hasAll(['name', 'photo']) &&
                   request.resource.data.size() == 2 &&
                   request.resource.data.name is string &&
                   request.resource.data.photo is string;
        }
    }
}

当我使用set()方法时,此规则正常,但在使用PERMISSION_DENIED: Missing or insufficient permissions.时会抛出update()异常。我无法使用set()方法,因为它会删除其他字段。这些是我的java代码。

private void update(FirebaseAuth fbAuth, String sNewName, String sNewPhoto){
    if (fbAuth.getUid() != null) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", sNewName);
        map.put("photo", sNewPhoto);

        Log.d(TAG, "changeProfile.map:" + map);

        firebasefirestore.collection("users")
                .document(fbAuth.getUid())
                .update(map)
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        Log.d(TAG, "changeProfile.onSuccess");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@android.support.annotation.NonNull Exception e) {
                        Log.d(TAG, "changeProfile.onFailure: " + e.getMessage());
                    }
                });
    }
}

0 个答案:

没有答案