@Test public void saveExistingUserButKeepPassword() throws IOException { UserForm user = new UserForm("user", "user", StringUtils.EMPTY, StringUtils.EMPTY, "email", true, null); BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(user, "userForm"); User oldUser = new User("user", "oldPW", "email", false); when(userStore.getUser("user")).thenReturn(oldUser); String view = userController.saveUser(user, bindingResult, authentication); assertEquals("/users", removeViewPrefix(view)); assertRedirect(view); assertFalse(bindingResult.hasErrors()); verify(userStore).saveUser(argUser("user", "oldPW", "email", true), same(USER)); }
@RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult, Authentication authentication) throws IOException { User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !form.getOriginalLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME) && StringUtils.equals(form.getLoginName(), UserStore.ANONYMOUS_USER_LOGIN_NAME)) { bindingResult.rejectValue("loginName", "user.loginName.invalid"); return "/user/edit"; } if (!form.getLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME)) { if (StringUtils.isNotBlank(form.getLoginName()) && (StringUtils.isBlank(form.getOriginalLoginName()) || !form.getLoginName().equals(form.getOriginalLoginName()))) { try { if (userStore.getUser(form.getLoginName()) != null) { bindingResult.rejectValue("loginName", "user.loginName.exists"); } } catch (UserNotFoundException e) { } } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword1())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2()) && !StringUtils.equals(form.getPassword1(), form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.passwordsNotEqual"); bindingResult.rejectValue("password2", "user.password.passwordsNotEqual"); } if (bindingResult.hasErrors()) { return "/user/edit"; } User existingUser = null; String password = null; if (StringUtils.isNotBlank(form.getOriginalLoginName())) { try { existingUser = userStore.getUser(form.getOriginalLoginName()); password = existingUser.getPassword(); } catch (UserNotFoundException e) { } } if (StringUtils.isNotBlank(form.getPassword1())) { password = passwordEncoder.encode(form.getPassword1()); } if (StringUtils.isBlank(password)) { bindingResult.rejectValue("password1", "user.password.blank"); bindingResult.rejectValue("password2", "user.password.blank"); } if (bindingResult.hasErrors()) { return "/user/edit"; } String newUserName = form.getOriginalLoginName(); if (StringUtils.isBlank(newUserName)) { newUserName = form.getLoginName(); } User newUser = new User(newUserName, password, form.getEmail(), form.isDisabled()); if (existingUser != null) { for (OpenId openId : existingUser.getOpenIds()) { newUser.addOpenId(openId); } } userStore.saveUser(newUser, user); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !StringUtils.equals(form.getLoginName(), form.getOriginalLoginName())) { userStore.renameUser(form.getOriginalLoginName(), form.getLoginName(), user); } } String[] authorityStrs = StringUtils.defaultString(form.getAuthorities()).split("\\|"); Set<RoleGrantedAuthority> authorities = Sets.newHashSet(); for (String authorityStr : authorityStrs) { if (StringUtils.isNotBlank(authorityStr)) { String[] parts = authorityStr.split(":"); Assert.isTrue(parts.length == 3); Type type = Type.valueOf(parts[0]); String targetId = parts[1]; String roleName = parts[2]; authorities.add(new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } userStore.saveUserAuthorities(form.getLoginName(), authorities, user); return "redirect:/users"; }
UserController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult, Authentication authentication) throws IOException { User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !form.getOriginalLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME) && StringUtils.equals(form.getLoginName(), UserStore.ANONYMOUS_USER_LOGIN_NAME)) { bindingResult.rejectValue("loginName", "user.loginName.invalid"); return "/user/edit"; } if (!form.getLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME)) { if (StringUtils.isNotBlank(form.getLoginName()) && (StringUtils.isBlank(form.getOriginalLoginName()) || !form.getLoginName().equals(form.getOriginalLoginName()))) { try { if (userStore.getUser(form.getLoginName()) != null) { bindingResult.rejectValue("loginName", "user.loginName.exists"); } } catch (UserNotFoundException e) { } } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword1())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2()) && !StringUtils.equals(form.getPassword1(), form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.passwordsNotEqual"); bindingResult.rejectValue("password2", "user.password.passwordsNotEqual"); } if (bindingResult.hasErrors()) { return "/user/edit"; } User existingUser = null; String password = null; if (StringUtils.isNotBlank(form.getOriginalLoginName())) { try { existingUser = userStore.getUser(form.getOriginalLoginName()); password = existingUser.getPassword(); } catch (UserNotFoundException e) { } } if (StringUtils.isNotBlank(form.getPassword1())) { password = passwordEncoder.encode(form.getPassword1()); } if (StringUtils.isBlank(password)) { bindingResult.rejectValue("password1", "user.password.blank"); bindingResult.rejectValue("password2", "user.password.blank"); } if (bindingResult.hasErrors()) { return "/user/edit"; } String newUserName = form.getOriginalLoginName(); if (StringUtils.isBlank(newUserName)) { newUserName = form.getLoginName(); } User newUser = new User(newUserName, password, form.getEmail(), form.isDisabled()); if (existingUser != null) { for (OpenId openId : existingUser.getOpenIds()) { newUser.addOpenId(openId); } } userStore.saveUser(newUser, user); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !StringUtils.equals(form.getLoginName(), form.getOriginalLoginName())) { userStore.renameUser(form.getOriginalLoginName(), form.getLoginName(), user); } } String[] authorityStrs = StringUtils.defaultString(form.getAuthorities()).split("\\|"); Set<RoleGrantedAuthority> authorities = Sets.newHashSet(); for (String authorityStr : authorityStrs) { if (StringUtils.isNotBlank(authorityStr)) { String[] parts = authorityStr.split(":"); Assert.isTrue(parts.length == 3); Type type = Type.valueOf(parts[0]); String targetId = parts[1]; String roleName = parts[2]; authorities.add(new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } userStore.saveUserAuthorities(form.getLoginName(), authorities, user); return "redirect:/users"; } }
UserController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult, Authentication authentication) throws IOException { User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !form.getOriginalLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME) && StringUtils.equals(form.getLoginName(), UserStore.ANONYMOUS_USER_LOGIN_NAME)) { bindingResult.rejectValue("loginName", "user.loginName.invalid"); return "/user/edit"; } if (!form.getLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME)) { if (StringUtils.isNotBlank(form.getLoginName()) && (StringUtils.isBlank(form.getOriginalLoginName()) || !form.getLoginName().equals(form.getOriginalLoginName()))) { try { if (userStore.getUser(form.getLoginName()) != null) { bindingResult.rejectValue("loginName", "user.loginName.exists"); } } catch (UserNotFoundException e) { } } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword1())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2()) && !StringUtils.equals(form.getPassword1(), form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.passwordsNotEqual"); bindingResult.rejectValue("password2", "user.password.passwordsNotEqual"); } if (bindingResult.hasErrors()) { return "/user/edit"; } User existingUser = null; String password = null; if (StringUtils.isNotBlank(form.getOriginalLoginName())) { try { existingUser = userStore.getUser(form.getOriginalLoginName()); password = existingUser.getPassword(); } catch (UserNotFoundException e) { } } if (StringUtils.isNotBlank(form.getPassword1())) { password = passwordEncoder.encode(form.getPassword1()); } if (StringUtils.isBlank(password)) { bindingResult.rejectValue("password1", "user.password.blank"); bindingResult.rejectValue("password2", "user.password.blank"); } if (bindingResult.hasErrors()) { return "/user/edit"; } String newUserName = form.getOriginalLoginName(); if (StringUtils.isBlank(newUserName)) { newUserName = form.getLoginName(); } User newUser = new User(newUserName, password, form.getEmail(), form.isDisabled()); if (existingUser != null) { for (OpenId openId : existingUser.getOpenIds()) { newUser.addOpenId(openId); } } userStore.saveUser(newUser, user); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !StringUtils.equals(form.getLoginName(), form.getOriginalLoginName())) { userStore.renameUser(form.getOriginalLoginName(), form.getLoginName(), user); } } String[] authorityStrs = StringUtils.defaultString(form.getAuthorities()).split("\\|"); Set<RoleGrantedAuthority> authorities = Sets.newHashSet(); for (String authorityStr : authorityStrs) { if (StringUtils.isNotBlank(authorityStr)) { String[] parts = authorityStr.split(":"); Assert.isTrue(parts.length == 3); Type type = Type.valueOf(parts[0]); String targetId = parts[1]; String roleName = parts[2]; authorities.add(new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } userStore.saveUserAuthorities(form.getLoginName(), authorities, user); return "redirect:/users"; }  }
UserController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult, Authentication authentication) throws IOException { User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !form.getOriginalLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME) && StringUtils.equals(form.getLoginName(), UserStore.ANONYMOUS_USER_LOGIN_NAME)) { bindingResult.rejectValue("loginName", "user.loginName.invalid"); return "/user/edit"; } if (!form.getLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME)) { if (StringUtils.isNotBlank(form.getLoginName()) && (StringUtils.isBlank(form.getOriginalLoginName()) || !form.getLoginName().equals(form.getOriginalLoginName()))) { try { if (userStore.getUser(form.getLoginName()) != null) { bindingResult.rejectValue("loginName", "user.loginName.exists"); } } catch (UserNotFoundException e) { } } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword1())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2()) && !StringUtils.equals(form.getPassword1(), form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.passwordsNotEqual"); bindingResult.rejectValue("password2", "user.password.passwordsNotEqual"); } if (bindingResult.hasErrors()) { return "/user/edit"; } User existingUser = null; String password = null; if (StringUtils.isNotBlank(form.getOriginalLoginName())) { try { existingUser = userStore.getUser(form.getOriginalLoginName()); password = existingUser.getPassword(); } catch (UserNotFoundException e) { } } if (StringUtils.isNotBlank(form.getPassword1())) { password = passwordEncoder.encode(form.getPassword1()); } if (StringUtils.isBlank(password)) { bindingResult.rejectValue("password1", "user.password.blank"); bindingResult.rejectValue("password2", "user.password.blank"); } if (bindingResult.hasErrors()) { return "/user/edit"; } String newUserName = form.getOriginalLoginName(); if (StringUtils.isBlank(newUserName)) { newUserName = form.getLoginName(); } User newUser = new User(newUserName, password, form.getEmail(), form.isDisabled()); if (existingUser != null) { for (OpenId openId : existingUser.getOpenIds()) { newUser.addOpenId(openId); } } userStore.saveUser(newUser, user); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !StringUtils.equals(form.getLoginName(), form.getOriginalLoginName())) { userStore.renameUser(form.getOriginalLoginName(), form.getLoginName(), user); } } String[] authorityStrs = StringUtils.defaultString(form.getAuthorities()).split("\\|"); Set<RoleGrantedAuthority> authorities = Sets.newHashSet(); for (String authorityStr : authorityStrs) { if (StringUtils.isNotBlank(authorityStr)) { String[] parts = authorityStr.split(":"); Assert.isTrue(parts.length == 3); Type type = Type.valueOf(parts[0]); String targetId = parts[1]; String roleName = parts[2]; authorities.add(new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } userStore.saveUserAuthorities(form.getLoginName(), authorities, user); return "redirect:/users"; }  @RequestMapping(value="/add", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String addUser(Model model); @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/edit/{loginName:" + DocumentrConstants.USER_LOGIN_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String editUser(@PathVariable String loginName, Model model); @RequestMapping(value="/delete/{loginName:" + DocumentrConstants.USER_LOGIN_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN) and !isAdmin(#loginName)") String deleteUser(@PathVariable String loginName, Authentication authentication); @ModelAttribute UserForm createUserForm(@RequestParam(required=false) String loginName,
			@RequestParam(required=false) String originalLoginName,
			@RequestParam(required=false) String password1, @RequestParam(required=false) String password2,
			@RequestParam(required=false) String email, @RequestParam(required=false) boolean disabled,
			@RequestParam(required=false) String authorities); }
UserController { @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") public String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult, Authentication authentication) throws IOException { User user = userStore.getUser(authentication.getName()); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !form.getOriginalLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME) && StringUtils.equals(form.getLoginName(), UserStore.ANONYMOUS_USER_LOGIN_NAME)) { bindingResult.rejectValue("loginName", "user.loginName.invalid"); return "/user/edit"; } if (!form.getLoginName().equals(UserStore.ANONYMOUS_USER_LOGIN_NAME)) { if (StringUtils.isNotBlank(form.getLoginName()) && (StringUtils.isBlank(form.getOriginalLoginName()) || !form.getLoginName().equals(form.getOriginalLoginName()))) { try { if (userStore.getUser(form.getLoginName()) != null) { bindingResult.rejectValue("loginName", "user.loginName.exists"); } } catch (UserNotFoundException e) { } } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword1())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isBlank(form.getOriginalLoginName()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isBlank(form.getPassword2())) { bindingResult.rejectValue("password2", "user.password.blank"); } if (StringUtils.isNotBlank(form.getPassword1()) && StringUtils.isNotBlank(form.getPassword2()) && !StringUtils.equals(form.getPassword1(), form.getPassword2())) { bindingResult.rejectValue("password1", "user.password.passwordsNotEqual"); bindingResult.rejectValue("password2", "user.password.passwordsNotEqual"); } if (bindingResult.hasErrors()) { return "/user/edit"; } User existingUser = null; String password = null; if (StringUtils.isNotBlank(form.getOriginalLoginName())) { try { existingUser = userStore.getUser(form.getOriginalLoginName()); password = existingUser.getPassword(); } catch (UserNotFoundException e) { } } if (StringUtils.isNotBlank(form.getPassword1())) { password = passwordEncoder.encode(form.getPassword1()); } if (StringUtils.isBlank(password)) { bindingResult.rejectValue("password1", "user.password.blank"); bindingResult.rejectValue("password2", "user.password.blank"); } if (bindingResult.hasErrors()) { return "/user/edit"; } String newUserName = form.getOriginalLoginName(); if (StringUtils.isBlank(newUserName)) { newUserName = form.getLoginName(); } User newUser = new User(newUserName, password, form.getEmail(), form.isDisabled()); if (existingUser != null) { for (OpenId openId : existingUser.getOpenIds()) { newUser.addOpenId(openId); } } userStore.saveUser(newUser, user); if (StringUtils.isNotBlank(form.getOriginalLoginName()) && !StringUtils.equals(form.getLoginName(), form.getOriginalLoginName())) { userStore.renameUser(form.getOriginalLoginName(), form.getLoginName(), user); } } String[] authorityStrs = StringUtils.defaultString(form.getAuthorities()).split("\\|"); Set<RoleGrantedAuthority> authorities = Sets.newHashSet(); for (String authorityStr : authorityStrs) { if (StringUtils.isNotBlank(authorityStr)) { String[] parts = authorityStr.split(":"); Assert.isTrue(parts.length == 3); Type type = Type.valueOf(parts[0]); String targetId = parts[1]; String roleName = parts[2]; authorities.add(new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } userStore.saveUserAuthorities(form.getLoginName(), authorities, user); return "redirect:/users"; }  @RequestMapping(value="/add", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String addUser(Model model); @RequestMapping(value="/save", method=RequestMethod.POST) @PreAuthorize("hasApplicationPermission(ADMIN)") String saveUser(@ModelAttribute @Valid UserForm form, BindingResult bindingResult,
			Authentication authentication); @RequestMapping(value="/edit/{loginName:" + DocumentrConstants.USER_LOGIN_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN)") String editUser(@PathVariable String loginName, Model model); @RequestMapping(value="/delete/{loginName:" + DocumentrConstants.USER_LOGIN_NAME_PATTERN + "}", method=RequestMethod.GET) @PreAuthorize("hasApplicationPermission(ADMIN) and !isAdmin(#loginName)") String deleteUser(@PathVariable String loginName, Authentication authentication); @ModelAttribute UserForm createUserForm(@RequestParam(required=false) String loginName,
			@RequestParam(required=false) String originalLoginName,
			@RequestParam(required=false) String password1, @RequestParam(required=false) String password2,
			@RequestParam(required=false) String email, @RequestParam(required=false) boolean disabled,
			@RequestParam(required=false) String authorities);  }