根据用户锁定特定节点的 read/write 访问权限

Locking down read/write access for a specific node based on user

我知道我也遇到过类似的问题,但我仍然对如何正确保护我的 Firebase 感到困惑。

首先,我使用的是 EmberFire。其次,我使用 Firebase Email/Password 身份验证进行帐户管理。我的每个账户都代表一个企业。我希望当前经过身份验证的企业只能访问 his/her 数据。最初,我的想法是使用以下数据结构:

+ businesses
    - uid-1
    - uid-2
    - uid-3

...其中 uid 是通过 Firebase email/password 身份验证分配给用户的。然后我会使用以下安全规则:

{
  "rules": {
    "businesses": {
      "$user_id": {
        ".read": "$user_id === auth.uid",
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

综上所述,有两个问题:

  1. 有用吗? (很确定它将基于 this post
  2. 如果是这样,我怎样才能让 EmberFire 允许我使用我自己的 ID 而不是由 push() 生成的 ID,根据文档,EmberFire 在幕后使用它。

更新 关于 EmberFire,这是我用来将模型保存到 Firebase 的代码:

var business = _this.store.createRecord('business', {
    uid: userData.uid,
    businessName: _this.get('businessName'),
    firstName: _this.get('firstName'),
    lastName: _this.get('lastName')
});

business.save().then(function(success) {
    flashMessages.success('Your account has been created! Please login below.');
    _this.transitionToRoute('login');
}, function(error) {
    flashMessages.warning(error);
});

我看到的是,这些创建的对象的根节点最终是由 Firebase 生成的唯一时间戳 ID。为了解决我的问题,我想将其覆盖到 auth.uid.

提前致谢! 詹姆斯

您可以在执行 createRecord 时指定 idid 将成为 firebase key:

var record = this.store.createRecord('business', {
  id: userData.uid,
  businessName: this.get('businessName'),
  firstName: this.get('firstName'),
  lastName: this.get('lastName')
});

您可能希望在创建用户记录之前确保该用户记录不存在,因此您需要一个 "find or create" 流程。在 Ember 数据中执行此类流程时会遇到一些问题。评论 here

中概述了一个示例解决方法