【问题标题】:Firestore rules working in simulator but not on websiteFirestore 规则在模拟器中有效,但在网站上无效
【发布时间】:2018-08-16 14:52:11
【问题描述】:

我正在尝试为我的 Firestore 数据库创建一些规则。当我在 firebase 控制台的模拟器中运行这些规则时,一切正常。然而,当我部署规则并在我的网站上试用时,我在控制台中收到权限被拒绝错误。

我试图访问的数据是这样存储的:

1. /teams/{teamId}

2. /teams/{teamId}/boards/{boardId}

{teamId}{boardId} 是自动生成的 ID:

例如:

/teams/JTUrZcqz9Z20JuyCCcnV

/teams/JTUrZcqz9Z20JuyCCcnV/boards/OfcLPZItCk6Li7OeXwwt

我正在尝试遍历所有这些孩子。

Firestore 数据库:

firestore.rules:

 match /teams/{teamId} {
   allow read: if isMemberOfTeam(teamId);
   allow write: if true; // memberOfTeam(teamId);

  match /boards/{boardID} {
   allow read: if memberOfTeam(teamId) || boardIsPublic(teamId);
   allow write: if memberOfTeam(teamId);

    match /{document=**} {
     allow read: if memberOfTeam(teamId) || boardIsPublic(teamId);
     allow write: if memberOfTeam(teamId);
     }
  }
  match /{document=**} {
   allow read: if isMemberOfTeam(teamId); // memberOfTeam(teamId);
   allow write: if true; // memberOfTeam(teamId);
  }
 }

function isMemberOfTeam(teamId) {
    return  get(/databases/$(database)/documents/teams/$(teamId)).members[request.auth.uid].isMember;
}
function boardIsPublic(teamId) {
    return get(/databases/$(database)/documents/teams/$(teamId)).data.isPublic;
}

网站代码: Firestore 查询:

ref.where('members.' + user.uid + '.isMember', '==', true)

所有代码:

this.teamsCollection = this.auth.user$.filter(user => user != null)
  .map(user => this.afs.collection<TeamsInterface>('teams', ref => ref.where('members.' + user.uid + '.isMember', '==', true)))
  .shareReplay(1);
this.$teams = this.teamsCollection.switchMap(collection => collection.snapshotChanges().map(actions => {
  return actions.map(a => {
    const data = a.payload.doc.data() as TeamsInterface;
    data.id = a.payload.doc.id;
    return data;
  });
}));

我在控制台中遇到错误:

权限缺失或不足。

【问题讨论】:

  • 请附上您用来测试的代码。

标签: firebase google-cloud-firestore


【解决方案1】:

我不知道为什么,但这段代码一切正常:

match /teams/{teamId} {
      allow read: if isMemberOfTeam(existingData());
      allow update: if isMemberOfTeam(existingData()); // memberOfTeam(teamId);
      allow create: if isSignedIn();

      match /archived/{boardID} {
        allow read: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data) 
              || boardIsPublic(existingData());
        allow write: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data);
      }

      match /boards/{boardID} {
        allow read: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data) 
              || boardIsPublic(existingData());
        allow write: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data);

        // match /beta {
        //   allow write: if memberOfTeam(teamId) || boardIsPublic(teamId);
        // }

        // All subcollection
        match /{document=**} {
          allow read: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data) 
                || boardIsPublic(get(/databases/$(database)/documents/teams/$(teamId)).data);
          allow write: if isMemberOfTeam(get(/databases/$(database)/documents/teams/$(teamId)).data);
         }
      }

     }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多