如果文档路径中带有逗号,则Firestore规则会中断

时间:2019-05-28 23:00:03

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

因此,我正在构建一个包含多个集合和用户邀请的多租户应用程序。由于我不知道注册将使用哪个用户ID,并且我希望公司开始对其用户数据进行更改和更新,因此我预先设置了用户ID,并在以下几种路径中使用它:

/companies/{company}/customers/{userId}
/users/{userId}

现在,此userId几乎是用逗号替换点的电子邮件。点是Firebase中不允许使用的字符,但逗号是逗号,反之亦然,例如电子邮件,因此这是有意义的,并且在那里没有问题。

问题是当我需要制定一些规则时。例如,我在配置中有此选项,因为我希望管理员可以访问每个公司:

  function cleanEmail(){
        return request.auth.token.email.split('.').join(',') //Here I also tried %2C
    }

    function isSuperadmin() {
      return exists(/databases/$(database)/documents/admins/$(cleanEmail()))
    }


 match /companies/{company} {
      allow write: if isSuperadmin();
      allow read: if isSuperadmin() || belongsToCompany(company)
}

模拟器坏了,但是我不明白为什么,所以我什至发送了一个错误报告。最终,我尝试对路径进行硬编码,然后发现了问题:

function isSuperadmin() {
      return exists(/databases/$(database)/documents/admins/test,account@gmail,com) //Same issue with get()
    }

enter image description here

我尝试使用exist(),因为我认为它可能是get()的错误,但问题仍然存在。我认为这应该是一个错误,因为这是有效的Firestore路径,而且我已经看到一些人已经在使用这种“干净的电子邮件”策略。

有趣的是,这不是在第一次测试期间发生的,我意识到只有在逗号位于@之前,才会发生此错误。如果您在此之前删除一个,然后保留第二个,则似乎可以工作:

enter image description here

我可以在干净的电子邮件中添加一个新步骤,将其转换为base64,并且可以正常工作。如果有人有很好的解决方案。

1 个答案:

答案 0 :(得分:0)

我做了类似的概述:

/invitation
    /fb-generated-doc-name
        email: somecustomer@gmail.com
        trip: db-ref-to-trip

/usertrips
    /doc-name-is-UID
        trip: db-ref-to-trip

/trips
   /fb-generated-doc-name
       { ... describes the trip ... }

onCreate认证用户的轮廓是:

exports.authDidCreateUser = functions.auth.user().onCreate((authUser, context) => {
  /* 
      find invitation where email == authUser.email
      let docref = collection('usertrips').doc(authUser.uid)
      docref.set({ trip: invitation.trip })
  */
});
相关问题