【问题标题】:Basic security rules with FirebaseFirebase 的基本安全规则
【发布时间】:2015-01-05 05:12:27
【问题描述】:

我在使用 Firebase 实施基本安全规则时遇到问题(我阅读了有关 Firebase 和 StackExchange 的文档,但无法使安全规则生效):

模型(模型的 Emberjs 表示):

App.User = DS.Model.extend({
  uid: DS.attr('string'),
  displayName: DS.attr('string'),
  books: DS.hasMany('statistic', { inverse: 'user', async: true}),
  actions: DS.hasMany('action', { inverse: 'user', async: true}),
});


App.Action = DS.Model.extend({
  date: DS.attr('date'),
  actionType: DS.attr('string'),
  comment: DS.attr('string'),
  user: DS.belongsTo('user', {inverse: 'actions', async: true} )
});


App.Book = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  user: DS.belongsTo('user', { inverse: 'books', async: true} )
});

这 3 个节点(模型)直接存储在 Firebase 应用的根目录中。 Book 和 Action 模型有一个 user 字段(属性)。

这样写的规则是什么:

  1. 只有在 Book 和 Action 模型(节点)的用户字段中标识的用户才能对自己的数据进行读写访问? (Book 和 Action 中 user 字段的值必须等于 Firebase 中 auth.uid 的值,才能授予用户读写权限。)
  2. 用户只能访问属于他们的用户模型(节点)的信息吗?

谢谢

【问题讨论】:

    标签: ember.js firebase-security emberfire


    【解决方案1】:

    了解 Firebase 中的数据结构很重要。

    基本上,有两种方法可以编写安全规则。您可以在 books/ 下设置安全规则,也可以分别为每个模型属性编写安全规则。或两者兼而有之,但请确保您首先了解自上而下的原则。

    我更喜欢为每个属性单独编写规则,这样更容易维护和测试。

    但在你的情况下,因为其他用户不需要访问书籍或用户的某些部分,所以很容易为整个模型编写规则:

    "rules" :{
      "books": {
        "$book_id": {
          ".read": "data.child('user').val() === auth.uid && auth !== null",
          ".write": "!data.exists() && newData.child('user').val() === auth.uid || data.child('user').val() === newData.child('uid').val() && auth !== null"
         },
       "users": {
         "$user_id": {
          ".read": "data.child('uid') === auth.uid",
           ".write": "!data.exists() && newData.child('uid').val() === auth.uid || data.child('uid').val() === newData.child('uid').val()"
         }
       }
      }
    }

    我没有测试这些规则,它们可能包含缺陷,请使用模拟器工具使其防弹:]

    查看我的中号帖子了解更多信息:https://medium.com/@martinmalinda/emberfire-is-awesome-but-querying-data-and-writing-security-rules-can-be-a-pain-f5370f4decb

    【讨论】:

      猜你喜欢
      • 2021-01-21
      • 2021-04-11
      • 2016-06-27
      • 1970-01-01
      • 2016-08-19
      • 2018-11-27
      • 2020-09-06
      • 2016-07-16
      相关资源
      最近更新 更多