【问题标题】:Unable to create proper rule for Firebase sub collections and roles无法为 Firebase 子集合和角色创建正确的规则
【发布时间】:2020-04-23 21:11:05
【问题描述】:

我正在创建一个具有 firebase 集成的 ionic 应用程序,其中我在应用程序中创建了一个帖子部分。用户可以像 facebook 这样写帖子,任何经过身份验证的用户都可以对帖子发表评论, 以下是我如何存储数据的数据库设计:-posts/{postsDocument}/cmets/{cmetsDocument} Comments 是 postsDocument 的子集合,其中包含特定帖子的所有 cmets。 postDocument 和 cmetsDocument 还包含用户的 Firebase uid。 我已经写了规则,任何用户都可以评论帖子并阅读其他 cmets,但我想要用户可以更新自己的评论而不是其他人的规则,并且只有帖子所有者可以删除评论。 检查下面的规则。请帮我解决我所缺少的

match /posts/{postId} {
  allow read, write: if request.auth.uid != null;
  match /{comments}/{commentId} {
    allow read, create: if request.auth.uid != null;
    allow update: if ;
    allow delete: if ;
  }
}

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    根据您的情况检查以下规则

        match /posts/{postId} {
          allow read, write: if request.auth.uid != null;
          match /{comments}/{commentId} {
            allow read, create: if request.auth.uid != null;
            allow update: if resource.data.commentOwnderId == request.auth.uid;//commentOwnderId, firebase uid of the user who wrote the comment
            allow delete: if get(/databases/$(database)/documents/posts/$(postId)).data.postOwnerId == request.auth.uid;//postOwnerId, firebase uid of the user who wrote the post
          }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-11-14
      • 1970-01-01
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-01
      • 2020-07-01
      • 1970-01-01
      相关资源
      最近更新 更多