I'm looking for a way to authorize access based on ROLES and DATA CONTENT (table row) using Spring Boot & JPA
The use case is the following:
Given a User with a Role 'ROLE_AAA'
When user fetches data from table 'REPORT'
Then only reports with specific 'REPORT_ROLE' association will be returned
ER
Table: role
| ID | ROLE |
|---|---|
| 1 | ROLE_AAA |
| 2 | ROLE_BBB |
Table: report
| ID | CONTENT |
|---|---|
| 1 | ... |
| 2 | ... |
| 3 | ... |
| 4 | ... |
| 5 | ... |
Table: report_roles
| REPORT_ID | ROLE_ID |
|---|---|
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 1 |
In this case a user with role ROLE_AAA will get reports with ID 1,2 & 5 only.
SELECT re.*
FROM report re
JOIN report_role rr
ON re.id = rr.report_id
JOIN role ro
ON ro.id = rr.role_id
WHERE ro.name = 'ROLE_AAA'
I could easily write queries (@org.springframework.data.jpa.repository.Query) joining on table I want to get data from, with the association role table, but I feel I'm missing a JPA or Spring Security feature that might offer it out off the box.
Any design/implementation suggestion will be very appreciated.
UPDATE
Since returns will be paginated (retuning Page<Object> from JPARepository), I believe PostFiltering is not an option here.