【问题标题】:Firebase multi-location rules - same valueFirebase 多位置规则 - 相同的值
【发布时间】:2016-08-27 09:40:06
【问题描述】:

这是我最后想要的:

{
  "data" : {
    "account" : {
      "K1472290187836" : {
        "created" : 1472290190043,
        "id" : "K1472290187836"
      }
    },
    "auth" : {
      "d182ddec-f1c7-41c5-8b0e-198bfb5d9efe" : {
        "account_id" : "K1472290187836",
        "active" : true,
        "created" : 1472290190043,
        "id" : "d182ddec-f1c7-41c5-8b0e-198bfb5d9efe"
      }
    }
  }
}

这是我的数据类:

public class Account {
    public long created;
    public String id;

    public HashMap<String, Object> toMap() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("created", ServerValue.TIMESTAMP);
        return map;
    }
}

public class Auth {
    public String id;
    public long created;
    public boolean active;
    public String account_id;

    public HashMap<String, Object> toMap() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        map.put("created", ServerValue.TIMESTAMP);
        map.put("active", active);
        map.put("account_id", account_id);
        return map;
    }
}

这是我的多地点更新:

final Auth auth = new Auth();
auth.id = FirebaseAuth().getCurrentUser().getUid();
auth.active = true;
auth.account_id = "K" + System.currentTimeMillis(); // TODO replace with proper id generator

final Account account = new Account();
account.id = auth.account_id;

HashMap<String, Object> newUserMap = new HashMap<String, Object>();
newUserMap.put("/data/account/" + account.id, account.toMap());
newUserMap.put("/data/auth/" + auth.id, auth.toMap());


FirebaseDatabase().getReference().updateChildren(newUserMap);

我需要的是在可以存储数据之前验证 data/account/$account_id/id 与 /data/auth/$auth_id/account_id 具有相同值的规则。

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-security


    【解决方案1】:

    将数据修剪回最小值,看起来像你想要的:

    {
      "data" : {
        "account" : {
          "K1472290187836" : {
            "id" : "K1472290187836"
          }
        },
        "auth" : {
          "d182ddec-f1c7-41c5-8b0e-198bfb5d9efe" : {
            "account_id" : "K1472290187836",
          }
        }
      }
    }
    

    这可能是一个好的开始:

    {
      "rules": {
        "data": {
          "account": {
            "$id": {
              "id": {
                ".validate": "newData.val() == $id"
              }
            }
          },
          "auth": {
            "$uid": {
              ".write": "$uid === auth.uid",
              "account_id": {
                ".validate": "
    newData.parent().parent().child('account').child(newData.val()).exists() &&
    newData.parent().parent().child('account').child(newData.val()).child('id').val() === newData.val()"
              }
            }
          }
        }
      }
    }
    

    由于rootdata指的是写操作之前存在的数据,所以上面使用了newData,它指的是写操作之后它将存在的数据(如果该操作成功的话)。

    您实际上不需要 account_id 验证的双重条件,因为第二个复制了第一个的功能。但是由于我不确定您是要验证帐户密钥还是 id 属性的值,所以我添加了这两个条件以便于复制/粘贴/删除你不关心的那个。

    如果您真的需要在该片段中存储两次 ID,我建议您重新考虑。数据重复很常见,但在这种情况下,我并没有看到太多的价值收益(这会导致像上面这样的考虑:哪个是领先的?)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多