使用文本和 user_id 的组合在位置上设置规则

setting a rule on a location with a combination of text and user_id

我的数据库结构如下:

users
 User_ddac15a6-1890-43d8-9bfb-a13e9b99499e
 ...

即用户文件夹格式为'User_'+$user_id

我正在尝试设置如下规则:

{
  "rules": {
    "users": {
      "User_$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".read": "$user_id === auth.uid",
        ".write": "$user_id === auth.uid"
      }
    }
  }
}

但是在尝试保存规则时我得到:

9:7: Key names can't contain ".", "#", "$", "/", "[", or "]" (unbound names start with "$")

如何为这样的文件夹名称结构设置安全规则?

你不能把 $ 符号放在规则名称的中间,它必须在开头。但是你可以像这样得到想要的结果:

{
  "rules": {
    "users": {
      "$user_id": {
        // grants write access to the owner of this user account
        // whose uid must exactly match the key ($user_id)
        ".read": "$user_id === ('User_'+auth.uid)",
        ".write": "$user_id === ('User_'+auth.uid)"
      }
    }
  }
}