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