Firestore 的安全规则问题

时间:2021-03-01 23:59:29

标签: firebase google-cloud-firestore firebase-security

这里我有一个关于firestore安全规则的小问题

我创建了一个在线记事本,我希望这些笔记只能由 groupId 标识的某些用户访问。我指定我还为我的城市指定了 groupId,以便能够将我识别的用户与我识别的城市进行匹配。 就我的代码而言,一切都很好,但在 Firestore 规则级别我没有成功。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users /{documents = **} {
        allow create,read,write : if request.auth != null
    }
    match /cities/{city}/citees/{citee}{
        allow read : if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.groupId == get(/databases/$(database)/documents/cities/$(city).data.groupId
        }
    }
}

2 个答案:

答案 0 :(得分:1)

其中存在语法错误。 ) 之后似乎缺少 $(city)。应该是这样的:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users /{documents = **} {
        allow create,read,write : if request.auth != null
    }
    match /cities/{city}/citees/{citee}{
        allow read : if get(/databases/$(database)/documents/users/$(request.auth.uid)).data.groupId == get(/databases/$(database)/documents/cities/$(city)).data.groupId
    }
  }
}

答案 1 :(得分:0)

是的,我可以访问数据库,因为当我尝试单独恢复我的“用户”集合时,我可以做到