【发布时间】:2020-07-11 00:32:58
【问题描述】:
在我的数据库中,我有以下结构。
/Players/UID{1,2,3,...n}/ 每个 UID ==> {Child1,Child2,...Childn}
在规则中,为了保证数据的安全,我在childs之间创建了一个类似下面的函数,但是写操作任务出错了,我不知道原因。此外,您能否提出一些更安全、更实用的实施方案?
{
"rules": {
".read": "auth != null",
".write": "auth != null && newData.child('Child1').val() * newData.child('Child1').val() * 1000000 + 1000 * newData.child('Child1').val() - 1000 === newData.child('Child2').val()"
}
}
我的简化写函数如下,其中 Child2 是 Child1 的函数;
public void WriteDatabase(float child1, int child2)
{
if (AuthController.Instance != null && AuthController.Instance.user != null)
{
uid = AuthController.Instance.user.UserId;
}
else
{
return;
}
LeaderBoardEntry entry = new LeaderBoardEntry(child1, child2);
Dictionary<string, System.Object> entryValues = entry.ToDictionary();
Dictionary<string, System.Object> childUpdates = new Dictionary<string, System.Object>();
childUpdates["/Players/" + uid + "/"] = entryValues;
reference.UpdateChildrenAsync(childUpdates).ContinueWith(task =>
{
if (task.IsCanceled)
{
UnityEngine.Debug.Log("Write database cancelled");
return;
}
if (task.IsFaulted)
{
UnityEngine.Debug.Log("Write database faulted");
return;
}
if (task.IsCompleted)
{
UnityEngine.Debug.Log("Write database completed");
}
});
}
注意:write 函数适用于以下规则:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
【问题讨论】:
-
我编辑了,谢谢你的评论。
-
我们无法知道
child1和child2在这里是什么。请确保代码既简洁又完整,这意味着我们可以独立运行它,而不需要您的问题中不存在的任何内容。
标签: c# firebase-realtime-database firebase-security