【发布时间】:2021-11-14 06:30:41
【问题描述】:
我在设置安全规则时遇到了困难
数据结构
-- books (collection)
-- bookId (autogenerated Id - doc)
-- {chapter: text}, {chapter: text},
userBooks
-- email (email of user logged in - doc)
-- books (sub collection)
-- bookId (referencing bookId doc on collection book)
-- role: admin or read
用例如下,一个拥有管理员角色的用户是唯一被允许共享一本书的用户,允许他在另一个用户的书中添加一个条目。
创建了试图实现该功能但没有成功通过的函数
我使用电子邮件 userWantsToShare@gmail.com 以用户身份登录。当用户尝试将他的书分享给 addUser@gmail.com 我提出以下请求:
userWantsToShare@gmail.com 请求在 addUser@gmail.com 集合下写入。
final role = {'role': 'edit'};
await FirebaseFirestore.instance
.collection('userBooks')
.doc("addUser@gmail.com")
.collection('books')
.doc('9KHYZJVBY3BNAlYPYYoA')
.set(role);
来到火力基地时
这应该翻译成 /userBooks/addUser@gmail.com/books/9KHYZJVBY3BNAlYPYYoA 数据 {'role': 'edit'}。
match /userBooks/{emailId}/books/{bookId} {
allow write: if isSharedEmail(bookId);
}
//here im verifying the user that wants to share has admin role and therefore is authorised to write in another user book subcollection
function isSharedEmail(bookId){
//this should translate to /userBooks/userWantToShare@gmail.com/books/9KHYZJVBY3BNAlYPYYoA
get(/databases/$(database)/documents/userBooks/$(request.auth.token.email)/books/$(bookId)).data.role == "admin" ;
}
由于 userWantToShare@gmail.com 具有该路径管理员角色,它应该允许插入。但很可能我忽略了一些东西,因为它根本不起作用。
我错过了什么?
提前致谢
【问题讨论】:
标签: flutter dart google-cloud-firestore firebase-security