如何在Firebase数据库中保护用户和管理员的访问权限?

时间:2018-06-28 12:55:17

标签: javascript firebase-realtime-database firebase-security-rules

我正在使用Redux-Saga作为中间件。我正在通过查询将参数传递给Firebase数据库,但无法在数据库端访问它。

查询:::

database.ref("workouts")
.child(userId)
.once("value")
.then(snapshot => {
 console.log("onSuccess:", snapshot.ref, snapshot.val());
 resolve(snapshot.val());
 })
.catch(function(error) {
 console.log("Error fetching document: ", error);
 reject(error);
 });

UserId是我从localStorage提取的值,并使用“ .child(userId)”通过查询发送到数据库。

查询:: :(对于管理员)

database.ref("workouts")
.once("value")
.then(snapshot => {
 console.log("onSuccess:", snapshot.ref, snapshot.val());
 resolve(snapshot.val());
 })
.catch(function(error) {
 console.log("Error fetching document: ", error);
 reject(error);
 });

数据库中的规则::::

{
"rules": {
         "workouts": {
          // grants write access to the owner of this user account || the user role is equal to  admin
          // grants read access to the owner of this user account || the user role is equal to  admin
".read":"(data.exists() && auth.uid != null && data.child(auth.uid).exists()) ||root.child('users').child(auth.uid).child('role').val() == 'admin'",
".write":"data.exists() ||root.child('users').child(auth.uid).child('role').val() == 'admin'"
                     }
         }
}

我已经尝试过[query.equalTo]和[data.child(auth.uid).val()]方法来访问该值,但是没有任何结果。

JSON for Workouts :::::

"workouts" : {
"6OiasllKwVSjjRfrCarMAjhkKAH2" : {
  "-LD3nNIKw9Yk3HcoAL0-" : {
         "exercises" : [ {
          "muscleGroup" : "Chest",
          "name" : "Incline Dumbbell Fly",
          "sets" : [ 0, 0, 0, 0, 0 ],
          "type" : "reps"
        } ],
        "name" : "Force Set",
        "reps" : [ "5", "5", "5", "5", "5" ],
        "type" : "Weights"
       }]
    },
    "workoutName" : "My Test workout"
  }

用户的JSON :::::

 "users" : {
     "6OiasllKwVSjjRfrCarMAjhkKAH2" : {
     "email" : "testuser@gmail.com",
     "role" : "user",
     "uid" : "6OiasllKwVSjjRfrCarMAjhkKAH2"
       }
     }

任何帮助都将受到高度赞赏。

非常感谢您。

编辑::::添加了对admin的查询。对于管理员,我想获取集合中的所有可用数据。

1 个答案:

答案 0 :(得分:4)

我想我知道出了什么问题。您的JSON似乎在/workouts/$uid下为用户提供了所有锻炼。您的规则试图使用户访问所有/workouts,而不仅仅是他们自己的访问权限。

解决方案是将规则下移一级到树中:

{
  "rules": {
    "workouts": {
      // grants access to the owner of this user account || the user role is equal to  admin
      "$uid": {
        ".read":"auth.uid == $uid || root.child('users').child(auth.uid).child('role').val() == 'admin'",
      },
      ".write":"data.exists() || root.child('users').child(auth.uid).child('role').val() == 'admin'"
    }
  }
}

另请参阅documentation on securing user data,其中有一个很好的示例。

更新:如果您希望允许管理员阅读/workouts,并且每个用户都可以在/workouts/$uid下阅读自己的锻炼,那么您需要遵循以下规则:

{
  "rules": {
    "workouts": {
      // grants access to the owner of this user account
      "$uid": {
        "read": "auth.uid == $uid",
      },
      // grants access to the admin
      ".read": "root.child('users').child(auth.uid).child('role').val() == 'admin'",
      ".write": "data.exists() || root.child('users').child(auth.uid).child('role').val() == 'admin'"
    }
  }
}
相关问题