首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >云修复安全规则文档示例

云修复安全规则文档示例
EN

Stack Overflow用户
提问于 2018-08-17 20:56:33
回答 1查看 662关注 0票数 0

此文档页:编写云修复安全规则的条件,如下所示:

另一个常见的模式是确保用户只能读写自己的数据。

并提供此示例:

代码语言:javascript
复制
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

我不明白为什么create规则不是用与其他规则if request.auth.uid == userId相同的条件定义的,而是用if request.auth.uid != null定义的。据我所知,有了这个规则,任何用户都可以在users中创建任何文档,但除非它与他的uid匹配,否则不能使用它做任何事情。那么,为什么要允许它呢?

EN

回答 1

Stack Overflow用户

发布于 2018-08-18 04:21:47

让我们讨论可以实现的非常基本的安全规则(通过用户身份验证):

代码语言:javascript
复制
allow read, update, delete: if request.auth.uid != null;
allow create: if request.auth.uid != null;

任何用户都可以删除其他人创建的文档。因此,为了限制/控制它,我们实现了所提供的代码片段。

代码语言:javascript
复制
service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

代码片段只是一个示例用例,它使用不同的条件作为参考,因为这是教程/指南,因此Firebase团队尝试将尽可能多的条件匹配到代码片段中。

当然,您可以执行allow create: if request.auth.uid == userId;来严格限制特定用户。

我希望它能给你一些想法!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51902762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档