简体   繁体   中英

Fetch persons with specific authorities - spring security with grails

I have typical Spring Security domain class layout (Person, Authority and join table PersonAuthority). I made following query, but I don't know how to build properly criteria for fetching persons that have only specific authorities (last 'if' block).

public static def query(Map params=[:]){
    def criteria = Person.createCriteria();
    def rows = criteria.list(max:params.max,offset:params.offset){
    order(params.column?:"id",params.order?:"asc")
        if(params.id){
            idEq (params.id as Long);
        }        
        if(params.username){
            ilike "username","%"+params.username+"%"    
        }
        if(params.email){
            ilike "email","%"+params.email+"%"
        }
        if(params.accountLocked){
            eq "accountLocked",Boolean.valueOf(params.accountLocked)
        }
        if(params.enabled){
            eq "enabled",Boolean.valueOf(params.enabled)
        }
        if(params.passwordExpired){
            eq "passwordExpired",Boolean.valueOf(params.passwordExpired)
        }
        if(params.authorities){
            inList "authority",params.authorities;
        }
    }
    return rows;
}

For this query I get org.hibernate.QueryException: could not resolve property: authority

If you haven't modified anything form the default Spring Security options and classes, then I suppose you don't have an authority or authorities fields.

However, you should have a method in your Person class called getAuthorities() that returns a set of Authorities

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM