Firebase database rules. Allow when wildcard's child === auth.uid

℡╲_俬逩灬. 提交于 2019-12-24 00:15:56

问题


Structure:

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

Rules:

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

Goal: Instead of using the auth.uid as key for the data, I would rather prefer to use the generated key from push().getKey()

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

Looking for the rule set for something like this:

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

回答1:


Answering your question, you will be able to achieve the rules you want with the rules bellow.

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

But keep in mind that, to retrieve and write data, you would need to already know the $key's since you wont be able to get any data by accessing /accounts. Thats beacause you don't have read/write rules to access the specific /accounts branch.

If it was in my hands I would prefer going with your current /accounts/uid solution. You would always be easly able to retrieve users data since you could get the current authenticated user uid.



来源:https://stackoverflow.com/questions/38355348/firebase-database-rules-allow-when-wildcards-child-auth-uid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!