Firestore规则-文档角色列表

时间:2019-11-19 15:22:37

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

我有一个具有以下结构的收藏项目:

{
  name
  roles {
    id_user1: 'owner',
    id_user2: 'read'
  }
}

现在,我希望“所有者”用户获得项目列表。我尝试设定规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /projects/{project} {
      function isSignedIn() {
        return request.auth != null;
      }

      function getRole(rsc) {
        return rsc.data.roles[request.auth.uid];
      }

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

      // <-- 'Read' rules here -->
      allow read: if isOneOfRoles(resource, ['owner', 'writer', 'commenter', 'reader']);

      match /records/{record} {
        allow create, read, write: if request.auth.uid != null
      }
   }
    match /users/{userId}{
        allow create
      allow read: if request.auth.uid != null
      allow write: if request.auth.uid == userId
    }
    match /notifications/{notification}{
        allow read, write: if request.auth.uid != null
    }
  }
}

如果我使用“所有者”用户访问单个项目,则说明成功。 但是当我访问由该用户创建的项目列表时,没什么。

我的反应代码:

const mapStateToProps = (state) => {
  debugger
  return {
    projects: state.firestore.ordered.projects,
    auth: state.firebase.auth,
    // notifications: state.firestore.ordered.notifications
  }
}

export default compose(
  connect(mapStateToProps),
  firestoreConnect([
    { collection: 'projects', orderBy: ['createdAt', 'desc']},
    // { collection: 'notifications', limit: 8, orderBy: ['time', 'desc']}
  ])
)(Dashboard)

当我调试并检查state时,我看到了:

state: {
  auth: {authError: null}
  ...other...
}

如何使用Firestore规则按用户查询项目列表?

0 个答案:

没有答案