2
votes

I have installed Spring Security UI. It is working with the default fields:

  • username
  • email
  • password
  • password2

I want to add more fields. For example:

  • phoneNumber
  • grailsIsCool

So I type:

grails s2ui-override register packageName

It creates an empty class which extends: grails.plugins.springsecurity.ui.RegisterController.

Is the correct way to do it to copy&paste all the code of the extended class where the default fields appear together (username, email, password and password2) Then, adding the new fields after them? (phoneNumber and grailsIsCool). Alain Perry followed this approach and posted is code here: http://grails.1312388.n4.nabble.com/Using-spring-security-UI-with-customized-user-class-td2330737.html

1

1 Answers

2
votes

I think this is a good question.

I started using Grails about 5 months ago, and one of the first things I did was to install Spring Security and Spring Security UI. I initially used the RegisterController provided by the plugin, however I ended up rewriting most of it.

The UI RegisterController contains a lot a business logic - I prefer to keep my controllers as dumb as possible, so I created a RegistrationService and moved the RegisterController code I needed there.

Another gotcha was the RegisterController's use of the grails command object pattern. I ran into problems when I deployed my app to CloudFoundry and couldn't figure out why. I later read that other people had experienced the same issue. Simple workaround, but it left me scratching my head for a while...

Hope this helps.

EDIT:

So, on to your actual question. I guess the approach I took when I needed to add fields and otherwise customise the way the plugin worked was to just write my own implementation.

You can override the default Spring Security UI User, Role and UserRole classes by adding this to Config.groovy:

grails.plugins.springsecurity.userLookup.userDomainClassName = 'com.myapp.MyUser'
grails.plugins.springsecurity.userLookup.authorityJoinClassName = 'com.myapp.MyUserRole'
grails.plugins.springsecurity.authority.className = 'com.myapp.MyRole'

If you need additional fields, just add them to your User domain class, you'll also need to add validation logic to the RegisterController's Commands to deal with these additional fields.

As for pulling your Commands out as top level classes, I just keep mine in the same package as the controller. One thing to note is that since they're no longer inside your controller, they will need to have a @grails.validation.Validateable annotation added to them otherwise they won't have the various validation methods available to them at runtime.