Search code examples
grailsspring-securitygrails-plugingrails-spring-security

How can I store the hierarchic Authority Class in the Person Class?


my question is: (How?) can I store my "authority" for my user in the user class in Grails with the Spring Security Plugin?

I've given the following SQL structure:

User table:

...
varchar(255): username
...
int(2) rank: (refers to Rank table)

Rank table:

int(11): id
varchar(25): rank_name

Can I configure my domain classes, to work with that structure instead of the typical "User, Authority, UserAuthority many to many" schema?

The rank (id) is hierarchial, that means a user with the rank 4 got all "authorities" from a user with rank 3 etc.

EDIT: I simply replaced in the User Domain, the getAuthorities method with the following:

Set<Permission> getAuthorities() {
    Authority.findAllById(this.rank)
    // instead of: UserAuthority.findAllByUser(this)*.permission
}

Solution

  • Look at GormUserDetailsService to see how authorites are loaded. There's an authoritiesPropertyName configuration for springsecurity core, which specifies the property name of user domain which should return the authorities, the default value is 'authorities' -- you can modify this to return the authorities based on your domain model.

    If this does not satisfy your requirement then You can create a custom user details service too.

    And override the original user details service by defining a bean with name userDetailsService in your conf/spring/resources.groovy