我的 Firebase 数据结构如下所示
-isAdmin
-user1
isAdmin: true
-users
-user1
-firsName: Jane
-lastLoggedIn: 12 March 2017
-user2
-firstName: John
-lastLoggedIn: 11 March 2017
我希望我的管理员(用户 1)能够执行以下操作
向 -users 分支添加更多用户。所以我需要以下权限(创建 user3、user4...等)
"users": {
.write: "(auth != null) && (root.child('isAdmin').child(auth.id).val == true)
}
但是我也希望非管理员用户能够更新最后登录条目。所以我需要以下权限
"users": {
$userId: {
"lastLoggedIn": {
.write: "(auth != null) && ($userId == auth.id)
}
}
}
这就是问题所在,firebase不允许嵌套规则,因为我在users下有一个.write规则,我相信users/$userId/lastLoggedIn下的.write规则将被忽略
有办法解决这个问题吗?
最佳答案
Firebase 安全规则不允许嵌套规则的说法是错误的 - 它们确实允许。规则级联,您需要注意的情况是,一旦授予权限,“嵌套”规则就无法撤销该权限。
因此,仅当更高层的规则已授予写入权限时,“嵌套”规则才会被忽略。如果尚未授予写入权限并且“嵌套”规则授予写入权限,则不会忽略它。
按照这些规则:
"users": {
".write": "(auth != null) && (root.child('isAdmin').child(auth.uid).val == true),
"$userId": {
"lastLoggedIn": {
".write": "(auth != null) && ($userId == auth.uid)
}
}
}
管理员和用户本身都可以写入lastLoggedIn
。
关于firebase - 复杂的 Firebase 安全规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42827179/