如何使用安全规则对Firestore的公共收藏进行查询?

发布于 2025-02-13 21:02:49 字数 1224 浏览 1 评论 0原文

我在以这种方式定义的Firestore安全规则中有一个规则集:

rules_version = '2';
service cloud.firestore {

  match /databases/{database}/documents {
    function isUserAllowed(root, metric) {
      return metric != 'private-data' || (metric == 'private-data' && request.auth.token.sub in get(/databases/$(database)/documents/$(root)/users).data.users);
    }
    
    match /{root}/{doc} {
      allow read:  if root != 'ispd' && doc != 'users';
      allow write: if false;

      match /{metric}/{docs=**} {
        allow read: if isUserAllowed(root, metric);
        allow write: if false;
      }
    }
  }
}

我正在尝试从React应用程序中执行此方法,

const checkForbiddenEmailDomain = (emailDomain) => {
  const collectionRef = collection(db, 'forbidden-domains');

  const q = query(collectionRef, where('domain', '==', emailDomain));

  return getDocs(q)
    .then(({ docs }) => {
      if (docs.length > 0) {
        const errorToThrown = { message: `The ${emailDomain} domain is forbidden` };
        throw errorToThrown;
      }
    });
};

因为我正在访问禁止域公共的我不应该遇到问题,但是,我总是会遇到丢失或不足的权限。我知道Firestore文档中有一部分,他们说诸如“规则不是查询,Blablabla”之类的东西,埃及象形文字可能更简单地理解

I have a ruleset in Firestore Security Rules defined in this way:

rules_version = '2';
service cloud.firestore {

  match /databases/{database}/documents {
    function isUserAllowed(root, metric) {
      return metric != 'private-data' || (metric == 'private-data' && request.auth.token.sub in get(/databases/$(database)/documents/$(root)/users).data.users);
    }
    
    match /{root}/{doc} {
      allow read:  if root != 'ispd' && doc != 'users';
      allow write: if false;

      match /{metric}/{docs=**} {
        allow read: if isUserAllowed(root, metric);
        allow write: if false;
      }
    }
  }
}

I'm trying to execute this method from my React application

const checkForbiddenEmailDomain = (emailDomain) => {
  const collectionRef = collection(db, 'forbidden-domains');

  const q = query(collectionRef, where('domain', '==', emailDomain));

  return getDocs(q)
    .then(({ docs }) => {
      if (docs.length > 0) {
        const errorToThrown = { message: `The ${emailDomain} domain is forbidden` };
        throw errorToThrown;
      }
    });
};

Since I'm accessing forbidden-domains which is public I shouldn't have problems, however I always get Missing or insufficient permission. I know that there is the section in Firestore documentation where they say something like "Rules are not queries, blablabla", probably the Egyptian hieroglyphs are simpler to understand ????.

With no jokes, can someone help me? I really don't understand what should I do.
Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-02-20 21:02:49

我终于以这种方式修改了MU规则集,它有效:

rules_version = '2';
service cloud.firestore {

  match /databases/{database}/documents {
    function isUserAllowed(root, metric) {
      return metric != 'private-data' || (metric == 'private-data' && request.auth.token.sub in get(/databases/$(database)/documents/$(root)/users).data.users);
    }
    
    match /forbidden-domains/{docs=**} {
        allow read;
      allow write: if false;
    }
    
    match /{root}/{doc} {
      allow read: if root != 'instagram-service-private-data' && doc != 'users';
      allow write: if false;

      match /{metric}/{docs=**} {
        allow read: if isUserAllowed(root, metric);
        allow write: if false;
      }
    }
  }
}

I finally ended up modifying mu ruleset in this way, and it works:

rules_version = '2';
service cloud.firestore {

  match /databases/{database}/documents {
    function isUserAllowed(root, metric) {
      return metric != 'private-data' || (metric == 'private-data' && request.auth.token.sub in get(/databases/$(database)/documents/$(root)/users).data.users);
    }
    
    match /forbidden-domains/{docs=**} {
        allow read;
      allow write: if false;
    }
    
    match /{root}/{doc} {
      allow read: if root != 'instagram-service-private-data' && doc != 'users';
      allow write: if false;

      match /{metric}/{docs=**} {
        allow read: if isUserAllowed(root, metric);
        allow write: if false;
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文