具有多个聊天室的应用的Firebase安全规则

时间:2013-08-01 01:51:50

标签: firebase firebase-security

我无法想象安全规则对于看起来像这样的应用程序的样子:

  • 一个有多个聊天室的火柱。
  • 主持人通过单独的PHP应用程序进行身份验证。
  • 版主只能修改自己的聊天室,他们可以在聊天室内读取,写入,更新和删除任何内容。
  • 访客通过单独的PHP应用程序到达并进行身份验证。
  • 访客具有读写权限,但可能无法删除任何内容。

我现在的问题是:

  1. 是否可以配置规则以满足所有这些要求?或者是否有一些不可能满足的要求?

  2. 在向Firebase通知用户是否存在时,PHP Server必须与Firebase通信的程度如何?

1 个答案:

答案 0 :(得分:9)

首先,check out this gist,这是我前段时间为多个聊天室工作的一个例子。

  1. 是。这完全有可能。
  2. PHP服务器?你不需要服务器! :)
  3. 数据结构基本如下:

    # chats roughly equal "rooms"
    /chats/chat_id/users/...
    
    # a timestamp of when each participant last viewed the room
    /chats/chat_id/last/... 
    
    # the messages sent
    /chats/chat_id/messages/...
    

    安全规则是自我记录的。这是参照完整性的本地副本。

    {
      "chat": {
         // the list of chats may not be listed (no .read permissions here)
    
         // a chat conversation
         "$key": {
    
             // if the chat hasn't been created yet, we allow read so there is a way 
             // to check this and create it; if it already exists, then authenticated 
             // user (specified by auth.account) must be in $key/users
            ".read": "auth != null && (!data.exists() || data.child('users').hasChild(auth.account))",
    
            // list of users authorized to participate in chat
            "users": {
               // if the list doesn't exist, anybody can create it
               // if it already exists, only users already in the list may modify it
               ".write": "!data.exists() || data.hasChild(auth.account)",
               "$acc": {
                  // for now the value is just a 1, later it could be a read/write/super privilege
                  ".validate": "newData.isNumber()"
               }
            },
    
            // timestamps recording last time each user has read this chat
            "last": {
               "$acc": {
                  // may only written by the authenticated user and if user is in $key/users
                  ".write": "$acc === auth.account && root.child('chat/'+$key+'/users').hasChild($acc)",
                  ".validate": "newData.isNumber()"
               }
            },
    
            "messages": {
               "$msg": {
                  // to write a message, it must have all three fields (usr, ts, and msg)
                  // and the person writing must be in $key/users
                  ".write": "root.child('chat/'+$key+'/users').hasChild(auth.account)",
                  ".validate":"newData.hasChildren(['ts', 'usr', 'msg'])",
                  "usr": {
                     // may only create messages from myself
                     ".validate": "newData.val() === auth.account"
                  },
                  "msg": {
                     ".validate": "newData.isString()"
                  },
                  "ts": {
                     ".validate": "newData.isNumber()"
                  }
               }
            }
         }
      }
    
    }
    

    主持人通过单独的PHP应用程序进行身份验证。 使用custom login module为管理员创建Firebase令牌。根据您在该令牌中存储的数据应用安全规则。

    版主只有权修改自己的聊天室... 通过简单地扩展上面的用户权限,这应该是非常自我解释的。

    访客通过单独的PHP应用程序到达并进行身份验证。 使用custom login module为管理员创建Firebase令牌。根据您在该令牌中存储的数据应用安全规则。

    (或者废弃PHP应用程序并使用Firebase's baked in authentication!)

    访客具有读写权限,但可能无法删除任何内容。 在“.write”规则中使用newData.exists()或newData.hasChildren(...)以防止删除。

    客人不能欺骗其他客人。 身份验证令牌将阻止此