如何保护用户和管理员在Firebase数据库中的访问? [英] How to secure access for user and admin in Firebase Database?

查看:18
本文介绍了如何保护用户和管理员在Firebase数据库中的访问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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是我从本地存储获取的值,并使用".Child(UserID)"通过查询发送到数据库

Query:(用于管理员)

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:

"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,我要获取集合中的所有可用数据。

推荐答案

我想我知道出了什么问题。您的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'"
    }
  }
}

这篇关于如何保护用户和管理员在Firebase数据库中的访问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆