【问题标题】:How to secure access for user and admin in Firebase Database?如何保护 Firebase 数据库中用户和管理员的访问权限?
【发布时间】:2018-12-07 13:43:52
【问题描述】:

我使用 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:::::

"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"
       }
     }

非常感谢任何形式的帮助。

非常感谢您。

Edit:::: 添加了管理员查询。在管理员的情况下,我想获取集合中的所有可用数据。

【问题讨论】:

  • “我尝试了 [query.equalTo] 和 [data.child(auth.uid).val()] 方法来访问该值,但没有得到任何结果。”那是行不通的,因为您没有执行查询。您能否更新您的问题以在/workouts/$uid/users/$uid 下包含JSON(作为文本,没有屏幕截图)?您可以通过单击Firebase Database console 中的“导出 JSON”链接来获取此信息。

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


【解决方案1】:

我想我知道出了什么问题。在/workouts/$uid 下,您的 JSON 似乎包含用户的所有锻炼。您的规则试图让用户访问所有/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'"
    }
  }
}

【讨论】:

  • 首先感谢 Frank 给予您宝贵的时间。我尝试过同样的事情,但这会破坏 OR 条件。因为我还需要提供对管理员的访问权限。
  • 嗨,弗兰克,除了使用“$ variables”之外,有什么方法可以提取“child()”中传递的值吗?
  • @SumitKhatri 你不能保持管理规则和你的问题一样,并使用这个答案中的用户规则吗?
  • .read 条件应该完全符合您的要求。什么中断?
  • 请查看更新后的问题。我也添加了管理员查询。
猜你喜欢
  • 2018-09-16
  • 1970-01-01
  • 2012-02-17
  • 2012-02-07
  • 2017-02-01
  • 1970-01-01
  • 2021-02-28
  • 2018-03-24
  • 2021-01-02
相关资源
最近更新 更多