Firestore安全规则 无效的变量名称: request

huangapple go评论53阅读模式
英文:

Firestore Security Rules Invalid variable name: request

问题

The issue in your code is that you're using HTML entity codes like > and & instead of their actual characters. Here's the corrected code with the proper translations:

rules_version = '2';
service cloud.firestore {

  // Check if user is authenticated
  function isAuthenticated() {
    return request.auth != null;
  }

  match /databases/{database}/documents {
  
    function isUserCompanyAdmin(companyID) {
      return isAuthenticated() &&
        request.auth.token.role == 'admin' && request.auth.token.companyID == companyID;
    }

    match /companies/{companyId} {
      allow read, write, update, delete, create, get: if isUserCompanyAdmin(companyId);
    }
  }
}

The issue was mainly with > and & which have been replaced with > and &.

英文:

I'm having an error when writing a security rule that verifies if the requestor can make an action. By I'm getting:

> Invalid variable name: request.

Here is my code:

rules_version = '2';
service cloud.firestore {

  // Check if user is authenticated
  function isAuthenticated() {
    return request.auth != null;
  }

  match /databases/{database}/documents {
  
    function isUserCompanyAdmin(companyID) {
      return isAuthenticated() &&
        request.auth.token.role == 'admin' && request.auth.token.companyID == companyID;
    }

    match /companies/{companyId} {
      allow read, write, update, delete, create, get: if isUserCompanyAdmin(companyId);
    }
  }
}

What's wrong?

Thanks

答案1

得分: 1

如果我记得正确,request 仅在 match 子句内可用。因此,您可以将 request 传递给该函数,或将其移到 match 子句内。

英文:

If I recall correctly, request is only available inside match clauses. So you can either pass request to the function - or move it into a match clause.

huangapple
  • 本文由 发表于 2023年5月25日 03:02:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76326674.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定