Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data REST filtering data based on the user

If I have a repository setup like the following, making use of Spring Data REST, I can access the data at /receipts and see all data. However, I want to only return data for the user. I have a custom finder "findByStorer" which would do this. How would I get Spring Data REST to use this and get the storer value from the user rather than specifying a query parameter?

@RepositoryRestResource(collectionResourceRel = "receipts", path = "receipts")
public interface ReceiptRepository extends PagingAndSortingRepository<Receipt, BigDecimal> {

    @Query
    public Page<Receipt> findByStorer(String storer, Pageable pageable);
}

I haven't implemented any security yet, so this question is more theory at the moment than practice.

Thanks.

like image 442
Patrick Grimard Avatar asked May 13 '14 19:05

Patrick Grimard


1 Answers

Building on @rpr's answer:

You should be able to reference properties of the joined entity (Storer). In your example if you have Receipt -> Storer -> User you can query the Receipts where Storer.user has a value injected from the Security Context.

@PreAuthorize("isFullyAuthenticated && (#userName==principal.username)")
Page<Receipt> findByStorer_User(@Param("userName") String userName)
like image 75
Ben M Avatar answered Oct 07 '22 05:10

Ben M