【问题标题】:Firestore rule split a custom token and see if it matchesFirestore 规则拆分自定义令牌并查看它是否匹配
【发布时间】:2021-11-11 22:24:33
【问题描述】:

我对用户设置了自定义声明,声明看起来像

organizations [
  933219123_Project-name,
  311239123_Different-Project-name
]

第一部分是 Firestore 中的 DocumentId。是否可以通过正则表达式或其他方式拆分或检查直到_?我知道我们不能在规则中使用循环

如果我在设置它们时不包括声明的_Project-name 部分,我有一条有效的规则,

  match /databases/{database}/documents {
    match /organizations/{uid}/repositories/{document=**} {
      allow read: if request.auth != null && uid in request.auth.token.organizations
    }
  }

但是,我在声明中需要项目 ID 和名称来获取前端内容。

除了将 ID 和名称都添加为单独的声明(似乎很浪费)之外,我还有其他选择吗?

【问题讨论】:

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


    【解决方案1】:

    根据问题,可以对字符串使用正则表达式。

    要查看如何在 Firebase 上使用 Regex,您可以参考Firebase Security Rules Regular Expressions

    我还建议尝试使用matchessplit 来满足您的期望。

    作为参考,我可以使用matches。 请看下面的示例:

     match /databases/{database}/documents {
        match /{organizations}/{uid}/repositories/{document=**} {
          allow read: if (request.auth != null && uid in request.auth.token.organizations && organizations.matches(document+'[_]test[-]project'))
        }
      }
    

    注意:Firestore Rules Playground 允许模拟读取(在没有 Auth 的情况下测试)。

    这将在_ProjectId 之前获得DocumentId,以检查带有ProjectIDDocumentID 是否在自定义声明列表中。

    【讨论】:

      猜你喜欢
      • 2018-11-14
      • 1970-01-01
      • 2019-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      • 2022-08-14
      相关资源
      最近更新 更多