我尝试制定手动规则我有两个集合,一个用于保存用户数据,如电话、电子邮件等名称(用户注册),其他集合名称幻灯片也有许多字段数据。所以现在我尝试做的事情是我需要制定一些规则,例如如果用户需要首先通过 firebase 规则从 SlideShow 集合中删除数据,请检查该用户是否在集合 user_registration 中按字段名称(Uid)可用,以及他是否具有(的规则) admin) 按字段名称角色。
我尝试这样做:
match /SlideShow/{SlideShowId} {
allow read: if true;
allow write, update, delete: if get(/databases/$(database)/documents/user_registration/$(request.resource.data.Uid)).data.role == 'admin';
}
和颤振代码:
Future deleteItem( ) async {
try {
DocumentSnapshot userDoc = await FirebaseFirestore.instance
.collection('user_registration')
.doc(Uid)
.get();
if (userDoc['role'] == 'admin') {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('SlideShow')
.where('IDSlideShow', isEqualTo: int.parse(widget.IDSlideShow))
.get();
for (DocumentSnapshot docSnapshot in querySnapshot.docs) {
await docSnapshot.reference.delete();
}
print('Documents deleted successfully.');
} else {
print('User does not have permission to delete.');
}
} catch (e) {
print('Error deleting documents: $e');
}
}
但是我每次都会遇到这个错误:
W/Firestore( 6815): (25.1.1) [WriteStream]: (9d81ab6) Stream closed with status: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}.
W/Firestore( 6815): (25.1.1) [Firestore]: Write failed at SlideShow/tfo1LvgdtZYOEVLawm1k: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}
I/flutter ( 6815): Error deleting documents: [cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.
和(幻灯片)集合的图像
我没有将 uid 添加到 SlideShow 我需要让用户可以删除他想要的任何文档。
有人能解决这个问题吗?
您的
SlideShow
文档没有 Uid
字段,您的规则使用该字段来确定要从 user_registration
集合中读取的文档。由于该字段不存在,因此该规则失败,写入被拒绝。
如果您想将
user_registration
文档用于当前登录的用户,其表达式为:
request.auth.uid