Firebase Cloud Firestore数据库规则数据访问被拒绝

时间:2018-10-18 21:39:01

标签: firebase google-cloud-firestore

我有一个与Cloud Firestore通信的应用程序,可根据其 uid 及其在当前设置下的正常运行来获取用户数据:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/thecorp/django/olympus/virt_env/lib/python3.6/site-packages/django/__init__.py", line 3, in <module>
    from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils.version'

但是很明显,这是公众无法接受的,并带有来自消防站的警告:

您的安全规则被定义为公共规则,因此任何人都可以窃取,修改或删除数据库中的数据

在下图中是我当前的数据库设置,该集合由为用户隐私隐藏的唯一用户 uid 组成...我要使用Firestore Rules完成的操作是允许uid只能以读写访问权限访问自己的集合uid

Firestore Database Structure 我尝试了以下规则,但无法访问数据:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}

感谢您为我指出编写规则的正确方法提供的帮助!我阅读了Firestore指南,但根本没有帮助!

非常感谢!

1 个答案:

答案 0 :(得分:1)

如果您希望用户读取/写入其收藏夹中的文档,请尝试以下操作:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // collection. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /{userId}/{userDocs} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

如果您有嵌套数据,并且希望用户访问其所有嵌套数据,请尝试:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // collection. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /{userId}/{userDocs=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}

文档: