firebase - 在 Firestore 规则中声明一个函数

标签 firebase google-cloud-firestore firebase-security

这是我现在面临的 Firestore 安全规则问题。

首先,这里是我的 firestore 数据库中的数据结构示例:

userProfiles/userId/userData

companies/companyId/companyData

看起来很简单。每个 userData 包含一个名为 companies 的数组,其中包含该用户有权访问的所有 companyId。

现在我需要编写规则以仅当 companyId 是特定用户信息公司列表时才允许读取 companyData。

以下是 为我工作 的规则:
service cloud.firestore {
  match /databases/{database}/documents {
    match /companies/{companyId} {
      allow read: if companyId in get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.companies
    }
  }
}

考虑到我将有更多规则,我想让它们更具可读性和重用性。根据这个 official guide 我可以创建自定义函数,根据这个 article 它们可以是通用的,并且可以在主要规则块之外声明。

我重构了我的规则,看起来像这样,它也 为我工作 :
service cloud.firestore {
  match /databases/{database}/documents {
    match /companies/{companyId} {
      allow read: if companyId in getUserCompanies()
    }
    function getUserCompanies() {
        return get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.companies
    } 
  }
}

但是现在我想将函数移到规则块之外以使其更加清晰:
service cloud.firestore {
  match /databases/{database}/documents {
    match /companies/{companyId} {
      allow read: if companyId in getUserCompanies()
    } 
  }
}

function getUserCompanies() {
    return get(/databases/$(database)/documents/userProfiles/$(request.auth.uid)).data.companies
}

这是行不通的。没有任何错误,我只是收到来自模拟器的常规 Read denied 消息。

所以问题是:是否可以像我在示例中那样将函数移到外面?我在这里犯了什么明显的错误吗?有没有更好的方法让我的规则设置得更加清晰?

附言
我还尝试将一些参数传递给该函数,包括用户和公司 ID - 不走运。

最佳答案

可以在规则文件中的任何级别定义函数。但是他们只能访问在您定义它们的范围内定义的变量。您必须作为变量传入的任何其他内容。

所以这个(无用的)函数在全局定义时有效:

function isTrue() {
  return true;
}

但是这个不会,因为它无法访问 request :
function isAdmin() {
  return (request.auth.token.email_verified && 
    request.auth.token.email.matches(".*@google.com"));
}

我有时会在函数定义中添加一个参数:
function isAdmin(request) {
  return (request.auth.token.email_verified && 
    request.auth.token.email.matches(".*@google.com"));
}

然后将变量传递给调用:
  allow update: if isAdmin(request) || 
    (request.auth.uid == uid && isUnmodified(request, resource, 'name'));

关于firebase - 在 Firestore 规则中声明一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56442795/

相关文章:

javascript - 尝试上传到存储时出现 firebase 错误

javascript - 在 Firebase 上创建/添加用户

firebase - react-native-firebase 包是否处理 "Setting a timer for a long period of time is a performance and correctness issue"警告?

java - 针对多个字段的 Firestore 查询过滤器

android - 类似SQL的Firestore中的联接

swift - 模拟器在 Firebase 中没有显示错误,但权限被拒绝

firebase - 当客户端重新联机并且挂起的写入完成时,如何确定 firestore 安全规则是否失败?

javascript - 对于 Firebase 简单登录,我必须登录两次才能注册

android - Firebase 日期格式 - 桥接 IoS 和 Android 的最佳实践

php - Laravel Firebase cURL 错误 60 : SSL certificate problem: unable to get local issuer certificate