2015-09-26 78 views
1

所以我有這個數據庫的結構:Firebase安全規則,設置孩子不可讀?

enter image description here

在型材我要寄&提供商名稱只爲管理員和 用戶名可讀爲每個登錄的用戶可讀。 我如何實現這一目標?

這裏是我的規則:

{ 
    "rules": 
    { 
    "users": 
    { 
     "$uid": 
     { 
     // grants write access to the owner of this user account whose uid  must exactly match the key ($uid) 
     ".write": "auth !== null && auth.uid === $uid", 
     "profile": 
     { 
      // grants read access only for registered users 
      ".read": "auth !== null", 
      "email": 
      { 
      // This doesn't work with firebase as I was reading doc.      
      ".read": false 
      } 
     } 
     } 
    } 
    } 
} 
+1

當您有權讀取該節點中的所有數據時,Firebase將只返回一個節點。因此,您必須對數據建模,以便將公共數據和私有數據存儲在不同的頂級節點中。 (https://www.firebase.com/docs/security/guide/securing-data.html#section-filter) –

+0

請給我看一個簡單的例子嗎? – Dragod83

回答

1

那麼一點研究後和閱讀去正規化結構我想這樣會工作。事實是,我很想嵌套,但在firebase上可能是一個壞主意。

{ 
     "rules": 
     { 
     "users": 
     { 
      "$uid": 
      { 
      // grants write access to the owner of this user account whose uid must exactly match the key ($uid) 
      ".write": "auth !== null && auth.uid == $uid", 
      "public-profile": 
      { 
       // grants read access only for registered users 
       ".read": "auth !== null" 
      } 
      } 
     }, 
     "private-profile": 
     { 
      "$uid": 
      { 
       ".read": "root.child('users').child(auth.uid).child('role').child('admin').val() === 'true' && root.child('users').child('1').child('role').child('admin').val() === 'true'", 
       ".write": "root.child('users').child(auth.uid).child('role').child('admin').val() === 'true' && root.child('users').child('1').child('role').child('admin').val() === 'true'" 
      } 
     } 
     } 
    }