【发布时间】:2019-05-30 11:39:04
【问题描述】:
我有一个名为“客户”的集合,并且我已向某些用户授予对该集合的读写权限。我的要求是我必须一次将删除操作限制为一个文档,以避免大规模删除收藏。如何使用 Firestore 安全规则来实现这一点。现在我已经尝试使用以下代码
match /customers/{customer=**} {
function isSignedIn() {
return request.auth != null;
}
function isRole(roles) {
return get(/databases/$(database)/documents/Teachers/$(request.auth.uid)).data.permissions.pages.hasAny(roles);
}
function isSize(size){
return request.resource.data.size()==size;
}
allow read: if isSignedIn()&&isRole(['create','modify']);
allow create,update:if isSignedIn()&&isRole(['create','modify']);;
allow delete:if isSignedIn()&&isSize(1)
}
这里是删除操作的代码;
async(id:string){
const batch=afs.firestore.batch();
const coll=await afs.firestore.collection('customers').ref.limit(1).where('id','==',id).get();
for(const i of coll.docs){
const ref= await afs.firestore.collection('customers').doc(i.id);
await batch.delete(ref);
}
return await batch.commit();
}
但上述规则在进行删除操作时总是返回权限被拒绝。
【问题讨论】:
-
您能否编辑问题以显示您试图阻止的“批量删除”操作的示例?
-
批量删除的场景比较少,但是这里要防止意外批量删除;
-
再一次,您要阻止的具体查询是什么?
-
请注意,
batch.dalete(i.ref);不是有效的 API 调用。如果您共享的代码是您实际尝试使用的代码,那真的很有帮助。
标签: javascript google-cloud-firestore angular6 firebase-security