如何为 Angular 和 Firestore 中定义为 Map 对象的角色编写查询?

问题描述 投票:0回答:1

我正在使用@angular/fire,并且我有一个具有以下模式的寺庙集合:

{
 name: 'Sri Chamundeshwari temple',
 address: 'Chamundi hills, Mysore',
 roles: {
  '[email protected]': 'owner',
  '[email protected]': 'admin',
  '[email protected]': 'member',
  '[email protected]': 'viewer'
 }
}

我只希望角色对象中的 4 个用户能够访问 Temple 文档。应拒绝其他用户访问。

我按照此链接中的指南进行操作: https://firebase.google.com/docs/firestore/solutions/role-based-access 并构建了我的安全规则如下:

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    
    match /temples/{temple} {
    
      function isSignedIn() {
        return request.auth != null;
      }
    
      function getRole(rsc) {
        return rsc.data.roles[request.auth.token.email];
      }

      function isOneOfRoles(rsc, array) {
        return isSignedIn() && getRole(rsc) in array;
      }

      allow read: if isOneOfRoles(resource, ['owner', 'admin', 'member', 'viewer']);
    }
  }
}

我应该如何编写合适的查询来获取登录用户可以访问的所有寺庙文档?

该指南没有描述任何有关查询的内容。

我尝试了以下查询,但它们给出了权限错误。

假设集合中有 10 座寺庙。

用户 A 在 3 个寺庙中担任查看者角色。

我期望以下查询应该只返回用户 A 可以访问的 3 个寺庙。

const q = query(
     collectionGroup(this.fireStore, "temples"), 
     where(`roles.${this.auth.currentUser?.email}`, 'array-contains', ['owner', 'administrator', 'member', 'viewer'])
);
const q = query(
        collection(this.fireStore, "temples"),
        where(`roles.${this.auth.currentUser?.email}`, 'array-contains', ['owner', 'administrator', 'member', 'viewer'])
);
firebase google-cloud-firestore firebase-security angularfire2
1个回答
0
投票

您在

array-contains
上使用
roles.${this.auth.currentUser?.email}
运算符,但该字段不是数组。

要筛选非数组字段是否具有多个值之一,请使用 in

 运算符

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.