首页 > 解决方案 > 指定 Firebase 实时数据库中属性的可读性

问题描述

这是我的数据库的结构:

users
  id1
    role: 'admin',
    tokens: [...]
  ...

这是我的规则列表:

{
    "rules": {
    "users": {
      ".read": "root.child('users').child(auth.uid).child('role').val() === 'admin'",
      "$user_id": {
        "role": {
          ".write": "$user_id === auth.uid",
          ".read": "$user_id === auth.uid",
          ".validate": "newData.child('role').val() === 'user'" 
        }, 
        "tokens": {
            ".write": "$user_id === auth.uid",
            ".read": "$user_id === auth.uid"
        },
        "$other": {
            ".read": false,
            ".write": false
        }
      }
    }
  }
}

我的意图是允许用户(在创建帐户时)将自己角色设置为“用户”,而不是“管理员”。用户可以自由地读取或写入自己的令牌,并且不应将其他属性写入表中。

问题是user在创建帐户时将用户角色设置为不再起作用。我在这里想念什么?

编辑:事后看来,我想我的问题可以简化为以下内容:

在文档中,我们给出了以下示例:

{
  "messages": {
    "message0": {
      "content": "Hello",
      "timestamp": 1405704370369
    },
    ...
  }
}
{
  "rules": {
    "messages": {
      "$message": {
        ".read": "true",
        ".write": "false"
      }
    }
  }
}

content有没有办法为 and等属性自定义读写行为timestamp,或者我们可以只使用一个规则来描述整个$message?

标签: firebasefirebase-realtime-database

解决方案


推荐阅读