A sample application showing how to infer one or more roles from persisted user information without explicitly storing roles in the traditional many-to-many format.
In this example there isn’t even a Role or UserRole domain class, only the User class; and all of the work is done in autorole.AutoRoleUserDetailsService, the custom UserDetailsService implementation.
A more realistic implementation would probably use a hybrid approach, storing some role information in the database and inferring the rest, with the UserDetailsService merging the "real" and "virtual" roles as needed.
Items of note:
-
test.Useris the user domain class generated by thes2-quickstartscript with one modification, adding aboolean adminproperty -
the Role and UserRole classes generated by the
s2-quickstartscript were deleted since they’re not used -
two users are created in
BootStrap.groovy; user “admin” (password “password”) has theadminboolean set totrueand will be auto-grantedROLE_ADMIN, and user “user” (password “password”) has the default value foradmin(false) which will result in a grant ofROLE_USER -
autorole.AutoRoleUserDetailsServiceis registered in grails-app/conf/spring/resources.groovy as theuserDetailsServicebean -
secured.SecureControllerhas two annotated actions;/securerequiresROLE_USER(orROLE_ADMINsince hierarchical roles are configured) and/secure/adminrequiresROLE_ADMIN -
debug/trace logging for the plugin and Spring Security is configured but commented out in
logback.groovy -
the application is intentionally stripped-down:
-
there are no static resources
-
the GSPs are very minimal
-
all unused attributes were removed from the
grails.plugin.springsecurityblock inapplication.groovy(even includinguserLookup.userDomainClassName, sinceAutoRoleUserDetailsServiceworks directly with theUserclass)
-