@Action(name = "/{username}/edit/status", method = HttpMethod.POST) public ActionResult updateUserStatus( @ActionParam("id") final String id, @ActionParam("status") int status) { User u = new DefaultUser() { { setId(id); } }; u = userManager.updateStatus(u, status); String redirectUri = "/admin/users/" + u.getUsername() + "/edit?success"; return new ActionResult("redirect", redirectUri); }
@Action(name = "/{username}/edit", method = HttpMethod.POST) public ActionResult updateUser( @ActionParam("username") String username, @ActionParam("user") DefaultUser user, @ActionParam("roles") String[] roles, @ActionParam("confirmPassword") String confirmPassword) { validateUser(user, confirmPassword); if (hasFieldErrors()) { ActionResult actionResult = new ActionResult("freemarker", "/view/admin/user/user-form.ftl"); showRoles(actionResult, username); return actionResult; } User u = userManager.saveUser(user); String redirectUri = "/admin/users/" + u.getUsername() + "/edit?success"; if (StringUtils.equalsIgnoreCase(username, "-")) { redirectUri = "/admin/users?success"; } else { for (UserRole r : userManager.findRoleByUser(u, null)) { userManager.removeRoleFromUser(u, r.getRole()); } } if (roles != null) { for (String r : roles) { DefaultRole role = new DefaultRole(); role.setName(r); userManager.addRoleToUser(u, role); } } return new ActionResult("redirect", redirectUri); }
private void validateUser(DefaultUser user, String confirmPassword) { User u = userManager.getUserById(user.getId()); String username = u == null ? "" : u.getUsername(); String email = u == null ? "" : u.getEmail(); if (StringUtils.isBlank(user.getUsername())) { addFieldError("user.username", getText("message.admin.user.username.notempty")); } else { if (!StringUtils.equals(username, user.getUsername())) { if (userManager.getUserByUsername(user.getUsername()) != null) addFieldError("user.username", getText("message.admin.user.username.exist")); } } if (StringUtils.isBlank(user.getEmail())) { addFieldError("user.email", getText("message.admin.user.email.notempty")); } else if (!EmailValidator.getInstance().isValid(user.getEmail())) { addFieldError("user.email", getText("message.admin.user.username.notvalid")); } else { if (StringUtils.isNotBlank(user.getId())) { if (!StringUtils.equals(email, user.getEmail())) { if (userManager.getUserByEmail(user.getEmail()) != null) addFieldError("user.email", getText("message.admin.user.username.exist")); } } else { if (userManager.getUserByEmail(user.getEmail()) != null) addFieldError("user.email", getText("message.admin.user.username.exist")); } } if (StringUtils.isBlank(user.getPassword()) && StringUtils.isBlank(user.getId())) { addFieldError("user.password", getText("message.admin.user.password.notempty")); } if (!StringUtils.equals(user.getPassword(), confirmPassword)) { addFieldError("confirmPassword", getText("message.admin.user.password.notmatch")); } }