保护多个用户Firestore的数据(Securing data for multiple users Firestore)

编程入门 行业动态 更新时间:2024-10-27 04:38:08
保护多个用户Firestore的数据(Securing data for multiple users Firestore)

我如何设置Firestore的规则来限制只有某些用户以下列方式访问某些集合文档/集合:

//base collection
clients:{
  //document
  client-1-store:{
    users-allowed:[
      "user-1-id",
      "user-2-id",
      "user-3-id",
      "user-4-id",
    ]
  }
} 
  
 

事情是,如果当前登录用户的ID设置在每个“商店”的users-allowed数组内,则我只想允许读写“client-1-store”文档及其文档和子集合,但我不知道我该怎么做......

How could I set rules for Firestore to restrict access only for some users to certain collections documents/collection in the following manner:

//base collection
clients:{
  //document
  client-1-store:{
    users-allowed:[
      "user-1-id",
      "user-2-id",
      "user-3-id",
      "user-4-id",
    ]
  }
} 
  
 

The thing is I'd like to only allow reading and writing to the "client-1-store" document and its documents and sub collections if the current logged in user's id is set inside the users-allowed array of each "store", but I have no idea of how i could do that...

最满意答案

而不是创建一个用户ID数组,创建一个值的映射

//base collection clients:{ //document client-1-store:{ users-allowed:{ "user-1-id": true, "user-2-id": true, "user-3-id": true, "user-4-id": true, } } } match /clients/{storeId=**} { allow read: if get(/databases/$(database)/documents/clients/$(storeId).data.users-allowed.$(request.auth.uid)) == true; }

但是,这不会扩展为允许使用者子集合,并将每个用户标识另存为文档。 相反,您可以使用exists条件。 这还允许您为用户的文档添加进一步的粒度以获取详细的CRUD权限。

Instead of creating an array of user IDs, create a map of values

//base collection clients:{ //document client-1-store:{ users-allowed:{ "user-1-id": true, "user-2-id": true, "user-3-id": true, "user-4-id": true, } } } match /clients/{storeId=**} { allow read: if get(/databases/$(database)/documents/clients/$(storeId).data.users-allowed.$(request.auth.uid)) == true; }

However, this doesn't scale as well as having an allowedUsers sub-collection, with each user ID saved as a document. You can then use the exists condition, instead. This also allows you to add further granularity to the user's document for detailed CRUD permissions.

更多推荐

本文发布于:2023-07-26 02:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1269898.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数据   用户   Firestore   users

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!