Firestore阻止的用户规则

问题描述 投票:1回答:2

我一直在寻找答案,

仅是我有一个Android应用程序,该应用程序允许用户向Firestore数据库中写入数据或从中读取数据>

我有一个名称为BlockList的集合,该集合用于将用户uid作为文档名和一个名为userUid的字段值,该集合的目的是拒绝行为不当的用户执行任何“仅写”请求在应用程序中。换句话说,我正在寻找Firestore规则,以允许BlockList中的用户读取only

在应用程序中共享的内容,以及deny他们尝试进行的所有写操作。

我已经测试了这些规则,但是它不起作用,即使用户不在BlockList上,它也不允许任何读取或写入操作]] >>

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
   function isBlackListed() {
      return exists(/databases/$(database)/BlockList/$(request.auth.uid))
    }
    match /{document=**} {
      allow read, write: if request.auth != null && !isBlackListed();
    }
  }
}


----------


rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
         allow write: if !exists(/databases/$(database)/documents/BlockList/$(request.auth.uid))

  }
}

我一直在寻找答案,现在我已经有了一个Android应用程序,该应用程序允许用户向Firestore数据库读写数据,而我有一个名为...的集合”]

有几种解决此问题的方法,这是一种基于已有知识的策略(用户文档的集合,其中包括要阻止其写访问的用户的uid)。

前端解决方案

[当用户通过您的应用进行身份验证时,发送请求以查看该uid是否也在BlockList集合中。如果确实存在,请在允许执行调用之前,在您的应用程序中设置所有数据库调用都引用的布尔标志。

后端解决方案

由于前端中没有什么是安全的,因此您需要将前端验证与后端安全性结合在一起。

用户正在尝试进行的更改可以在'触发文档'中进行,由后端的云功能然后执行onWrite

您的云函数然后可以查询BlockList集合以查看uid是否确实在该集合中,如果是,则在阻止任何写操作的同时向客户端发送一个错误(或不发送)给客户端。

不知道我是否正确解决了您的问题,了解您到目前为止尝试过的规则可能会有所帮助,但是我认为这应该可行:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      // Make sure a write to all documents is only allowed when
      // the current user has no document in BlockList collection
      allow write: if !exists(/databases/$(database)/documents/BlockList/$(request.auth.uid))
    }
  }
}

我已经在我的firestore实例中对此进行了测试,对我来说看起来还不错。当然,您很可能需要更好地指定文档,而不是通配符匹配。

只要在BlockList集合中有一个以userUid作为文档名称的文档,您在BlockList文档中拥有什么数据都没有关系。

我在BlockList集合中创建了两个文档(代表用户)。

two users in BlockList

现在,我尝试通过模拟器写入具有提到的security.rule的锦标赛表。

blocked by rule because user is in BlockList

当从BlockList中删除用户“ 8Zvq1fpWl2S0h1t7bIxNoxDcucn1”并再次使用Simulator时,我得到:]

allowed by second rule

摘要:对我来说,它看起来工作得很好。

android firebase kotlin google-cloud-firestore firebase-security-rules
2个回答
0
投票

有几种解决此问题的方法,这是一种基于已有知识的策略(用户文档的集合,其中包括要阻止其写访问的用户的uid)。

前端解决方案


0
投票

不知道我是否正确解决了您的问题,了解您到目前为止尝试过的规则可能会有所帮助,但是我认为这应该可行:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      // Make sure a write to all documents is only allowed when
      // the current user has no document in BlockList collection
      allow write: if !exists(/databases/$(database)/documents/BlockList/$(request.auth.uid))
    }
  }
}

我已经在我的firestore实例中对此进行了测试,对我来说看起来还不错。当然,您很可能需要更好地指定文档,而不是通配符匹配。

© www.soinside.com 2019 - 2024. All rights reserved.