Firebase安全规则每个节点Emberfire多个访问级别

时间:2017-01-25 22:42:38

标签: javascript ember.js firebase emberfire

我有两个Ember模型:itemscomments。用户将发布项目,其他用户将能够对项目发表评论。

我无法在firebase中设置允许namedescription只能由当前用户写入的安全规则,但允许任何已记录的comments写入// app/models/item.js export default DS.Model.extend({ name: DS.attr('string'), description: DS.attr('string'), comments: DS.hasMany('comment'), user: DS.belongsTo('user') }) 在用户中。

物品

// app/models/comment.js
export default DS.Model.extend({
  user: DS.belongsTo('user')
  body: DS.attr('string'),
  timestamp: DS.attr('string'),
  item: DS.belongsTo('user')
})

注释

// app/components/comment-form.js
const comment = this.get('comment');
const item = this.get('item');
// service that tracks the currently logged in user
const currentUser = this.get('sessionManager.user');
comment.set('timestamp', new Date());
comment.set('user', currentUser);

// setup both sides of the relationship
item.get('comments').pushObject(comment);
comment.set('item', item');

// save both
return Ember.RSVP.Promise.all([item.save(), user.save()]);

保存评论

"items": {
    ".read": "auth !== null",
    "$item_id": {
        // only the currently logged in user can write to this node
        ".write": "root.child('users/'+data.child('user').val()+'/uid').val() === auth.uid",
        // allow any user to write comments
        "comments": {
            ".write": "auth !== null"
        }
    }
},

这一切都很好。今天,我在firebase中添加了安全规则。我只希望当前登录的用户能够编辑项目,但允许任何其他用户向任何项目添加注释。

/items/<item_id>/description

在firebase模拟器中这是有效的。作为拥有该项目的用户,我可以写入/items/<item_id>/comments/。作为不拥有该项目的用户,我可以写信至/items/<item_it>/description,但不能写入item.save()。然而它在Ember中使用Emberfire失败了。

我的工作理论是,当我向一个项目添加新评论时,我没有&#34;拥有&#34;,然后我调用/items/<item_id> Emberfire尝试写入item

如何设置Firebase安全规则,以便只有拥有comment的用户才能更新其大部分属性,但任何用户都可以添加/items/<item_id>/comments

这是在firebase规则模拟器中:

尝试使用不拥有此项目项的用户使用数据写入{ "cde": "comment_id" }

/items/<item_id>

将成功使用上述规则编写。

然而,

尝试使用不拥有此项目项的用户使用数据写入{ "comments": { "cde": "comment_id" } }

d = {}
file = open('data.txt','r')
for line in file:
        data = line.split()
        if len(data) == 2:
            key, value = data[0], data[1]
            d[key] = value
print(d)

失败。

1 个答案:

答案 0 :(得分:1)

问题在于.write规则中您使用了data predefined variabledata指的是:

  

表示尝试操作之前存在的数据的RuleDataSnapshot

对于新项目,data.exists()将为false。要允许用户编写新项目,您应该使用newData,而不是:

".write": "root.child('users/' + newData.child('user').val() + '/uid').val() === auth.uid"

并且,如果您想允许删除项目,您应该测试是否存在newData并使用data(如果它不存在):

".write": "root.child('users/' + (newData.exists() ? newData.child('user').val() : data.child('user').val()) + '/uid').val() === auth.uid"
相关问题