Firestore规则中的子收集资源变量

发布于 2025-01-22 23:13:28 字数 1126 浏览 5 评论 0原文

我在Firestore中有一个任务集合。 任务具有标题为子任务的子汇编。任务和子任务都有一个所有者字段。 我有以下规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth != null;
    }

    match /tasks/{task} {
      allow read, update, delete: if isSignedIn() && request.auth.uid == resource.data.owner;
      allow create: if isSignedIn();
    
        match /subtasks/{subtask} {
            allow read, write: if isSignedIn() && request.auth.uid == resource.data.owner;
        }
    }
  }
}

匹配/tasks/{task}使用resource.data.data.owner检查auth uid的预期工作。为了避免额外的get()充电,我也将所有者存储在子任务中。

为什么request.auth.uid == resource.data.outher投掷firebaseError:丢失或不足的权限。尝试检索子任务时??

编辑: 参考我如何查询文档也可能很重要:

const subtasksSnapshot = await getDocs(collection(db, "tasks/" + taskId + "/subtasks"));
                
subtasksSnapshot.forEach((subtask) => {
    console.log(subtask.data());
});

I have a tasks collection in firestore. Tasks have subcollections titled subtasks. Both tasks and subtasks have an owner field.
I have the following Rules:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    function isSignedIn() {
      return request.auth != null;
    }

    match /tasks/{task} {
      allow read, update, delete: if isSignedIn() && request.auth.uid == resource.data.owner;
      allow create: if isSignedIn();
    
        match /subtasks/{subtask} {
            allow read, write: if isSignedIn() && request.auth.uid == resource.data.owner;
        }
    }
  }
}

The match /tasks/{task} works as expected with checking the auth uid against resource.data.owner. To avoid an extra get() charge I am storing the owner on the subtasks as well.

Why does request.auth.uid == resource.data.owner throw FirebaseError: Missing or insufficient permissions. when attempting to retrieve a subtask?

Edit:
It also might be of important to reference how I am querying documents:

const subtasksSnapshot = await getDocs(collection(db, "tasks/" + taskId + "/subtasks"));
                
subtasksSnapshot.forEach((subtask) => {
    console.log(subtask.data());
});

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

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

发布评论

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

评论(1

无法回应 2025-01-29 23:13:28
await getDocs(collection(db, "tasks/" + taskId + "/subtasks"));

运行上述查询时,它还包括所有者不是当前用户的文档。安全规则不会过滤数据。您必须自己滤除具有给定约束的文档。尝试运行以下查询:

const q = query(collection(db, "tasks/" + taskId + "/subtasks"), where("owner", "==", CURRENT_USER_ID))

const subtasksSnapshot = await getDocs(q);

另外,请确保在运行查询之前登录用户。

await getDocs(collection(db, "tasks/" + taskId + "/subtasks"));

When you run the above query, it also includes documents where owner is not current user. Security rules do not filter out data. You must filter out documents with given constraints yourself. Try running the following query:

const q = query(collection(db, "tasks/" + taskId + "/subtasks"), where("owner", "==", CURRENT_USER_ID))

const subtasksSnapshot = await getDocs(q);

Additionally, do ensure that user is logged in before running the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文