Search code examples
grails-spring-securitygrails-3.3.x

Grails 3.3.9 Spring Security UI 3.1.2 generate error for duplicate email


In my User.groovy domain class, I have added the field String email which I want to capture on registration. I have these constraints defined:

email email:true, blank: false, maxsize: 255, unique: true

When I register, if I try to reuse an existing username, I get a nice error next to the username field that says the username is taken. I see in the generated HTML that this comes from

<span class='s2ui_error'>The username is taken</span>

However when I try to reuse an existing email address, I get a black pane with yellow text in the top right of my screen that says:

"sorry there was a problem processing your registration".

This message only appears if there are no other errors.

I would like to cause a duplicate email error to generate a field error similar to the duplicate user name error. I cannot seem to locate where these additional tags are generated.

Thanks in advance for any advice.


Solution

  • 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:

    1. not sure of the protocol for including the license text at the top but I assume what I've done is correct;
    2. 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?