同一子级中数据的Firebase安全规则

我的数据库结构是

Homeland
 +Ui4PSHU0kCPGpFJ6jwiJpodLvEds
  +service
  +quta
  +period
+55dwaSHU0kCPGpFJ6jwiJpodLvEds
  +service
  +quta
  +period

我想公开“服务”和“ quta”子项,但只有注册用户可以显示“句点”子项。但是关于我的用户目录是否存在问题。

首先,我想测试$ uid的能力。但是我不能。当我将$ uid删除时,只能使用“ Homeland”,这是可行的。

{
  "rules": {
    "Homeland": {
         "$uid" : {
           ".read": true,".write": "auth !== null"
         },},".read": "auth !== null",".write": "auth !== null"
  }
}

这是工作

{
  "rules": {
    "Homeland": {
           ".read": true,".write": "auth !== null"
        },".write": "auth !== null"
  }
}

更新

按照samthecodingman所述更改我的安全规则。

{
  "rules": {
    "Homeland": {
      "$uid" : {
        "service": {
          ".read": true,// <-- publically readable
          ".write": "auth.uid === $uid" // <-- writable only by owner
        },"quta": {
          ".read": true,"period": {
          ".read": "auth !== null",// <-- readable by any logged in user
          ".write": "auth.uid === $uid" // <-- writable only by owner
        }
      },"$other": { // <-- everywhere else not named above
      ".read": "auth !== null",// <-- allows read if logged in
      ".write": "auth !== null" // <-- allows write if logged in
    }
  }
}

代码

var kullanici = firebase.database().ref().child("Homeland").child(uservaluefrominput).child("service");
kullanici.on('value',function(datasnapshot) {


     sifre = datasnapshot.val();

     console.log(sifre);



});

但是代码和模拟都被拒绝读取值。

捕获错误。注意:这次我宣布清除用户ID。

try {

kullanici = firebase.database().ref().child("Homeland").child("Ui4PSHU0kCPGpFJ6jwiJpodLvEO2").child("service");
kullanici.on('value',function(datasnapshot) {


     sifre = datasnapshot.val();

     console.log(sifre);



});

} catch (someError) {
  console.log('got some error: ',someError);
}
chenglong676 回答:同一子级中数据的Firebase安全规则

请务必注意database rules cascade。即使您拒绝/users/$uid以外的任何人访问$uid,但允许任何人访问/users/,任何人都可以修改更深的位置/users/$uid

在上述规则中,即使您已经为/Homeland/$uid配置了规则,您仍允许任何登录用户对根数据库进行读/写访问。

{
  "rules": {
    "Homeland": {
      "$uid" : {
        ".read": true,".write": "auth !== null"
      },},".read": "auth !== null",// <-- allows read everywhere if logged in
    ".write": "auth !== null" // <-- allows write everywhere if logged in
  }
}

要解决此问题,您可以引入$others键,以便登录用户可以读取/写入数据库中/Homeland下方的任何位置,除非另有允许。

{
  "rules": {
    "Homeland": {
      "$uid" : {
        ".read": true,"$other": { // <-- everywhere else not named above
      ".read": "auth !== null",// <-- allows read if logged in
      ".write": "auth !== null" // <-- allows write if logged in
    }
  }
}

下一步是在/service键下限制对/quta/period/Homeland/$uid的访问。

{
  "rules": {
    "Homeland": {
      "$uid" : {
        "service": {
          ".read": true,// <-- publically readable
          ".write": "auth.uid === $uid" // <-- writable only by owner
        },"quta": {
          ".read": true,"period": {
          ".read": "auth !== null",// <-- readable by any logged in user
          ".write": "auth.uid === $uid" // <-- writable only by owner
        }
      },// <-- allows read if logged in
      ".write": "auth !== null" // <-- allows write if logged in
    }
  }
}

但是,由于/Homeland下的混合权限级别,您受到可以对此数据执行哪些查询的限制。例如,您不能只使用查询列出/Homeland下的所有用户,因为读取操作将被拒绝。

无效的查询将是:

firebase.database().ref('/Homeland').on('child_added',(snap) => { ... })`

要以尽可能简单的方式解决此问题,可以根据谁可以访问数据将/Homeland分为/HomelandPublic/HomelandRegistered。规则类似于:

{
  "rules": {
    "HomelandPublic": {
      "$uid" : {
        ".write": "auth.uid === $uid" // <-- writable only by owner
      },".read": true // <-- publically readable
    },"HomelandRegistered": {
      "$uid" : {
        ".write": "auth.uid === $uid" // <-- writable only by owner
      },".read": "auth !== null" // <-- readable by any logged in user
    },// <-- allows read if logged in
      ".write": "auth !== null" // <-- allows write if logged in
    }
  }
}

以前的查询现在将更改为:

// get publicly accessible data
firebase.database().ref('/HomelandPublic').on('child_added',(snap) => { ... }) // alternatively: once('value',(snap) => {})

// get data accessible by registered users
if (firebase.auth().currentUser) {
  firebase.database().ref('/HomelandRegistered').on('child_added',(snap) => {})
}

这应该为您提供足够的知识,以使其适应您的情况和需求。

本文链接:https://www.f2er.com/3092304.html

大家都在问