我想在插件提供的用戶註冊字段中添加其他字段。我相信重寫RegisterController是添加更多字段的正確方法(糾正我,如果我錯了)。我從原始文件創建了一個新的RegisterController.groovy,其中包含以下內容。此時我沒有添加新的字段屬性,只是試圖模仿插件的功能。Grails Spring Security UI註冊其他字段
package myPackage
import grails.plugin.springsecurity.authentication.dao.NullSaltSource
import grails.plugin.springsecurity.ui.CommandObject
import grails.plugin.springsecurity.ui.RegistrationCode
import grails.plugin.springsecurity.ui.strategy.MailStrategy
import grails.plugin.springsecurity.ui.strategy.PropertiesStrategy
import grails.plugin.springsecurity.ui.strategy.RegistrationCodeStrategy
class RegisterController extends grails.plugin.springsecurity.ui.RegisterController {
static defaultAction = 'register'
def saltSource
MailStrategy uiMailStrategy
RegistrationCodeStrategy uiRegistrationCodeStrategy
PropertiesStrategy uiPropertiesStrategy
def register(RegisterCommand registerCommand) {
if (!request.post) {
return [registerCommand: new RegisterCommand()]
}
if (registerCommand.hasErrors()) {
return [registerCommand: registerCommand]
}
def user = uiRegistrationCodeStrategy.createUser(registerCommand)
String salt = saltSource instanceof NullSaltSource ? null : registerCommand.username
RegistrationCode registrationCode = uiRegistrationCodeStrategy.register(user, registerCommand.password, salt)
if (registrationCode == null || registrationCode.hasErrors()) {
// null means problem creating the user
flash.error = message(code: 'spring.security.ui.register.miscError')
return [registerCommand: registerCommand]
}
sendVerifyRegistrationMail registrationCode, user, registerCommand.email
[emailSent: true, registerCommand: registerCommand]
}
}
class RegisterCommand implements CommandObject {
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'
}
}
email email: true
password validator: RegisterController.passwordValidator
password2 nullable: true, validator: RegisterController.password2Validator
}
}
當我運行這個程序,我得到一個錯誤
Cannot invoke method findWhere() on null object.
任何想法來解決這個問題,將不勝感激。
你是否希望這個額外的字段在數據庫中持久化在用戶域類? –
是的,那是目標。我還需要弄清楚如何使註冊頁面遵守用戶域類的約束。 – elixir