Javascript 限制可写入路径的记录数(参考安全规则中的其他路径)
假设我的Firebase系列看起来像:Javascript 限制可写入路径的记录数(参考安全规则中的其他路径),javascript,firebase,firebase-security,Javascript,Firebase,Firebase Security,假设我的Firebase系列看起来像: { "max":5 "things":{} } 如何在安全规则中使用max的值来限制事物的数量 { "rules": { "things": { ".validate": "newData.val().length <= max" } } } { “规则”:{ “事情”:{ “.validate”:“newData.val().length使用现有属性是使用或完成的,非常简单 { "rules":
{
"max":5
"things":{}
}
如何在安全规则中使用max
的值来限制事物的数量
{
"rules": {
"things": {
".validate": "newData.val().length <= max"
}
}
}
{
“规则”:{
“事情”:{
“.validate”:“newData.val().length使用现有属性是使用或完成的,非常简单
{
"rules": {
"things": {
// assuming value is being stored as an integer
".validate": "newData.val() <= root.child('max')"
}
}
}
一个真实的例子
这样的数据结构可以工作:
/max/<number>
/things_counter/<number>
/things/$record_id/{...data...}
每次删除记录时,计数器都必须递减
var recordId = 'thing123';
var fb = new Firebase(URL);
fb.child('thing_counter').transaction(function(curr) {
if( curr === 0 ) { return undefined; } // cancel if no records exist
return (curr||0)-1;
}, function(err, success, snap) {
// if the counter updates successfully, then write the record
if( err ) { throw err; }
else if( success ) {
var ref = fb.child('things/'+recordId).remove(function(err) {
if( err ) { throw err; }
console.log('removed '+recordId);
});
}
});
现在谈谈安全规则:
{
"rules": {
"things": {
".write": "newData.hasChildren()", // is an object
"thing1": { ".validate": true },
"thing2": { ".validate": true },
"thing3": { ".validate": true },
"thing4": { ".validate": true },
"thing5": { ".validate": true },
"$other": { ".validate": false
}
}
}
{
"rules": {
"max": { ".write": false },
"thing_counter": {
".write": "newData.exists()", // no deletes
".validate": "newData.isNumber() && newData.val() >= 0 && newData.val() <= root.child('max').val()"
},
"things": {
".write": "root.child('thing_counter').val() < root.child('max').val()"
}
}
}
{
“规则”:{
“max”:{”.write:false},
“物品计数器”:{
“.write”:“newData.exists()”,//不删除
“.validate”:“newData.isNumber()&&newData.val()>=0&&newData.val()使用现有属性是使用或完成的,非常简单
{
"rules": {
"things": {
// assuming value is being stored as an integer
".validate": "newData.val() <= root.child('max')"
}
}
}
一个真实的例子
这样的数据结构可以工作:
/max/<number>
/things_counter/<number>
/things/$record_id/{...data...}
每次删除记录时,计数器都必须递减
var recordId = 'thing123';
var fb = new Firebase(URL);
fb.child('thing_counter').transaction(function(curr) {
if( curr === 0 ) { return undefined; } // cancel if no records exist
return (curr||0)-1;
}, function(err, success, snap) {
// if the counter updates successfully, then write the record
if( err ) { throw err; }
else if( success ) {
var ref = fb.child('things/'+recordId).remove(function(err) {
if( err ) { throw err; }
console.log('removed '+recordId);
});
}
});
现在谈谈安全规则:
{
"rules": {
"things": {
".write": "newData.hasChildren()", // is an object
"thing1": { ".validate": true },
"thing2": { ".validate": true },
"thing3": { ".validate": true },
"thing4": { ".validate": true },
"thing5": { ".validate": true },
"$other": { ".validate": false
}
}
}
{
"rules": {
"max": { ".write": false },
"thing_counter": {
".write": "newData.exists()", // no deletes
".validate": "newData.isNumber() && newData.val() >= 0 && newData.val() <= root.child('max').val()"
},
"things": {
".write": "root.child('thing_counter').val() < root.child('max').val()"
}
}
}
{
“规则”:{
“max”:{”.write:false},
“物品计数器”:{
“.write”:“newData.exists()”,//不删除
“.validate”:“newData.isNumber()&&newData.val()>=0&&newData.val()虽然我认为仍然没有可用的规则来执行此操作,但这里有一个示例云函数可以执行此操作:
虽然我认为仍然没有可用的规则来做这样的事情,但这里有一个示例云函数可以做到这一点:
这是非常彻底的-感谢您抽出时间来指导我。非常有用。关于$id>='rec'+root.child('incid/counter').val()的快速问题
其中,$id
等于您小提琴中的计数器值。如果用户删除$id
说记录3
我们可能只剩下1,2,4,5
,在这一点上,这些规则将从右边消失?我将如何处理?嗨,丹,是的,这是两种不同的策略,一种用于创建唯一的增量记录,另一个用于强制执行最大值。将两者结合起来需要一些计划,我在这里没有这样做。我试图想象,如果$id
不是与当前计数器
值匹配的整数,.write
在一个对象上怎么可能强制执行这两个记录多条记录共享,或者如果每个$id
都是随机的?你有什么想法不需要$id
使用计数器来递增吗?这是我提出的完全客户端方法的唯一想法。你总是可以在大约20行代码中启动一个节点进程来监视路径、计数记录、执行ce最大值,等等。是的,如果$id
想要摆脱这种模式,我想这就是我必须要做的。这是非常彻底的-感谢你花时间来指导我。非常有用。关于$id>='rec'+root.child('incid/counter').val()的快速问题
其中,$id
等于您小提琴中的计数器值。如果用户删除$id
说记录3
我们可能只剩下1,2,4,5
,在这一点上,这些规则将从右边消失?我将如何处理?嗨,丹,是的,这是两种不同的策略,一种用于创建唯一的增量记录,另一个用于强制执行最大值。将两者结合起来需要一些计划,我在这里没有这样做。我试图想象,如果$id
不是与当前计数器
值匹配的整数,.write
在一个对象上怎么可能强制执行这两个记录多条记录共享,或者如果每个$id
都是随机的?你有什么想法不需要$id
使用计数器来递增吗?这是我提出的完全客户端方法的唯一想法。你总是可以在大约20行代码中启动一个节点进程来监视路径、计数记录、执行ce the max,等等。是的,如果$id
想要摆脱这种模式,我想这就是我必须要做的。感谢Gonzalo。这是一个真正严格且功能强大的解决方案。这是因为可以将逻辑和限制他/她的用户限制在客户端,但是使用云功能,任何试图绕过的人都很容易被抓住你在客户端提供的规则。谢谢Gonzalo。这是一个真正严格且功能强大的解决方案。这是因为你可以在客户端设置逻辑并限制他/她的用户,但是使用云功能,很容易抓到任何试图绕过你在客户端提供的规则的人。检查it out