向特定域内的用户提供限制式Firestore CRUD [英] Restric firestore CRUD to users within a specific domain

查看:30
本文介绍了向特定域内的用户提供限制式Firestore CRUD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Firestore中创建一条规则,以限制对属于特定域的用户的所有CRUD操作的使用.我的问题是,规则上下文中似乎不包含contains子句.

I'm trying to create a rule in Firestore that restricts usage to all CRUD operations for users that belongs to a specific domain, My issue is that seems like contains clause doesn't exist in the rule context.

这是我的规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if isUserAndSignedIn();
    }
  }

  function isUserAndSignedIn(){
    return request.auth != null && request.auth.token.email.contains('domain.com')
  }
}

我得到的错误是: 错误:simulator.rules行[9]的列[36].找不到函数错误:名称:[包含].

the error I'm getting is: Error: simulator.rules line [9], column [36]. Function not found error: Name: [contains].

我也尝试过使用indexOf之类的

I also tried with indexOf like

request.auth.token.email.indexOf('domain.com')!=-1 request.auth.token.email.endsWith('domain.com')

request.auth.token.email.indexOf('domain.com')!=-1 request.auth.token.email.endsWith('domain.com')

推荐答案

request.auth.token.email将是一个字符串,并且 matches 的方法,用于使用正则表达式,您会看到文档中有一些方便的示例代码:

request.auth.token.email is going to be a String, and according to the reference docs, String doesn't have a method called "contains". It does have a method called matches for using regular expressions, and you can see the documentation has a handy bit of sample code:

'user@domain.com'.matches('.*@domain[.]com') == true

您应该能够适应您的情况:

Which you should be able to adapt to your case:

return request.auth != null && request.auth.token.email.matches('.*@domain[.]com')

这篇关于向特定域内的用户提供限制式Firestore CRUD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆