After a lot of trial and error, I took what I had learned and "went back to the drawing board". I decided that the best possible outcome would be to make this work simply by replacing the default RegisterCommand.groovy that "comes with" Spring Security UI with one of my own that includes the check for duplicate email.
I also decided to get rid of the stub RegisterController.groovy provided by the s2ui-override process, just in case that interfered.
This approach works. The new RegisterCommand.groovy
goes in
src/main/groovy/grails/plugin/springsecurity/ui
in the project directory and it looks like this:
/* Copyright 2015-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.plugin.springsecurity.ui
class RegisterCommand implements CommandObject, grails.validation.Validateable {
protected static Class<?> User
protected static String usernamePropertyName
String username
String email
String password
String password2
static constraints = {
username validator: { value, command ->
if (!value) {
return
}
if (User.findWhere((usernamePropertyName): value)) {
return 'registerCommand.username.unique'
}
}
// Original begins
// email email: true
// Original ends
// Modifications begin
email email: true, validator: { value, command ->
if (!value) {
return
}
if (User.findWhere('email': value)) {
return 'registerCommand.email.unique'
}
}
// Modifications end
password validator: RegisterController.passwordValidator
password2 nullable: true, validator: RegisterController.password2Validator
}
}
Two comments worth making:
- not sure of the protocol for including the license text at the top but I assume what I've done is correct;
- it's possible that I could have declared a static String emailPropertyName at the top instead of using a hard-coded key (and depended on some other piece of code to materialize the correct value) but for now this seemed the most straightforward solution.
One comment perhaps not work making - in all the years that this wonderful Spring Security UI has been available, why hasn't this already been discovered and documented?