Java 如何从Firebase Auth和数据库授予对特定UID的读/写访问权限
我一直在努力从最近的研究中理解如何实现向系统中的某些用户授予不同的访问级别。我希望能够授予一个用户读访问权限,并授予另一个用户读/写引用其uid的权限。要实现这一点,需要做哪些工作 我是否需要重新构造我的数据库,我的JSON规则是如何构造的 更新-实施新规则和数据库结构 存储01的当前数据库参考-Java 如何从Firebase Auth和数据库授予对特定UID的读/写访问权限,java,android,firebase,firebase-realtime-database,firebase-security,Java,Android,Firebase,Firebase Realtime Database,Firebase Security,我一直在努力从最近的研究中理解如何实现向系统中的某些用户授予不同的访问级别。我希望能够授予一个用户读访问权限,并授予另一个用户读/写引用其uid的权限。要实现这一点,需要做哪些工作 我是否需要重新构造我的数据库,我的JSON规则是如何构造的 更新-实施新规则和数据库结构 存储01的当前数据库参考- database = FirebaseDatabase.getInstance().getReference("stores").child("Store 01").child("Task Lis
database = FirebaseDatabase.getInstance().getReference("stores").child("Store 01").child("Task List"); //Find the Task List table in database and making a reference.
将规则结构更新为以下内容
{
"rules": {
"stores": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
}
}
将数据库结构更新为以下内容
{
"rules": {
"stores": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
一种解决方案是让一些特定的数据库节点列出您的用户,如下所示:
{
"rules": {
"Store01": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
}
}
{
"rules": {
"stores": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
}
}
但是,对于您的数据模型,会出现问题,因为您正在创建多个存储
作为数据库根节点。每次创建新商店时,都需要更新安全规则
您需要在父节点中创建这些存储,例如存储
。因此,使用新的readUsers
和readWriteUsers
节点,您的数据库将如下所示:
- task-list-for-managers
- stores
- Store01
- ....
- Store02
- ....
- readUsers
- WV0676TY67TY9: true //user Id
- PU8776TIU6543: true
- .....
- readWriteUsers
- BD563DHDV7669: true //user Id
- 87RSBE6383912: true
- .....
规则如下:
{
"rules": {
"Store01": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
}
}
{
"rules": {
"stores": {
".read": "auth != null && (root.child('readUsers').hasChild(auth.uid) || root.child('readWriteUsers').hasChild(auth.uid))",
".write": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)"
},
"readUsers": {
".read": "auth != null && root.child('readUsers').hasChild(auth.uid)",
".write": false
},
"readWriteUsers": {
".read": "auth != null && root.child('readWriteUsers').hasChild(auth.uid)",
".write": false
}
}
}
请注意,如前所述,读写规则级联:
如果规则授予特定路径的读或写权限,则
它还授予对其下所有子节点的访问权
感谢Renaud,这看起来真的很可行,因为我正在尝试这样做,我发现在我自己的项目中很难理解“保护您的数据”文档。@AlexBingham很高兴我能提供帮助!你也可以接受我的回答,看。谢谢一旦我让它运行@renaudtarnec,我肯定会的。如果您需要更多帮助,请毫不犹豫地询问!我已经在我的项目中实现了上述功能,但是,我无法从其他读/写uid读取数据库中的任何内容。我已经用我目前的结构更新了我的帖子。