Firestore安全规则:仅允许读取作者规则不起作用
我正在尝试使用availability
状态来保护我的文档,这可以是public
或private
。每个人都可以读取public
文档和private
文档可以由创建文档的用户读取(含义createoruid
设置为用户的UID)。
这些是我的规则:
match /Routines/{document} {
allow read: if resource.data.availability == "public" || (resource.data.availability == "private" && resource.data.creatorUid == request.auth.uid);
allow write: if true;
}
我创建了一些测试以查看我的规则是否有效:
describe.only("Routine Routine Rules", () => {
const userAuthA = { uid: "user123", email: "[email protected]" };
const demoRoutinePrivate = {
id: "DemoRoutine",
title: "Demo Routine",
availability: "private",
creatorUid: userAuthA.uid,
exercises: []
}
it("Authors can read private routines", async () => {
const admin = getAdminFirestore();
await admin.collection("Routines").doc(demoRoutinePrivate.id).set(demoRoutinePrivate);
const db1 = getAuthedFirestore(userAuthA);
const routines1 = db1.collection("Routines").where("creatorUid", "==", userAuthA.uid);
const routine1 = db1.collection("Routines").doc(demoRoutinePrivate.id);
await firebase.assertSucceeds(routines1.get());
await firebase.assertSucceeds(routine1.get());
});
});
但是,我会遇到此错误:
FirebaseError:
Property availability is undefined on object. for 'list' @ L33
我可以肯定我的文档具有availabilty
属性集,所以我不了解错误。
I'm trying to secure my Documents with a availability
status, that can be public
or private
. Everybody can read public
Documents and private
Documents can be read by Users who have created the Document (meaning creatorUid
is set to the user's uid).
These are my rules:
match /Routines/{document} {
allow read: if resource.data.availability == "public" || (resource.data.availability == "private" && resource.data.creatorUid == request.auth.uid);
allow write: if true;
}
I have created some tests to see if my rules work:
describe.only("Routine Routine Rules", () => {
const userAuthA = { uid: "user123", email: "[email protected]" };
const demoRoutinePrivate = {
id: "DemoRoutine",
title: "Demo Routine",
availability: "private",
creatorUid: userAuthA.uid,
exercises: []
}
it("Authors can read private routines", async () => {
const admin = getAdminFirestore();
await admin.collection("Routines").doc(demoRoutinePrivate.id).set(demoRoutinePrivate);
const db1 = getAuthedFirestore(userAuthA);
const routines1 = db1.collection("Routines").where("creatorUid", "==", userAuthA.uid);
const routine1 = db1.collection("Routines").doc(demoRoutinePrivate.id);
await firebase.assertSucceeds(routines1.get());
await firebase.assertSucceeds(routine1.get());
});
});
However, I get this error:
FirebaseError:
Property availability is undefined on object. for 'list' @ L33
I'm pretty certain that my Document has the availabilty
property set, so I don't understand the error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了。我的查询错误。
正确的查询是:
尽管原始错误消息不是很有帮助...
I figured it out. I had an error in my query.
the correct query is:
although the original error message is not very helpful...