首页 > 解决方案 > Firebase Firestore 安全规则:在安全规则中使用查询?

问题描述

我正在 Firebase 和 Firestore 上开发一个网络应用程序,这就是我想要做的事情:

下面是数据库结构:

 db.collection('posts').doc({postid})  
 //When a user writes a post, a new document is created. Includes the boolean 'public' field and the 'uid' field, which stores the writer's uid.
 db.collection('subscription').doc({viewer_user_id}) 
 //Once the logged-in user subscribes to another user, a document is created under the user's UID. The doc includes array of the UID of the users the viewer is subscribing.

以下是“posts”和“subscription”集合中每个文档下相关字段的描述:

posts/{postid}: {
  'uid': Writer's uid (String)
  'public': Boolean value to reflect visibility. If false, it is private.
}
subscription/{viewer's uid}: {
   'subscription': Array of uids of the users the viewer is subscribing.
}

所以,对于私有文档,基本思路是在订阅集合中查看查看者的文档,获取数据,并查看那里是否包含了作者的uid。这将需要一些 Javascript 代码,但我不知道 Firestore 安全规则中的语法是什么,或者一开始是否有可能。

 rules_version = '2';
 service cloud.firestore {
    match /databases/{database}/documents {
       function publicOrSubscribed() {
          return request.auth.uid!=null&&(resource.data.public==true|| /*What could be the syntax here??*/)
       }
     match /posts/{postid} {
         allow read: if publicOrSubscribed();
         allow create: if request.auth.uid!=null;
         allow update, delete: if  request.auth.uid==resource.data.userid;
     }
   }
}

有什么建议么?如果在安全规则中不可能,解决方法可能是什么?提前致谢!

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


推荐阅读