【问题标题】:Cannot get function with 'get' part in Firestore rules to work correctly无法通过 Firestore 规则中的“获取”部分获取功能以正常工作
【发布时间】:2018-07-09 20:11:07
【问题描述】:

在我的 Firestore 规则中,我有以下功能:

//True if the current user is 1 of the admins of this event, false otherwise
function isAnAdminOfEvent(){
  return get(/databases/$(database)/documents/users/$(request.auth.uid)).data.mail in resource.data.admins;
  //return "eUB5Lnl5" in resource.data.admins;
}

不知何故,这条规则总是失败。当我注释第一行并注释掉最后一行时,规则工作正常。请注意,我确定当前用户对象确实具有设置为“eUB5Lnl5”的邮件属性。

用户对象具有以下获取权限:

match /users/{userId} {
       allow read: if request.auth.uid == userId; //users can only read their own data
}

备注:这是我使用“get”方法的规则定义的第一部分。我可能在这里做错了什么......

应要求在 cmets 中添加:

目前失败的测试代码如下:

//Check that event admins are allowed to update other data of the event, such as the description or color
    CompletableFuture<Boolean> futureEventAdminChangeAction = new CompletableFuture<>();
    mFieldsMap.clear();
    mFieldsMap.put(DESCRIPTION, "An updated description!");
    mFieldsMap.put(COLOR, 3);
    mFieldsMap.put(NAME, "The new name");
    FireUtilEvents.getEvent( key ).set( mFieldsMap, SetOptions.merge()).addOnCompleteListener(task -> futureEventAdminChangeAction.complete( task.isSuccessful() ));
    Assert.assertEquals( true, futureEventAdminChangeAction.get() );

在测试中,我正在验证是否允许事件管理员更新他是管理员的事件。事件数据的更新规则如下:

allow update: if isNoEventOwnerChange() && 
            (isOwner() || (isAnAdminOfEvent() && isNotForbiddenAdminChange()));

在测试中:

  • isNoEventOwnerChange() 返回真,
  • isOwner() 返回 false,
  • isNotForbiddenAdminChange() 返回 true,
  • isAnAdminOfEvent() 返回 false(这是意料之外的)。

【问题讨论】:

  • 能否包含用于访问 Firestore 中数据的代码?
  • 你的函数isAnAdminOfEventmatch /databases/{database}/documents块中定义了吗?
  • 您能分享您尝试发送的数据的结构吗?看起来 mail 属性可能是嵌套的,因此它没有被验证
  • 嗨@Laurent,非常感谢您的评论!我发现我将函数嵌套在我的规则中一级到高级(与match /databases/{database}/document 相同的级别而不是在内部。将函数更深一层直接解决了问题。
  • 欢迎您!我添加了一个答案,以便有相同问题的人快速了解这个想法。如果您认为相关,请将其标记为已解决。祝你有美好的一天;)

标签: firebase google-cloud-firestore firebase-security


【解决方案1】:

将你的函数isAnAdminOfEvent 的定义放在match /databases/{database}/documents 块中。

【讨论】:

    猜你喜欢
    • 2020-04-29
    • 2013-07-25
    • 2020-01-14
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多