Cloud Firestore安全规则为生产代码中的resource.data提供null



我正在开发一款教育应用程序,该应用程序允许教师访问基于共同主题的学生作业文档。我为老师设置了一个自定义声明客户端,并设置了具有公共字段的文档,";主题";。我的规则是这样的:

match /assignments/{entry} {
allow read: if isSignedIn() && resource.data.subject == request.auth.token.subject;
allow write: if request.auth.token.moderator == true;

//TODO: assignment rules go here
function isSignedIn() {
return request.auth.uid != null;
}

}
}

尽管如此,我还是无法获得资源来提取任何数据。即使resource.data != null显示false。

我的自定义声明通过了罚款和request.auth.token.subject == "Biology"显示为真实的我的示例帐户。

以下是查询:

_guestBookSubscription = FirebaseFirestore.instance
.collection('assignments')
.orderBy('start_date', descending: true)
.limit(30)
.snapshots()
.listen((snapshot) {
_guestBookMessages = [];
for (final document in snapshot.docs) {
_guestBookMessages.add(RTIAssignment(
// endDate: (document.data()['end_date'] as DateTime),
standard: document.data()['standard'] as String,
startDate: DateTime.now(), // (document.data()['start_date']),
student: Student(name: document.data()['student_name']),
subject: document.data()['subject'] as String,
teacher: document.data()['name'] as String,
));
}
notifyListeners();
});

它来自谷歌发布的关于编写查询的漂亮代码实验室。

我不知道如何在查询的目标文档中进行复制。它在Firebase,其中一个字段是";主题";。

Firestore规则不用于对可以下载和无法下载的数据进行排序。您可能在不包含where()方法/函数的情况下查询数据。Firestore认为您想查询所有文档,所以他拒绝了整个查询。

包含在您的查询中:

JavaScript

const q = query(citiesRef, where("subject", "==", "Biology"));
...

db.collection("cities").whereField("subject", isEqualTo: "Biology")

第二种.whereField()方法可能适用于颤振,但我不知道。

规则应该是这样的:

match /databases/{database}/documents {
match /{document=**} { // here you restrict access to whole database
allow read, write: if false;
}
match /assignments/{docID} { // here except data to whole database was restricted you allow to read write documents in this collection path.
allow read: if isTeacher() || isModerator()
allow write: if isModerator()
}
function isTeacher() {
return request.auth.token.subject == resource.data.subject
}
function isModerator() {
return request.auth.token.moderator == true;
}
}

具有token.subject == "Biology"的教师只能请求字段为subject == "Biology"的文档,因此他们需要在查询中使用whereField()。如果教师科目是";数学"他只需要请求CCD_ 8的文档;其中字段((";他们可以读取整个数据。

相关内容

最新更新