【问题标题】:What Firebase Security rules should I use to read the messages?我应该使用哪些 Firebase 安全规则来阅读消息?
【发布时间】:2017-04-01 10:21:17
【问题描述】:

我只希望那些用户阅读 uid 等于或等于 messagetofrom 字段中的一个或两个的消息,或者如果消息的“to”部分设置为 @987654327 @。
这是我的数据库:

{
"intents" : {
  "intentFields" : "",
  "intentName" : "",
  "to" : ""
},
"messages" : {
  "-KViVgc7ZG051eMXP0-5" : {
    "from" : "Ed",
    "name" : "Ed",
    "photoUrl" : "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-512.png",
    "text" : "Hello, I'm Ed",
    "timeStamp" : "1476880306",
    "to" : "all",
    "type" : "text"
  },
  "-KWmsuvm0uJIf01eHfyN" : {
    "from" : "Capyv3mxQsUUn2W1nPOgcJ0Ex9T2",
    "name" : "Aakash Bansal",
    "photoUrl" : "https://lh5.googleusercontent.com/-oQyA4HXVycc/AAAAAAAAAAI/AAAAAAAAHKo/Ov0A0p0LjiY/s96-c/photo.jpg",
    "text" : "ho",
    "timeStamp" : "1479396273",
    "to" : "Ed",
    "type" : "text"
  }
}
}

我尝试了几条规则,并阅读了 Firebase 文档。但是没有一条规则帮助我实现了我的结果。

我尝试过的规则示例是:

{
"rules": {
  "intents" : {
    ".read" : "auth.uid === data.child('to').val()",
    ".write" : true
  },
  "messages" : {
    "$message": {
           ".read":  "auth.uid !== null",
           ".write": true
    }
  }
}
}

以下规则集工作正常,虽然它们没有达到我想要的结果,但它们显示了应用程序中的所有消息。

{
"rules": {
  "intents" : {
    ".read" : "auth.uid === data.child('to').val()",
    ".write" : true
  },
  "messages" : {
    ".read":  "auth.uid !== null",
    "$message": {
           ".write": true
    }
  }
}
}


只是通知一下,我在 Android 应用程序中使用 firebaseui 来读取数据。如果我对“firebase 安全规则”的理解有问题,请告诉我。

编辑:即使以下规则集也不起作用:

{
"rules": {
  "intents" : {
    ".read" : "auth.uid === data.child('to').val()",
    ".write" : true
  },
  "messages": {
    "$message": {
      ".read" : true,
      ".write": true
    }
  }
}
}


我用来读取数据的android代码是:

mFirebaseAdapter = new MessageAdapter(FriendlyMessage.class,
       R.layout.message_my,
       RecyclerView.ViewHolder.class,
       mFirebaseDatabaseReference.child("/messages"));

【问题讨论】:

  • 你如何阅读这些信息?因为如果您尝试阅读/messages,那么这些规则确实会失败。这是一个常见的混淆来源,我们在文档和this answer here 中将其描述为rules are not filters
  • 你的代码在哪里?你实际上在读什么? (提示:Puf 上面的评论几乎可以肯定是答案,但没有代码就无法判断)
  • 是的 puf,我已经将 firebaserecycleradapter 放在 /messages 中,我应该把它放在哪里来获取消息?

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


【解决方案1】:
{
  "rules": {
    "messages": {
      "$message": {
        ".read": "auth.uid === data.child('to').val() || auth.uid === data.child('from').val() || data.child('to').val() === 'all' ",
        ".write": true
      }
    }
  }
}

【讨论】:

  • 不,先生,它不工作。虽然我已经尝试过这个解决方案和一些更类似的解决方案,但我没有在应用程序中获取任何数据
  • 什么不起作用?你想做什么?你遇到了什么错误?
  • 我正在使用 Android 中的回收器适配器读取消息,并且“不工作”是指消息没有显示在应用程序中
  • 你的 auth.uid 是 Capyv3mxQsUUn2W1nPOgcJ0Ex9T2?
猜你喜欢
  • 1970-01-01
  • 2021-01-05
  • 2014-12-19
  • 2020-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
相关资源
最近更新 更多