【问题标题】:Security rules in FirebaseFirebase 中的安全规则
【发布时间】:2016-03-26 19:31:21
【问题描述】:

我有类似的数据库结构

 appointments
     [$userId]
         [$appointmentId]
              message:"something"
              date:"14/12/2015"
 users
    [$userId]
        name: Hardik
        email: hardikmsondagar@gmail.com

我正在使用 Firebase 的 angularfire 库,我正在尝试限制基于 uid 的读取操作(意味着创建约会的人只能读取它)。我已经尝试遵循安全规则

{
  "rules": {
    "appointments": {
      "$userId":{
        "$appointmentId":{
         ".read": "auth.uid==$userId",
         ".write": true
        }
      }
    },
    "users": {
      "$userId":
        {
          ".read": "auth!=null && $userId === auth.uid",
          ".write": "auth!=null && $userId === auth.uid"
        }
    }
  }

但最终出现此错误 错误:permission_denied:客户端无权访问所需数据。

我正在尝试使用以下代码访问所有用户的约会

 var ref = new Firebase("https://<FIREBASE-APP>.firebaseio.com/appointments/"+uid);
 $scope.appointments = $firebaseArray(ref);

【问题讨论】:

    标签: firebase firebase-security


    【解决方案1】:

    $uid 通配符设置规则,以读取所有子项。

    "appointments": {
      "$uid":{
        ".read": "auth.uid == $uid",
        ".write": "auth.uid == $uid",
      }
    }
    

    $uid 通配符设置整个列表的权限,而$appointmentId 通配符设置每个单独项目的权限。

    但是安全规则是级联的,所以你只需要设置最顶层的规则。

    Read the docs on cascading for more information.

    【讨论】:

    • 非常感谢。现在我明白这是我以错误方式执行的读取操作。
    猜你喜欢
    • 2016-06-27
    • 1970-01-01
    • 2016-08-19
    • 2020-09-06
    • 2016-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多