我有一款使用Firestore进行多人游戏保存的游戏。
基本上每个文档都采用这种格式($表示20个字符的p随机ID)
$savegame = {
users = [$user1, $user2]
info = {turn: 2, }
gamestate = {longjsonstring}
}
我使用
查询数据firebase.firestore().collection('savegamescollection').where('users', 'array-contains', firebase.auth().currentUser.uid)
我想使用Firestore规则来仅允许游戏中的人更新保存,我找不到找到检查Firestore规则中的用户数组的方法。
所以我所做的是(>表示集合)
savegame = {
> usercol = { $user1, $user2 } - line A
users = [user1, user2] - line B
...
}
我使用规则
match /savegamescollection/{doc} {
allow read, write: if exists(/databases/$(database)/documents/savegamescollection/$(doc)/usercol/$(request.auth.uid));
这确实有效,但是它有很多麻烦和丑陋。 我将相同的数据复制为数组中的元素和文档ID。
有没有一种方法可以在数据结构(上面的A和B)中使用这些行之一来允许查询和规则都起作用。
谢谢
答案 0 :(得分:1)
如果您要强制用户必须使用包含数组的查询仅请求列出用户的UID的文档,则可以在列表字段上使用hasAny来检查其UID是否在列表。
match /savegamescollection/{doc} {
allow read, write: if resource.data.users.hasAny([request.auth.uid]);
}