Javascript Firestore规则:如何检查集合中所有文档的地图内容?
我正在尝试完成以下功能:Javascript Firestore规则:如何检查集合中所有文档的地图内容?,javascript,firebase,google-cloud-firestore,firebase-security,Javascript,Firebase,Google Cloud Firestore,Firebase Security,我正在尝试完成以下功能: 用户可以加入匹配 如果他们还没有一场比赛 如果他们还没有参加比赛,他们就会试图加入 如果它们不在matches集合中的任何其他匹配项中 我在最后一个问题上苦苦挣扎。我试过这个: !exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid)) // if the user is not already in another match …但它似乎不起作用
- 用户可以加入匹配
- 如果他们还没有一场比赛
- 如果他们还没有参加比赛,他们就会试图加入
- 如果它们不在matches集合中的任何其他匹配项中
!exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid)) // if the user is not already in another match
…但它似乎不起作用
以下是我的规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /userAuthData/{uid}/{documents=**} {
allow read: if false
allow write: if false
}
match /userData/{uid}/{documents=**} {
allow read: if request.auth != null && request.auth.uid == uid
allow write: if false
}
function allowCreateMatch(uid) {
return request.auth != null
&& exists(/databases/$(database)/documents/userData/$(request.auth.uid)) // if the user exists
&& !exists(/databases/$(database)/documents/matches/$(request.auth.uid)) // if the user does not have their own match already
&& !exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid)) // if the user is not already in another match
&& request.resource.data.maxPlayers == 2 // if the match is set to have 2 players (1v1 is the default for now)
&& request.resource.data.state == 1 // if the match is set to a state of WAITING_FOR_PLAYERS
}
function allowJoinMatch(uid) {
return request.auth != null
&& exists(/databases/$(database)/documents/userData/$(request.auth.uid)) // if the user exists
&& !exists(/databases/$(database)/documents/matches/$(request.auth.uid)) // if the user does not have their own match already
&& !exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid)) // if the user is not already in another match
&& !(request.auth.uid in resource.data.players) // if the user is not already in the match
&& resource.data.players.keys().toSet().size() < resource.data.maxPlayers // if there is room left based on the max number of players
&& resource.data.state == 1 // if the match is in a state of WAITING_FOR_PLAYERS
&& resource.data.maxPlayers == request.resource.data.maxPlayers // if there max players value is the same
&& request.resource.data.players.keys().toSet().size() + 1 == resource.data.maxPlayers
? request.resource.data.state == 2 // the match is should be set to a state of READY_TO_START
: request.resource.data.state == 1 // the match is should be set to a state of WAITING_FOR_PLAYERS
}
function allowDeletion() {
return request.auth.uid == resource.id
&& resource.data.state == 1
}
match /matches/{uid}/{documents=**} {
allow read: if true
allow create: if allowCreateMatch(uid)
allow update: if allowJoinMatch(uid)
allow delete: if allowDeletion()
}
}
}
rules_version='2';
服务云.firestore{
匹配/databases/{database}/documents{
匹配/userAuthData/{uid}/{documents=**}{
允许读取:如果为false
允许写入:如果为false
}
匹配/userData/{uid}/{documents=**}{
允许读取:if request.auth!=null&&request.auth.uid==uid
允许写入:如果为false
}
函数allowCreateMatch(uid){
return request.auth!=null
&&存在(/databases/$(database)/documents/userData/$(request.auth.uid))//如果用户存在
&&!exists(/databases/$(database)/documents/matches/$(request.auth.uid))//如果用户还没有自己的匹配项
&&!exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid))//如果用户尚未处于另一个匹配中
&&request.resource.data.maxPlayers==2//如果匹配设置为有2名玩家(目前默认为1v1)
&&request.resource.data.state==1//如果比赛设置为等待球员的状态
}
函数allowJoinMatch(uid){
return request.auth!=null
&&存在(/databases/$(database)/documents/userData/$(request.auth.uid))//如果用户存在
&&!exists(/databases/$(database)/documents/matches/$(request.auth.uid))//如果用户还没有自己的匹配项
&&!exists(/databases/$(database)/documents/matches/$(uid)/players/$(request.auth.uid))//如果用户尚未处于另一个匹配中
&&!(resource.data.players中的request.auth.uid)//如果用户不在比赛中
&&resource.data.players.keys().toSet().size()
使用安全规则,除了简单的单个文档get()
之外,不可能执行查询。不允许查询未知数量的文档,因为它们不会按照安全规则要求的方式进行扩展
如果您不能用获取单个文档的方式来表达您的规则,那么您需要以某种方式更改数据以实现这一点,或者通过可以执行所需查询的某个后端发送您的写操作。您对如何重新标记我的数据有何建议?我很容易做到,但不知道如何解决我的问题。你将如何实现一个模型是另一个模型的成员的概念,并具有上述限制?根据你在这里所描述的,我不知道。那完全取决于你。如果你尝试了一些东西,但没有达到预期效果,请发布一个新问题。对于这个问题,底线是您的规则不起作用,因为它实际上不能像我所描述的那样进行查询。