开发者

Passing arguments to Grails custom validator

http://www.grails.org/doc/latest/ref/Constraints/validator.html

I have a project where I would like users to log in with a temporary password I provide them via开发者_开发技巧 SMS message. Thus, on login I would like to use the custom validator to check against the password I have created using a Java class.

class LoginCommand {

    String telephoneNumber;  /* the users telephone number, used for logging in */
    String password;         /* the password they entered, must match the generated pw below */

    /* the generated password, which has been sent to the user via SMS */
    String generatedPassword = PasswordGenerator.passwordGenerator(telephoneNumber)

    def jsecSecurityManager

    boolean authenticate() {
        def authToken = new UsernamePasswordToken(telephoneNumber, password)
        try {
            this.jsecSecurityManager.login(authToken)
            return true
        } catch (AuthenticationException ae) {
            return false
        }
    }

    static constraints = {
        telephoneNumber     blank:false
        password            blank:false

        /* the problem lies here, I need to pass that generatedPassword as an argument to the validator here, however I have no idea how to do that */
        password(validator: {
                if (!it.startsWith(generatedPassword)) return ['invalid.password']
        })
    }

}


The validator constraint takes an argument for the object being validated:

validator: { val, obj ->
    if(!val.startsWith(obj.generatedPassword)) ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜