首页 > 解决方案 > Firestore 安全规则 - 嵌套集合

问题描述

我正在尝试为我的 firestore 实例创建安全规则。这是集合的数据结构和要求。

用户收藏

/users/{userId}
{
 id:userId
 isAdmin:bool
},

新闻集锦

/news/{docId}
{
 id:docId,
 userId:uid,
 viewCount:number,
 commentsCount:number,
 upVotes: number,
 comments: collection
}

评论收藏

/news/comments/{commentId}
    {
     id:commentId,
     userId:uid,
    }

新闻采集要求:

只有将 isAdmin 标志设置为 true 的用户才能创建、更新和删除此文档,但 viewCount 和 commentsCount 除外,如果满足以下任一条件,则可以更新。

  1. 任何经过身份验证或未经过身份验证的用户查看文档
  2. 如果只有经过身份验证的用户发表了评论

/news/{docId}/comments 集合的要求:

所有通过身份验证或未通过身份验证的用户都可以查看评论
只有经过身份验证的用户才能创建评论。
只有具有 uid == userId 的用户才能删除或更新文档

到目前为止我的努力。

  match /news/{docId}{  
    allow read;  
    //we need to improve the below statement  
    allow write: if Authenticated() && request.auth.uid == 'adminUserId';  
    match /comments/{commentId}{  
        allow read;  
        allow create: if Authenticated() && request.auth.uid == request.resource.data.userId;  
        allow update,delete: if Authenticated() && request.auth.uid == resource.data.userId;  
    }  
  }  

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


推荐阅读