【问题标题】:Firebase - Fetch Data Without Secure Nodes?Firebase - 在没有安全节点的情况下获取数据?
【发布时间】:2017-03-08 18:29:45
【问题描述】:

使用 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 中,那么您可以按指定读/写。

但是,我现在的问题变成了,如果作为一名培训师,我想查看我有权阅读的所有客户的列表。

如何获取客户?尝试仅对 clients 进行数据读取/获取失败。

【问题讨论】:

  • 这些权限在模拟器中真的对你有用吗?我无法让他们工作。
  • 不,不适合我。我正在关注一个示例,如下所示:firebase.googleblog.com/2016/10/… 我知道它不是一个适合所有人的答案,我问的原因是因为我无法找到一个可行的解决方案,或者不知道它是如何工作的。

标签: ios objective-c firebase firebase-realtime-database firebase-security


【解决方案1】:

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

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.
    }];
  }
}];

【讨论】:

  • 添加了 Obj C 查询。
猜你喜欢
  • 2020-04-13
  • 1970-01-01
  • 2020-12-03
  • 2022-01-05
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2017-07-24
相关资源
最近更新 更多