我想通过firebase规则和firebase查询实现以下目标。(我正在使用firestore和vuejs)
前提条件:在任何情况下都必须对用户进行身份验证登录
read/write
自己创建所有数据条目read
字段(在我的情况下"visibility"
)设置为的所有数据"public"
id
即使条目的"visibility"
字段设置为不相等"public"
(例如"private"
),也允许用户读取具有条目的特定条目。假设这是我收藏的条目
从用户A创建的第一个条目,从用户B创建的第二个条目,从用户C创建的第三个条目:
所以:
read/write
进入第二个条目(对于其他用户,依此类推)read
进入第一和第三项。read
通过直接document-id查询知道Firebase文档的ID ,则应允许他们进入第二个条目。喜欢db.collection('my_collection').doc('abcxyz').get()
第二个问题是:我是否甚至需要该字段"user_id"
能够根据用户创建的文档过滤请求?还是firebase具有内置功能基于(身份验证)检索自创建文档?
[
{
"user_id": "the User-A's firebase id",
"foo": "foo",
"bar": true,
"visibility": "public"
},
{
"user_id": "the User-B's firebase id",
"foo": "foo",
"bar": true,
"visibility": "private"
},
{
"user_id": "the User-C's firebase id",
"foo": "foo",
"bar": true,
"visibility": "public"
}
]
有人可以帮助我创建规则和请求吗?(我正在使用此npm包)
这是我现在要检索自己创建的条目的查询
this.db.collection("my_collection").where('user_uid','==',this.$store.state.user.id).get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
//do something
});
});
这是为了创造
db.collection("my_collection").add({
user_uid: this.$store.state.user.id,
foo: this.foo,
bar: this.bar,
visibilty: this.visibility
})
这是我到目前为止的规则
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth.uid != null;
}
}
}
Firestore不会自动将文档与导致其创建的用户相关联。如果需要此类元数据,则需要像现在一样将其存储在每个文档中。
为了确保用户只能阅读自己的文档,您需要做两件事:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if request.auth != null && request.auth.uid == resource.data. user_uid;
}
}
}
有关更多信息,请参阅有关安全查询数据的文档。
要允许其他用例,您将需要扩展规则以也以类似方式允许该访问。但是您需要针对每种情况执行单独的查询,因为Firestore查询不能在多个字段上包含OR类型条件,而这正是您的用例所需要的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句