firestore.rules 中的编译错误:意外 'if'

Compilation errors in firestore.rules: Unexpected 'if'

我的firestore.rules中有这个编译错误:

Compilation errors in firestore.rules:
[E] 35:7 - Unexpected 'if'.
[E] 38:7 - Unexpected 'else'.
[E] 42:3 - Unexpected '}'.

摘自firestore.rules如下:

service cloud.firestore {
  match /databases/{database}/documents {

    match /requests/{id} {
     allow read: if isSignedIn() && ((getUserKennung() in resource.data.users_read) || userIsXY());
     allow write: if isSignedIn() && ((getUserKennung() in resource.data.users_write) || userIsXY());
    }

    function getUserKennung(){
     let email = request.auth.token.email;
     if(email.lower().matches('.*@provider[.]org')){
       return email[0:7].upper();
     }
     else {
       return 'ERR: Invalid usermail';
     }
    }

    function userIsXY(){
      return request.auth.token.email == 'XY@provider.org';
    }

    function isSignedIn(){ 
      return request.auth != null;
    }
  }
}

我不知道为什么会出现错误。

我认为安全规则的自定义函数中不允许使用 if...then...else,因此您可能想尝试这样的三元表达式:

function getUserKennung(){
 let email = request.auth.token.email;
 return email.lower().matches('.*@provider[.]org')
   ? email[0:7].upper()
   : 'ERR: Invalid usermail';
}