@RequestMapping(value = "/register", method = RequestMethod.POST) public String register( @ModelAttribute("UserForm") UserForm userForm, BindingResult result, HttpServletRequest request, Model model, @RequestParam("recaptcha_challenge_field") String challangeField, @RequestParam("recaptcha_response_field") String responseField, RedirectAttributes attributes, HttpServletResponse response) throws IOException { // check captcha String remoteAddress = request.getRemoteAddr(); ReCaptchaResponse reCaptchaResponse = this.reCaptcha.checkAnswer(remoteAddress, challangeField, responseField); if (!reCaptchaResponse.isValid()) { model.addAttribute(PathHolder.ATTRIBUTE_NAME__MESSAGE, PathHolder.MESSAGE__WRONG_CAPTCHA); model.addAttribute(PathHolder.ATTRIBUTE_NAME__USERFORM, userForm); LOG.info(PathHolder.MESSAGE__WRONG_CAPTCHA); return PathHolder.PATH__REGISTRATION_PAGE; } // validate form new UserFormValidator().validate(userForm, result); if (result.hasErrors()) { model.addAttribute(PathHolder.ATTRIBUTE_NAME__MESSAGE, PathHolder.MESSAGE__WRONG_USER_DATA); model.addAttribute(PathHolder.ATTRIBUTE_NAME__USERFORM, userForm); LOG.warn(PathHolder.MESSAGE__WRONG_USER_DATA); return PathHolder.PATH__REGISTRATION_PAGE; } // check login String login = userForm.getLogin(); try { if (!userService.checkLogin(userForm.getLogin())) { model.addAttribute(PathHolder.ATTRIBUTE_NAME__MESSAGE, PathHolder.MESSAGE__LOGIN_BUSY); model.addAttribute(PathHolder.ATTRIBUTE_NAME__USERFORM, userForm); LOG.warn("Can't check login " + login); return PathHolder.PATH__REGISTRATION_PAGE; } // if OK userService.createUser(createUser(userForm)); } catch (Exception e) { model.addAttribute( PathHolder.ATTRIBUTE_NAME__MESSAGE, PathHolder.MESSAGE__INTERNAL_SERVICE_ERROR); model.addAttribute(PathHolder.ATTRIBUTE_NAME__USERFORM, userForm); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); LOG.error("Can't save new user " + userForm.getLogin(), e); return PathHolder.PATH__REGISTRATION_PAGE; } attributes.addFlashAttribute( PathHolder.ATTRIBUTE_NAME__MESSAGE, PathHolder.MESSAGE__REGISTRATION_COMPLETE); return PathHolder.PATH__REDIRECT + PathHolder.PATH__LOGIN_PAGE; }
private User createUser(UserForm form) throws Exception { Role role = roleService.getRoleByName(form.getRoleName()); return new User.UserBuilder() .login(form.getLogin()) .password(form.getPassword()) .email(form.getEmail()) .firstName(form.getFirstName()) .lastName(form.getLastName()) .phone(form.getPhone()) .registration(new Date()) .status(UserStatus.DISABLED) .role(role) .build(); }