We are migrating from Firestore to MongoDB.
In Firestore they have security rules which specify which role can access which set of collections / sub-collections, documents, or even specific fields in a document
Example :
match / databases / { database } / documents {
match / student / { documentId }{
allow read;
allow write: if ((isSuperAdmin() || isManager() || (isTeacher() && request.resource.data.diff(resource.data).affectedKeys().hasOnly(['grade'])) && noStudentWithSameDocId())
}
function noStudentWithSameDocId() {
return get(/databases/$(database) / documents / student / $(documentId)) == null;
}
}
so for example we have a student collection
const student = {
name: string
class: string
grade: number
fees: number
}
and 3 roles :
super_admin
manager
teacher
super_admin role can edit all the fields in the student document and can delete a student document
the manager role can edit all the fields in the student document but cannot delete a student document
the teacher role can only edit the grade field in the student document (cannot delete the document)
and cannot read the fees field from the student
- what is the best way of achieving this?
- My take so far is to have it in consecutive middleware
- but is there any library or MongoDB built-in tool that eases this?
Thank you in advance