【问题标题】:Restrain access to users in GroupChat using Firebase使用 Firebase 限制对群聊中用户的访问
【发布时间】:2018-12-20 10:41:21
【问题描述】:

我实际上正在构建一个 react-native 和 firebase 群聊,我需要在适当的渠道中将用户分开。我有 6 个频道可以正常聊天,我不希望用户可以创建他的频道。在我的应用程序中,我想授权访问 A 组中的 5 个用户、B 组中的 8 个用户等。我目前有一个看起来像这样的 firebase 数据库

我的规则是:

{
  "rules": {
    "GeneralMessage": {
      ".read": "root.child('user').child(auth.uid).child('inGeneral').child('GeneralMessage').child('read').val() === true",
      ".write": "root.child('user').child(auth.uid).child('inGeneral').child('GeneralMessage').child('write').val() === true"
    }
  }
}

但这不允许我访问读写选项。

编辑:我在房间里推送聊天的反应原生代码。

import * as types from './actionTypes'
import firebaseService from '../../services/firebase'

const FIREBASE_REF_MESSAGES = firebaseService.database().ref('/GeneralMessage')
const FIREBASE_REF_MESSAGES_LIMIT = 20

export const sendMessage = message => {
  return (dispatch) => {
    dispatch(chatMessageLoading())

    let currentUser = firebaseService.auth().currentUser
    let createdAt = new Date().getTime()
    let chatMessage = {
      text: message,
      createdAt: createdAt,
      user: {
        id: currentUser.uid,
        email: currentUser.email,
      }
    }

    FIREBASE_REF_MESSAGES.push().set(chatMessage, (error) => {
      if (error) {
        dispatch(chatMessageError(error.message))
      } else {
        dispatch(chatMessageSuccess())
      }
    })
  }
}

export const updateMessage = text => {
  return (dispatch) => {
    dispatch(chatUpdateMessage(text))
  }
}

export const loadMessages = () => {
  return (dispatch) => {
    FIREBASE_REF_MESSAGES.limitToLast(FIREBASE_REF_MESSAGES_LIMIT).on('value', (snapshot) => {
      dispatch(loadMessagesSuccess(snapshot.val()))
    }, (errorObject) => {
      dispatch(loadMessagesError(errorObject.message))
    })
  }
}


const chatMessageLoading = () => ({
  type: types.CHAT_MESSAGE_LOADING
})

const chatMessageSuccess = () => ({
  type: types.CHAT_MESSAGE_SUCCESS
})

const chatMessageError = error => ({
  type: types.CHAT_MESSAGE_ERROR,
  error
})

const chatUpdateMessage = text => ({
  type: types.CHAT_MESSAGE_UPDATE,
  text
})

const loadMessagesSuccess = messages => ({
  type: types.CHAT_LOAD_MESSAGES_SUCCESS,
  messages
})

const loadMessagesError = error => ({
  type: types.CHAT_LOAD_MESSAGES_ERROR,
  error
})

【问题讨论】:

  • 反应原生代码?
  • 我刚刚编辑了我的帖子,但我认为这不是问题所在。问题是当我定义我的规则时。
  • 在涉及安全规则的情况下,最好掌握完整的情况。很多时候这是规则和读/写代码之间的简单不匹配。在您的情况下,我认为这是您的数据结构和我认为的规则之间的不匹配。我会在几分钟内写出答案。

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


【解决方案1】:

您的数据结构和安全规则不完全匹配。您有 /user/user.uid/inGeneral/GeneralMessage/read 的规则,但在您的数据结构中的 inGeneral 下没有 GeneralMessage 子项。

根据您当前的数据结构,您的规则必须如下所示:

{
  "rules": {
    "GeneralMessage": {
      ".read": "root.child('user').child(auth.uid).child('inGeneral').child('general').val() === true",
      ".write": "root.child('user').child(auth.uid).child('inGeneral').child('general').val() === true"
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2013-09-07
    • 1970-01-01
    相关资源
    最近更新 更多