【问题标题】:Firestore security rules ternary operators is not workingFirestore 安全规则三元运算符不起作用
【发布时间】:2021-08-27 20:22:59
【问题描述】:

Firebase firestore 安全规则三元运算符如果 else 不起作用我不知道为什么请帮助我参考文章或答案

if author(userid)
                && data().postcounter != null
                ? data().postcounter >= 0
                : data().itemtype == 'jug'

我在测试中发现的只是第一个,如果操作符 return 正在工作,而不是 else 部分 我是飞镖背景的

编辑:data() = request.resource.data

【问题讨论】:

  • 请说明什么不起作用并提供代码 sn-ps。它是允许还是拒绝所有读/写?是否因为无效规则而引发错误?还有什么问题吗?
  • 您找到解决方案了吗?

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


【解决方案1】:

你的问题应该说明你想用你给出的代码来完成什么。

尽管如此,我不知道这是否能解决问题,但至少这可以指导您编写更好、更清晰的代码。

如果您尝试将自定义函数与 request.resource.data 一起使用,我会建议这种授予权限的方式。它将使代码更具可读性和更易于维护,并且还可以在许多权限检查中使用相同的功能:

function exampleFunction(data) {
let postcounter = data.postcounter != null;
let postcounter2 = data.postcounter >= 0;
let itemtype = data.itemtype == 'jug';

return request.auth.uid != null && postcounter ? postcounter2 : itemtype;
}

match /ExampleColletion/{ID}{
    allow read, write, update, delete: if exampleFunction(request.resource.data);
  }

我自己将上面的示例用于具有 20 个集合之类的项目,并且具有用于权限检查的通用功能。这种授予权限的方式更加简洁,并且可以更轻松地创建和维护规则,尤其是在具有数据验证和在 Firestore 规则中授予自定义权限的大型项目中。

参考:https://firebase.google.com/docs/rules/rules-language#building_conditions
https://firebase.google.com/docs/firestore/security/rules-conditions

【讨论】:

    猜你喜欢
    • 2019-01-31
    • 2018-03-19
    • 2021-05-12
    • 2021-11-22
    • 2019-09-06
    • 2018-05-28
    • 2020-08-24
    • 1970-01-01
    相关资源
    最近更新 更多