【问题标题】:Firebase database rules. Allow when wildcard's child === auth.uidFirebase 数据库规则。当通配符的孩子 === auth.uid 时允许
【发布时间】:2016-11-16 06:11:23
【问题描述】:

结构:

{
  "accounts" : {
    "JGeRgwAUBM..." : {
      "active" : true,
      "created" : 1468406951438,
      "key" : "JGeRgwAUBM..."
    }
}

规则:

{
  "rules": {
    ".read": false,
    ".write": false,    
    "accounts": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

目标: 我宁愿使用从 push().getKey() 生成的密钥,而不是使用 auth.uid 作为数据的密钥

{
  "accounts" : {
    "theKeyIGetFrom: push().getKey()" : {
      "active" : true,
      "created" : 1468406951438,
      "auth_uid" : "JGeRgwAUBM..."
    }
}

寻找这样的规则集:

{
  "rules": {
    ".read": false,
    ".write": false,    
    "accounts": {
      "$key": {
        ".read": "$key.auth_uid === auth.uid",
        ".write": "$key.auth_uid === auth.uid"
      }
    }
  }
}

【问题讨论】:

  • 能否请您添加您想要此更改的原因?
  • 只是随便玩,问题就出现了
  • 这听起来很合理。很高兴我能帮助你:) 继续询问是否有任何进一步的疑问!

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


【解决方案1】:

回答您的问题,您将能够通过以下规则实现您想要的规则。

{
  "rules": {
    "accounts": {
      "$key": {
        ".read": "data.child(auth_uid).val() === auth.uid",
        ".write": "data.child(auth_uid).val() === auth.uid"
      }
    }
  }
}

但请记住,要检索和写入数据,您需要已经知道$key,因为您无法通过访问/accounts 来获取任何数据。那是因为您没有读/写规则来访问特定的 /accounts 分支。

如果它在我手中,我更愿意使用您当前的/accounts/uid 解决方案。您将始终能够轻松检索用户数据,因为您可以获得当前经过身份验证的用户 uid。

【讨论】:

    猜你喜欢
    • 2021-12-12
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2016-09-28
    • 2020-05-31
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多