1

I am having a typical application where a user can be in multiple groups, and of course, a group can hold multiple users:

User <-> GroupMember (ManyToMany) <-> Group

Also, a GroupMember can have other relations for example it can have an item which belongs to it - but only in the context of that particular group:

GroupMember <- OneToMany <- GroupMemberItem

My first question would be this: Let's say there is a GroupRepository

@RepositoryRestResource(collectionResourceRel = "groups", path = "groups")
public interface GroupRepository extends JpaRepository<GroupEntity, Long> {
}

How would one fetch only the groups the user is actually assigned to and make it impossible for another user to access groups he is not.

Are @RepositoryRestResource / JpaRepository actullay designed for this? I am absolutely against writing @Query annotations where I end up writing queries which imho defy the whole purpose of why I would like to use them in the first place.

So if I told you

"I want http://localhost:8080/groups to return all the groups that the current logged in user is logged in"

would you recommend me to go the @RestController, @Service, @Repository where I just implement the query by myself where might even end up switching up to something like jOOQ at this point or would you tell me to go for @RepositoryRestResource because there is a very easy way to accomplish that without having to write complex queries inside that Java interface?


The second question is kind of a follow up to the first one. Having said that a GroupMember can own one or many GroupMemberItem, I am having struggles to understand how @RepositoryRestResource can be used in order to fetch all the items from the logged in user in a selected group. Just writing the SQL query is not the problem but can this be done with @RepositoryRestResource / JpaRepository? If yes, how? I cannot find a single example and I've been looking for quite a while now.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
  • Possible duplicate of [Spring Data JPA and spring-security: filter on database level (especially for paging)](https://stackoverflow.com/questions/15108543/spring-data-jpa-and-spring-security-filter-on-database-level-especially-for-pa) – Jens Schauder Apr 07 '18 at 05:22
  • Maybe my answer will be helpful: https://stackoverflow.com/a/46758549 – Cepr0 Apr 07 '18 at 08:27
  • 1
    @Cepr0 Thanks for the link but if I had to do it like this I would actually move to something like jOOQ - I wouldn't want to write my queries like that to be honest. – Stefan Falk Apr 07 '18 at 08:44

0 Answers0