Firebase 规则授予消息收件人读取权限
Firebase rule to give read access to the recipient of the message
我正在尝试使用 Ionic 2 + angularFire2 制作移动应用程序。
基本上我的数据库将由用户和消息组成。用户将通过电子邮件和密码进行身份验证。
用户将通过以下方式交换消息:
- 正在搜索收件人电子邮件是否为 valid/registered
- 正在向收件人发送消息
- 只能收件人
阅读邮件
示例数据:
"messages" : {
// message key
"-KbUpdXdFT6ggEqCXDd5" : {
"data" : "Lorem Ipsum",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
"-KbUr1_8AakzKjdwfmny" : {
"data" : "Some message",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
}
"users": {
// users key
"OpYYjG3iJsprgPNmhrNZ: {
"email": "john.doe@gmail.com"
},
"bwEd48upa0e7h5ghKEpo":{
"email": "bill.doe@gmail.com"
}
}
示例规则:
"rules": {
"messages":{
// everyone is allowed to write new messages
".write": "auth != null",
"$message": {
// read only if you are an authenticated user and your email is the same as the recipient
".read": "auth != null && data.child('recipient').val() == auth.token.email"
},
"users":{
".read": "auth != null"
}
}
设置规则后,用户 John Doe 将调用此查询以获取收到的消息列表:
messages = this.af.database.list('messages',{
query:{
orderByChild: 'recipient',
equalTo: "john.doe@gmail.com"
}
});
问题:
我不断收到 "Client doesn't have permission to access the desired data." 错误。
我猜它与以下内容有关:
".read": "auth != null && data.child('recipient').val() == auth.token.email"
我尝试了各种其他规则,但没有成功。甚至考虑过采用更嵌套的设计,将消息置于用户之下,但这似乎被认为是一种反模式。
欢迎任何帮助、建议或教程!谢谢!
您的问题与一个讨论非常广泛的主题有关:
Firebase 规则不是过滤器。
最主要的是,您不能使用规则来按照您想要的方式进行过滤,如果您有权访问节点的一个子节点而不是其兄弟节点,则在尝试读取父节点时会出错。
解决方案
一种解决方案是将您的结构更改为如下所示:
"messages" : {
// Each recipient has his own child node
"user-id-of-john-doe" : {
"KbUpdXdFT6ggEqCXDd5" : {
"data" : "Lorem Ipsum",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
"-KbUr1_8AakzKjdwfmny" : {
"data" : "Some message",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
}
}
John Doe 是您的收件人。
使用这种方法,每个用户都有自己的消息子节点,他可以被授予读取权限,其他用户可以将他们的消息添加到该用户。如果您想观察特定用户收到的消息,您只需观察节点
messages/userId-or-email-or-whatever-unique-identifier-you-use/
我正在尝试使用 Ionic 2 + angularFire2 制作移动应用程序。
基本上我的数据库将由用户和消息组成。用户将通过电子邮件和密码进行身份验证。
用户将通过以下方式交换消息:
- 正在搜索收件人电子邮件是否为 valid/registered
- 正在向收件人发送消息
- 只能收件人 阅读邮件
示例数据:
"messages" : {
// message key
"-KbUpdXdFT6ggEqCXDd5" : {
"data" : "Lorem Ipsum",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
"-KbUr1_8AakzKjdwfmny" : {
"data" : "Some message",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
}
"users": {
// users key
"OpYYjG3iJsprgPNmhrNZ: {
"email": "john.doe@gmail.com"
},
"bwEd48upa0e7h5ghKEpo":{
"email": "bill.doe@gmail.com"
}
}
示例规则:
"rules": {
"messages":{
// everyone is allowed to write new messages
".write": "auth != null",
"$message": {
// read only if you are an authenticated user and your email is the same as the recipient
".read": "auth != null && data.child('recipient').val() == auth.token.email"
},
"users":{
".read": "auth != null"
}
}
设置规则后,用户 John Doe 将调用此查询以获取收到的消息列表:
messages = this.af.database.list('messages',{
query:{
orderByChild: 'recipient',
equalTo: "john.doe@gmail.com"
}
});
问题: 我不断收到 "Client doesn't have permission to access the desired data." 错误。
我猜它与以下内容有关:
".read": "auth != null && data.child('recipient').val() == auth.token.email"
我尝试了各种其他规则,但没有成功。甚至考虑过采用更嵌套的设计,将消息置于用户之下,但这似乎被认为是一种反模式。
欢迎任何帮助、建议或教程!谢谢!
您的问题与一个讨论非常广泛的主题有关:
Firebase 规则不是过滤器。
最主要的是,您不能使用规则来按照您想要的方式进行过滤,如果您有权访问节点的一个子节点而不是其兄弟节点,则在尝试读取父节点时会出错。
解决方案 一种解决方案是将您的结构更改为如下所示:
"messages" : {
// Each recipient has his own child node
"user-id-of-john-doe" : {
"KbUpdXdFT6ggEqCXDd5" : {
"data" : "Lorem Ipsum",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
"-KbUr1_8AakzKjdwfmny" : {
"data" : "Some message",
"recipient" : "john.doe@gmail.com",
"sender" : "bill.doe@gmail.com"
},
}
}
John Doe 是您的收件人。
使用这种方法,每个用户都有自己的消息子节点,他可以被授予读取权限,其他用户可以将他们的消息添加到该用户。如果您想观察特定用户收到的消息,您只需观察节点
messages/userId-or-email-or-whatever-unique-identifier-you-use/