2016-08-12 63 views
5
{ 
    "rules": { 
     "users": { 
      "$uid":{ 
       //Private whatever under "uid" but Public is exposed 
       ".read": "auth != null && auth.uid == $uid", 
       ".write": "auth != null && auth.uid == $uid", 

       "public": { ".read": "auth != null" } 
       } 
       } 
      } 
} 
  • 我創建這些規則,讓用戶公/私輪廓
  • 用戶/{} UID/公共」輪廓應該是那些被認證的任何用戶訪問,但在「用戶/ UID

這裏不能訪問的數據存儲在我的火力點數據庫中的一些假數據。如何使用Firebase安全規則創建公/私用戶配置文件?

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "Name:" : "Example 1", 
     //This public child should be accessible by 
     //"Example 2" but cannot know the name of 
     // this user 
     "public" : { 
     "email" : "[email protected]" 
     } 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "Name:" : "Example 2", 
     //This public child should be accessible by 
     //"Example 1" but cannot know the name of 
     // this user 
     "public" : { 
     "email" : "[email protected]" 
     } 
    } 
    } 
} 

我想知道這是否是防止任何用戶訪問用戶重要信息的可靠方法!無論如何,我可以通過驗證來改善這一點嗎?我很樂意接受你們的建議。我想爲我的應用程序創建最佳和簡單的安全規則。

+0

請拼寫您的標題。 – 2016-08-12 17:04:56

回答

7

您可以使用當前的數據結構來確保訪問私人和公共數據。

但是您可能需要的一種用例是顯示所有用戶的公共信息列表。用你目前的數據結構是不可能的,因爲Firebase's security model cannot be used to filter data。有關這方面的很好的答案,請參閱Restricting child/field access with security rules

大多數開發商在完全獨立的子樹分割的公共和私有數據:

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "Name:" : "Example 1", 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "Name:" : "Example 2", 
    } 
    }, 
    "public_profiles": { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "email" : "[email protected]" 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "email" : "[email protected]" 
    } 
    } 
} 

可以與當時的安全訪問:

{ 
    "rules": { 
    "users": { 
     "$uid":{ 
      ".read": "auth != null && auth.uid == $uid", 
      ".write": "auth != null && auth.uid == $uid", 
     } 
    }, 
    "public_profiles": { 
     ".read": "auth != null", 
     "$uid":{ 
      ".write": "auth != null && auth.uid == $uid", 
     } 
    } 
    } 
} 

現在,任何身份驗證的用戶可以收聽/public_profiles,這意味着你可以輕鬆顯示這些配置文件的列表。

0

嗯不會更容易(重新)結構分貝,讓你有一個公共和私人領域每個用戶?喜歡的東西:

{ 
    "users" : { 
    "YFIIAgwa2kaannrXjwvSZmoywma2" : { 
     "private": { 
     "Name:" : "Example 1" 
     }, 
     "public" : { 
     "email" : "[email protected]" 
     } 
    }, 
    "YgSfSzPzxLbyDL17r6P9id2cdvH2" : { 
     "private": { 
     "Name:" : "Example 2" 
     }, 
     "public" : { 
     "email" : "[email protected]" 
     } 
    } 
    } 
} 

/UPD:這樣,它應該是很容易(ER)有不同的權限,因爲他們不會從父繼承呢?

+0

我喜歡你如何分開。我想我可以做到這一點,但是當我想向它添加更多數據時,它不會在將來產生嵌套問題!有沒有辦法爲公共數據創建一個單獨的樹? – user2884707bond

+0

您可以使用公共配置文件作爲「默認」數據並使用它在私有字段中查找其他內容。這樣你就不會有私人和公共場所有相同數據的重複字段。您請求公開數據,然後私人並將它們合併到客戶端 – REJH

+0

感謝您的幫助:D – user2884707bond