Search code examples
grailsgrails-ormgrails-2.0

GORM get domain object list by property


I'm trying to get a list of domain objects back, but only those that the user is allowed to see. This is based on what granted role that user has from spring security.

What I'd like to be able to do in my controller is something along the lines of

[reportInstanceList: Report.list(params).sort{it.name}]

but only get the reports where

Report.role = SecurityContextHolder.getContext().getAuthentication().getAuthorities()

I have the Spring security stuff in another service class, but for simplicity's sake, its inline here.

Is there a way to direct GORM to only pull the records where the roles match?


Solution

  • Something like:

    def reports = Report.createCriteria().list( params ) {
        'in'( "role", SpringSecurityUtils.principalAuthorities )
    }
    [ reportInstanceList: reports, totalCount: reports.totalCount ]
    

    Should work...