Firebase - 在没有安全节点的情况下获取数据?

Firebase - Fetch Data Without Secure Nodes?

如何使用 Firebase 获取一个节点,其中一些子节点是安全的?

例如...

数据结构:

root: {
    clients: {
        c1: { 
            data: {
                name: "person1"
            }
            permissions: {
                clientId: "abc"
                trainerId: "123"
            }
        }
        c2: { 
            data: {
                name: "person2"
            }
            permissions: {
                clientId: "def"
                trainerId: "123"
            }
        }
    }
}

安全:

"clients": {
          "$clientKey": {
                "data": {
                    ".read": "data.parent().child('permissions').child('clientId').val() == auth.token.name || data.parent().child('permissions').child('trainerId').val() == auth.uid",
                    ".write": "data.parent().child('permissions').child('trainerId').val() == auth.uid"
              }
          }
        }

所以我想要实现的是以下内容;客户端可以读取自己的数据。培训师可以读写他们的任何客户。如果您的 idpermissions 中,那么您可以 read/write 按照指定。

但是,我现在的问题是,作为培训师,我想查看我有权阅读的所有客户的列表。

如何获取客户?尝试在 clients 上做数据 read/fetch 失败。

我会像这样构建您的数据库:

root: {
  clients: {
    abc: { 
        data: {
            name: "person1"
        },
        trainer: {
            123: true
        }
    },
    def: { 
        data: {
            name: "person2"
        },
        trainer: {
            123: true
        }
    }
  },
  trainers: {
    123: {
      clients: {
        abc: true,
        def: true
      }
    }
  }
}

具有以下规则。 我认为在 client_id 对象上强制执行读写规则会更容易,如下所示:

{
  "rules": {
    "clients" : {
      "$client_id" : {
        ".read": "auth.uid === $client_id || data.child('trainer/'+auth.uid).exists()",
        ".write" : "data.child('trainer/'+auth.uid).exists()"
      }
    },
    "trainers": {
      "$trainer_id": {
        ".read": "auth.uid === $trainer_id || data.child('clients/'+auth.uid).exists()",
        ".write": "auth.uid === $trainer_id"
      }
    }
  }
}

要获取培训师的客户,您首先要获取培训师的节点,然后为客户节点下的每个客户获取客户信息。

正在获取一个教练手下所有客户的信息(我不知道 Obj C,但我认为这应该可行。)

[[self.trainersRef child:[NSString stringWithFormat:@"%@/clients", userId]] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
  for ( FDataSnapshot *child in snapshot.children) {
    [[self.clientsRef child:[NSString stringWithFormat:@"%@/data", child.key]] observerEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull dataSnapshot) {
      // Client info available here.
    }];
  }
}];