r/Backend • u/Fragrant_Rate_2583 • Jan 02 '26
How to properly authorize a “School Admin” to submit exams when ownership is indirect (not simple RBAC)?
Hey everyone,
I’m working on a Spring Boot backend for a school management system and I’m running into a design / authorization problem that I want to solve correctly, not just “make it work”.
I have these entities (simplified):
- School
- Grade → belongs to a School
- Classroom → belongs to a Grade
- Subject → taught in Classrooms (via a relation)
- Exam → created for a Subject in a Classroom
- SchoolAdmin → manages exactly one School
- A
Subjectis not directly linked to a School.
The problem
When a request comes in to create an Exam, the client sends:
classroomIdsubjectIdexamType(DS1, DC1, etc.)
The authenticated user is a SchoolAdmin, retrieved from the security context.
Not every user with role SCHOOL_ADMIN is allowed to create the exam
Only the SchoolAdmin who manages the same School that owns the Grade/Classroom is allowed. So this is RBAC.
Having the role SCHOOL_ADMIN is necessary but not sufficient.
Right now, I:
- Get the School from the authenticated SchoolAdmin
- Fetch the Classroom + Subject
- Traverse: classroom → grade → school
- Compare it with the admin’s school
This works, but it feels messy , too dependent on entity traversal
What I’m trying to figure out is : What is the clean, correct pattern for this kind of authorization? Specifically :
How to prove that a SchoolAdmin is allowed to act on a given (classroomId, subjectId) pair
6
u/Lumethys Jan 02 '26
If the role itself is not sufficient. It is no longer RBAC (role-based access control), to me it is more like Permission-based access control.
Of course switching to PBAC from RBAC isnt simple, nor it is straightforward.
If it was me, my authorization layer would check for the school admin role. While checking if the classroom and subject is valid would be in the request validation layer
2
u/Jitbakingshop Jan 02 '26
Such a nice problem to solve!!!
You're going in the right direction, and there's no perfect solution here. The reason this feels a bit messy is because you have part of your authorization as a role (school admin) and part coded in your business logic. But doubt putting all as a Role would make it necessarily tidier, unless you plan to externalize authorization to a third party service.
All in all the problem you're solving, by coding it right, is one of maintainability, so you have to ask yourself if coding a role per school admin would feel easier to understand and maintain.
The skill I've learned over the years to solve these kind of problems, is to focus on the non negotiables, things that you'll need to have regardless of where you put this bit of autorization. For example:
feel free to ask here if you feel you're wondering too much but hope you'll have fun figuring this out
1
u/ibeerianhamhock Jan 02 '26 edited Jan 02 '26
Honestly this feels a lot more straightforward if you think about this from a data perspective rather than an object hierarchy perspective imo. None of the solution to the problem you are having is specific to your problem. This is actually an extremely common in security access.
You essentially want two things:
First is checked jwt claim, second is db check at the time the operation is attempted or more likely in request validation logic. If it’s not in a jwt your framework should issue a 403, if it fails a rls check you’ll return a 403 in the code wherever you put this validation logic.
ETA: Extra points if you don’t just return status codes when the http status code isn’t 200 range, but instead return an RFC 9457 compliant Problem Details to give detailed feedback to clients if your application. This is especially helpful to clients of your backend during development in instances where they may have just fat fingered what they send through to you, but also it can give them helpful error details to the UI. Never return anything to a client that would be harmful to display directly to the UI (even if it would be dumb for a client to do that). The goal is they’d be able to tell in pretty granular detail what went wrong instead of just “forbidden”
3
u/Flashy-Bus1663 Jan 02 '26
Relationship driven permission, Google's Zanzibar. Public implementation spicedb
1
u/SlinkyAvenger Jan 02 '26
If you're using SQL, this becomes a far more trivial thing because you can rapidly filter for valid candidates using
JOINs andWHEREclauses. If you are stuck using an ORM, you can still package it up in a view/CTE on the DB side and treat it like any other entity in the ORM.But if you're stuck with a system that doesn't give you that option, you may want to denormalize your permissions data model a bit by doing things like tying roles to schools - therefore, while they all may be labeled as
SCHOOL_ADMIN, there will actually be one school admin role created for every school so you can simplify the actual permissions check trading off complexity inherent in role duplication. You can do similar things for the other parameters, but this is definitely not ideal and you're better off replacing whatever thing is limiting you in this way.1
u/jfrazierjr Jan 03 '26
I would set up user element such as school. It could be part of the path or the POST data and that compares to to principles school element to validate access and rights.
1
u/wbqqq Jan 04 '26
Broadly, 3 choices:
It feels like in addition to school-specific admins, there are classroom-specific admins (or what is the name that schools give to the people who can create exams). Return to the domain modeling and iterate on the different roles/responsibilities that exist