Firebase 创建要在firestore规则中重用的变量

Firebase 创建要在firestore规则中重用的变量,firebase,google-cloud-firestore,firebase-security,Firebase,Google Cloud Firestore,Firebase Security,在复杂的产品中,通常必须使用相同的规则条件,例如,如果用户是管理员: match /games/{game} { allow read: if true; allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true } match /locations/{location} { all

在复杂的产品中,通常必须使用相同的规则条件,例如,如果用户是管理员:

match /games/{game} {
  allow read: if true;
  allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
}
match /locations/{location} {
  allow read: if true;
  allow write: if request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true
}
是否有办法将条件存储在变量中以重用它(而不是每次都写入)?例如:

const isAdmin = request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;    

match /games/{game} {
  allow read: if true;
  allow write: if isAdmin
}
match /locations/{location} {
  allow read: if true;
  allow write: if isAdmin
}

我不知道firestore规则背后的语言是什么,我可以使用什么类型的资产。

是的,您可以使用自定义函数,如中所述

在您的情况下,它将是以下内容(未经测试):



请注意,函数在安全规则“层次结构”中的位置很重要。有关详细信息,请查看此项。

是的,您可以使用自定义函数,如中所述

在您的情况下,它将是以下内容(未经测试):



请注意,函数在安全规则“层次结构”中的位置很重要。有关更多详细信息,请查看此文档。

如果您想了解有关安全规则语法的更多信息,请从文档开始。如果您想进一步了解安全规则的语法,应该从文档开始。
function isAdmin() {
  return request.auth != null && get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true;    
}
match /games/{game} {
  allow read: if true;
  allow write: if isAdmin();
}
match /locations/{location} {
  allow read: if true;
  allow write: if isAdmin();
}