Google cloud firestore Firestore规则-使用存在于何处

Google cloud firestore Firestore规则-使用存在于何处,google-cloud-firestore,firebase-security,Google Cloud Firestore,Firebase Security,在我的项目中,我有firestore用户和类的集合。每个用户都可以是一个或多个类的一部分。类文档具有属性成员,该属性成员是一个数组,包含该类中的所有用户UID 例如: users documents: doc.id: USER1UID { name: 'user1', email: 'user1@user1.com', phone: '+123 456 789 001' } doc.id: USER2UID { name: 'user2', email: 'user2@user2.com',

在我的项目中,我有firestore用户和类的集合。每个用户都可以是一个或多个类的一部分。类文档具有属性成员,该属性成员是一个数组,包含该类中的所有用户UID

例如:

users documents:
doc.id: USER1UID
{ name: 'user1', email: 'user1@user1.com', phone: '+123 456 789 001' }

doc.id: USER2UID
{ name: 'user2', email: 'user2@user2.com', phone: '+123 456 789 002' }

doc.id: USER3UID
{ name: 'user3', email: 'user3@user3.com', phone: '+123 654 789 003' }

classes documents:
doc.id: ABCDEF
{ name="class1", members: ['USER1UID', 'USER2UID'] }

doc.id: GHIJKL
{ name="class2", members: ['USER1UID', 'USER3UID'] }

doc.id: MNOPQR
{ name="class3", members: ['USER3UID'] }
我需要编写一个规则,允许用户仅在其他用户在同一个类中时读取有关其他用户的详细信息。每个用户也可以读取自己的配置文件

在这种情况下,user1可以读取user2和user3的详细信息。(他们一起在一班和二班)。 User2只能读取user1的详细信息(它们一起在class1中)。 User3只能读取user1的详细信息(它们一起在类2中)

我需要像这样的东西:

match /users/{userId} {
   allow read: 
     //user is logged in
     if request.auth != null 
     && (
            //user can read own profile
            request.auth.id == $(userId)

            //there is a class where are both (requesting and requested) users
            || exists( (/databases/$(database)/documents/classes/).where(request.auth.id in members).where($(userId) in members)
        )
}

由于安全规则不允许您执行查询,所以您试图对数据库架构执行的操作是不可能的。您一次只能使用已知路径请求一个文档,每个规则执行最多10个文档


您可以改为管理每个用户文档,使其包含与该用户具有相同类的所有其他用户的列表。但是您必须编写一些代码来保持最新,因为课程的花名册可能会随着时间的推移而变化。这可能是云功能的一个很好的用途。

谢谢Doug。从安全角度来看,这是一个风险,因为用户可以编辑自己的配置文件,从而将其他用户添加到列表中。因此,我可能需要再创建一个只能从firebase admin SDK写入的集合(规则中为“if false”),并使用firestore触发器更新列表。对于这样简单的用例,解决方案相当困难(而且昂贵)。有没有其他方法可以做到这一点?可能禁用直接数据库读取,并使用云函数读取、检查和返回数据?或者切换到实时数据库?当然,您可以通过云功能强制所有请求。我不认为改变数据库产品会有帮助。