4

我有火力地堡數據結構有點像這樣:火力地堡安全規則 - 允許讀取僅用於用戶的內容

+ tasks 
    + task_id 
    + title 
    + user 
    + ... 
+ users 
    + ... 

的從前端(使用AngularJS和AngularFire(加上內置的SimpleLogin)) - 當登錄用戶創建任務時,用戶的uid(例如simplelogin:2)被放入正在創建的任務的用戶部分。

當用戶登錄時,他們可以通過使用只查看他們的任務:

$firebase(ref.orderByChild('user').equalTo(User.uid)); 

下一步是保護數據,這就是我掙扎。我已經試過:

"tasks": { 
    ".indexOn": ["user", "order"], 
    ".write": "auth != null", 
    ".read": "auth != null && data.child('user').val() == auth.uid", 
    "$task": { 
     "title": { 
      ".validate": "newData.isString() && newData.val().length <= 1000" 
     }, 
     "completed": { 
     }, 
     "time_added": { 
      ".validate": "newData.val() <= now" 
     }, 
     "order": { 
     }, 
     "user": { 
      ".validate": "newData.val() != null && newData.val() == auth.uid" 
     }, 
     "$other": { 
      ".validate": false 
     } 
    } 
    } 

也:

"tasks": { 
    "$task": { 
     ".indexOn": ["user", "order"], 
     ".write": "auth != null", 
     ".read": "auth != null && data.child('user').val() == auth.uid", 

     "title": { 
      ".validate": "newData.isString() && newData.val().length <= 1000" 
     }, 
     "completed": { 
     }, 
     "time_added": { 
      ".validate": "newData.val() <= now" 
     }, 
     "order": { 
     }, 
     "user": { 
      ".validate": "newData.val() != null && newData.val() == auth.uid" 
     }, 
     "$other": { 
      ".validate": false 
     } 
    } 
    } 

但是我得到:

Error: permission_denied: Client doesn't have permission to access the desired data.

我要去哪裏錯了?

+1

不能使用安全規則的過濾器。這包括使用查詢。由於查詢必須從本質上讀取任務/以獲取匹配的子項,因此查詢失敗是因爲在父級(即迭代)級別上不允許讀取訪問。 – Kato 2015-01-27 20:57:48

+0

Hi @Kato - 在這種情況下,你的建議是什麼?所有任務只能在'user'字段中被用戶(1個或多個)訪問。還是我錯誤地構造數據? – mcnamee 2015-01-28 08:20:19

+0

查看[結構化數據](https://www.firebase.com/docs/web/guide/structuring-data.html)指南。有一個標題爲創建涵蓋指數的數據的部分。這裏有一些變體是必要的。 – Kato 2015-01-28 16:12:23

回答

4

tasks集合沒有user孩子。每個具體的任務有一個user孩子。所以,你需要將你的讀取的規則下一層:

"tasks": { 
    "$task_id" { 
     ".read": "auth != null && data.child('user').val() == auth.uid", 
    } 

也看到了例子:https://www.firebase.com/docs/security/guide/user-security.html

+0

感謝您的時間@Frank - 不幸的是它仍然無法正常工作 - 我在安全規則中添加了更多信息以防萬一。 – mcnamee 2015-01-27 08:22:10

+0

此答案試圖過濾正確嗎?我認爲過濾是不可能的。 – 2015-04-24 07:39:38

+0

好點@ChristiaanWesterbeek!不知道是我錯過了,還是在編輯中。你能爲此添加一個答案嗎?我會高興。 :-) – 2016-02-11 15:19:00