【发布时间】:2018-08-14 21:13:06
【问题描述】:
我创建了一个应用程序,用户可以在其中发布Tasks。注册用户可以查看其他用户的任务,但不应允许对其进行编辑,除非用户是该任务的发布者。
直到现在我还没有设置 firebase 规则,所以所有内容都可以被删除或读取。我会尽我所能解释我想要完成的事情。
如果我专注于该节点中的主要任务道具,它包含 3 个基本道具:
- 状态
- 海报ID
- 工人ID
任务的发布者或其他任何人都不能编辑状态,它应该由服务器功能处理。应该通过发布新任务来设置posterID,并且它也不应该对任何人进行编辑。而阅读规则,注册用户可以阅读一个任务的所有道具。
我想避免接受任务作为数据并通过数据库管理员权限设置它的服务器功能,因为用户将无法以这种方式体验离线持久性。
我想出了这个“不是很有效”的解决方案。我一直在测试是否可以编辑未由我的用户 ID 发布的任务的状态,因为它会更改状态。它不应该这样做的地方。
安全规则:
"Tasks" : {
".read": "auth != null",
".write": "auth != null", //Users can post tasks
"$taskID":{
// Only the owner of the task can edit its props
".write": "root.child('Tasks').child('$taskID').child('posterID').val() === auth.uid",
// But the status is for nobody editable
"status": {
".write": false
}
}
}
代码:
updateForeignTask() {
firebase
.database()
.ref("Tasks")
.child("-LJraTW2KV7BgcSZ8yJ6")
.update({ status: 2 });
}
testingFBRules() {
const taskID = firebase
.database()
.ref("Tasks")
.push().key;
var task_to_post = {
taskID: taskID,
posterID: firebase.auth().currentUser.uid,
title: "test",
desc: "test",
creationDate: firebase.database().getServerTime(),
//status: 0,
workerID: ""
};
firebase
.database()
.ref("Tasks")
.child(taskID)
.set(task_to_post)
.then(res => {
var temp;
alert("res : " + res);
})
.catch(error => {
var temp;
alert("err: " + error);
});
}
我已经注释掉了 testingFBRules() 上的状态属性,以便能够发布任务。我可以想象触发一个云功能,然后使用管理员权限添加状态。但我不确定这是否是正确的方法。
我的问题是,如何设置 firebase-rules 以符合我的要求。如果不可能,我应该如何重组我的数据库以使其成为可能?
【问题讨论】:
-
你之前不是问过这个(或类似的东西)吗?
-
是的。 stackoverflow.com/questions/51833803/… @niksn:请不要转发相同的问题。如果您犯了错误或有其他信息,请单击问题下方的编辑链接进行更新。
标签: javascript firebase firebase-realtime-database firebase-security