android - Android 应用的 Firestore 安全设置应该是什么?
问题描述
我刚刚创建了我的第一个Android
应用程序Kotlin
作为FIrestore
数据库,我也使用了电话身份验证。创建数据库时,我使用了测试模式,现在我想将我的应用程序上传到 Google Play,以便公众可以开始使用我的应用程序。我应该考虑更改的 Firestore 的安全设置是什么?请对此提出一些建议。
目前,这是“规则”选项卡下的内容。
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if
request.time < timestamp.date(2021, 8, 29);
}
}
}
解决方案
根据我从评论中的示例中了解到的情况,您有 7 个集合(示例)A、B、C、P、Q、Y 和 Z。
收藏 | 可以通过 |
---|---|
甲、乙、丙 | 某些用户 |
磷,问 | 通过电话身份验证的用户 |
Y, Z | 某些用户 |
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /A/{docId} {
allow read, write: if isCertainUser();
}
// Same rule for B and C
match /P/{docId} {
allow read, write: if request.auth != null && request.auth.token.firebase.sign_in_provider == "phone";
}
// Same rule for Q
// Checks if user is logged in by Phone auth
match /Y/{docId} {
allow read, write: if isCertainUser();
}
// Same rule for Z
}
}
您必须编写一个函数isCertainUser
才能使规则起作用。现在有多种方法可以指定用户具有访问权限。例如,您可以添加管理员自定义声明或将可以访问该集合的用户的文档存储在单独的集合中,并检查请求数据的用户文档是否存在于该白名单集合中。
match /A/{docId} {
allow read: if request.auth != null && exists(/databases/$(database)/documents/whitelisted/$(request.auth.uid));
}
此规则将允许用户读取集合 A 中的数据,仅当“白名单”集合中存在以用户 UID 作为文档键的文档时。
参考:
推荐阅读
- javascript - 使用 AJAX 从 Flask 渲染整个 HTML 页面
- javascript - Braintree / Javascript:如何获取交易数据(卡和地址)以预填充以前交易的客户?
- reactjs - 使用水合物时,为什么我们需要匹配服务器和客户端中的classNames,以及如何?
- c++ - 程序在尝试创建 ofstream 或 fopen 时崩溃
- php - 使用时如何设置导航栏选项卡处于活动状态?
- excel - Excel 中具有多个条件的 IF、AND、OR 函数
- python - 误解gensim中filter_extreme的使用
- android - 启动弹出活动后如何从后台活动中完全移除焦点
- symfony - Symfony3 在深度链接中添加语言环境
- mysql - 如何从 JOIN 创建多表子查询?