2

我正在寻找一种解决方案,允许首次在 Firebase 中创建数据,然后限制对该数据所有者(或管理员)的访问。从本质上讲,我已经为一家企业创建了一个注册流程,该流程在 Firebase 中创建了一个用户,并为该企业创建了一个关联用户。不幸的是,根据我当前的架构和安全规则,我遇到了permission_denied错误。这是我当前的模式,它是由 EmberFire 使用businessemployee模型之间的 hasMany <--> belongsTo 关系生成的:

+ businesses
  + business_id (generated by EmberFire)
    - name
    + employees
      - employee_id
      - employee_id
+ employees
  + employee_id (generated by Firebase's auth system)
    - first name
    - last name
    - business_id
    - role (ex: 99 for admin)

这是我的安全规则:

{
  "rules": {
    "businesses": {
      // allows only administrators of the business to read and write business data
      "$business_id": {
        ".read": "root.child('employees/' + auth.uid + '/business').val() === $business_id && root.child('employees/' + auth.uid + '/role').val() === 99",
        ".write": "root.child('employees/' + auth.uid + '/business').val() === $business_id && root.child('employees/' + auth.uid + '/role').val() === 99"
      }
    },
    "employees": {
      // only allow employees to read/write their own data or the admin of the business they belong to
      "$employee_id": {
        ".read": "auth.uid === $employee_id || (root.child('employees/' + auth.uid + '/role').val() === 99 && root.child('businesses/' + (root.child('employees/' + auth.uid + '/business').val()) + '/employees/' + $employee_id).exists())",
        ".write": "auth.uid === $employee_id || (root.child('employees/' + auth.uid + '/role').val() === 99 && root.child('businesses/' + (root.child('employees/' + auth.uid + '/business').val()) + '/employees/' + $employee_id).exists())"
      }
    }
  }
}

任何人都可以推荐一组更新的安全规则/架构,以允许在注册期间最初创建数据,然后只能由该数据的所有者/管理员访问?

注意:我目前正在运行 Firebase 2.2.4 和 EmberFire 1.4.4。

在此先感谢,詹姆斯

更新

这是注册过程中使用的代码:

// first, create the user account to be the admin of the business
_this.firebase.createUser({
  email: _this.get('email'),
  password: _this.get('password')
}, function(error, userData) {

  if (error) {
    flashMessages.warning(error);
  } else {

    // authenticate the user and log them in
    _this.get('session').authenticate('authenticator:firebase', {
      'email': _this.get('email'),
      'password': _this.get('password')
    }).then(function() {

      // create the business record
      var business = _this.store.createRecord('business', {
        name: _this.get('businessName')
      });

      // create the employee record and associate the firebase uid
      var employee = _this.store.createRecord('employee', {
        id: userData.uid,
        firstName: _this.get('firstName'),
        lastName: _this.get('lastName'),
      });

      // add the employee<->business relationship
      business.get('employees').then(function(employees) {
        employees.addObject(employee);
      });

      // save the records to Firebase
      employee.save().then(function() {
        business.save();
      });

    }).catch(function(error) {
      flashMessages.warning(error);
    });

  }

}); 

此外,这是说明我所描述内容的数据快照:

数据快照

4

1 回答 1

3

您的安全规则需要role = 99,但您从未设置它。

以下似乎可以解决您的问题:

// create the employee record and associate the firebase uid
var employee = _this.store.createRecord('employee', {
  id: userData.uid,
  firstName: _this.get('firstName'),
  lastName: _this.get('lastName'),
  role: 99
});

要回答更大的问题,是的,写入是分多个部分完成的。这些hasMany关系单独保存,以免完全覆盖其他客户端上可能发生的任何更改。更具体地说,每个hasMany链接都是在单独的写入中添加或删除的,因此可能有 2 次或更多次写入。

就您的代码而言,business/<id>/employees<employee_id>首先保存了链接,这意味着在第二次通过时 - 在编写主business/<id>哈希时 -!data.exists()您的部分规则失败了。这很好,因为员工已创建并链接到业务,但role = 99检查也失败了。

于 2015-04-23T22:51:43.887 回答