Android Firebase多位置规则-相同值

Android Firebase多位置规则-相同值,android,firebase,firebase-realtime-database,firebase-security,Android,Firebase,Firebase Realtime Database,Firebase Security,这就是我最后想要的: { "data" : { "account" : { "K1472290187836" : { "created" : 1472290190043, "id" : "K1472290187836" } }, "auth" : { "d182ddec-f1c7-41c5-8b0e-198bfb5d9efe" : { "account_id" : "K14722901

这就是我最后想要的:

{
  "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;
    }
}
公共类帐户{
公众长期创造;
公共字符串id;
公共HashMap toMap(){
HashMap=newHashMap();
地图放置(“id”,id);
map.put(“已创建”,ServerValue.TIMESTAMP);
返回图;
}
}
公共类身份验证{
公共字符串id;
公众长期创造;
公共布尔活动;
公共字符串帐户id;
公共HashMap toMap(){
HashMap=newHashMap();
地图放置(“id”,id);
map.put(“已创建”,ServerValue.TIMESTAMP);
映射放置(“活动”,活动);
map.put(“账户id”,账户id);
返回图;
}
}
这是我的多位置更新:

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);
final Auth Auth=new Auth();
auth.id=FirebaseAuth().getCurrentUser().getUid();
auth.active=true;
auth.account_id=“K”+System.currentTimeMillis();//TODO替换为正确的id生成器
决算账户=新账户();
account.id=auth.account\u id;
HashMap newUserMap=新HashMap();
newUserMap.put(“/data/account/”+account.id,account.toMap());
newUserMap.put(“/data/auth/”+auth.id,auth.toMap());
FirebaseDatabase().getReference().updateChildren(newUserMap);

我需要的是验证数据/account/$account\u id/id与/data/auth/$auth\u id/account\u id具有相同值的规则,然后才能存储数据。

将数据缩减到最小值,看起来您需要:

{
  "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()"
          }
        }
      }
    }
  }
}
由于
root
data
指的是写入操作之前存在的数据,因此上面使用的
newData
,它指的是写入操作之后将存在的数据(如果该操作成功)

您实际上不需要在帐户id验证中使用双重条件,因为第二个条件与第一个条件的功能相同。但由于我不确定您是要验证account key还是id属性的值,因此我添加了这两个条件,以便轻松复制/粘贴/删除您不关心的内容

我建议重新考虑是否确实需要在该片段中存储两次ID。重复数据很常见,但在这种情况下,我看不到太多的价值收益(这导致了上面提到的问题:哪一个是领先的?)