Firebase 中的安全规则

Security rules in Firebase

我的数据库结构像

 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);

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

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

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

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

Read the docs on cascading for more information.