Fixes #180 Display validation errors on user settings tab

Signed-off-by: Andrew DeMaria <lostonamountain@gmail.com>
master
Andrew DeMaria 8 years ago
parent 8d28f2ab87
commit f344fbb989
No known key found for this signature in database
GPG Key ID: 0A3F5E91F8364EDF
  1. 72
      libresonic-main/src/main/java/org/libresonic/player/controller/UserSettingsController.java

@ -33,6 +33,7 @@ import org.libresonic.player.validator.UserSettingsValidator;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.bind.ServletRequestUtils;
@ -71,29 +72,34 @@ public class UserSettingsController {
@RequestMapping(method = RequestMethod.GET) @RequestMapping(method = RequestMethod.GET)
protected String displayForm(HttpServletRequest request, Model model) throws Exception { protected String displayForm(HttpServletRequest request, Model model) throws Exception {
UserSettingsCommand command = new UserSettingsCommand(); UserSettingsCommand command;
if(!model.containsAttribute("command")) {
command = new UserSettingsCommand();
User user = getUser(request);
if (user != null) {
command.setUser(user);
command.setEmail(user.getEmail());
UserSettings userSettings = settingsService.getUserSettings(user.getUsername());
command.setTranscodeSchemeName(userSettings.getTranscodeScheme().name());
command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user)));
} else {
command.setNewUser(true);
command.setStreamRole(true);
command.setSettingsRole(true);
}
} else {
command = (UserSettingsCommand) model.asMap().get("command");
}
command.setAdmin(User.USERNAME_ADMIN.equals(command.getUsername()));
command.setUsers(securityService.getAllUsers()); command.setUsers(securityService.getAllUsers());
command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null)); command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath()); command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
command.setTranscodeSchemes(TranscodeScheme.values()); command.setTranscodeSchemes(TranscodeScheme.values());
command.setLdapEnabled(settingsService.isLdapEnabled()); command.setLdapEnabled(settingsService.isLdapEnabled());
command.setAllMusicFolders(settingsService.getAllMusicFolders()); command.setAllMusicFolders(settingsService.getAllMusicFolders());
User user = getUser(request); model.addAttribute("command", command);
if (user != null) {
command.setUser(user);
command.setEmail(user.getEmail());
command.setAdmin(User.USERNAME_ADMIN.equals(user.getUsername()));
UserSettings userSettings = settingsService.getUserSettings(user.getUsername());
command.setTranscodeSchemeName(userSettings.getTranscodeScheme().name());
command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user)));
} else {
command.setNewUser(true);
command.setStreamRole(true);
command.setSettingsRole(true);
}
model.addAttribute("command",command);
return "userSettings"; return "userSettings";
} }
@ -121,21 +127,37 @@ public class UserSettingsController {
} }
@RequestMapping(method = RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
protected String doSubmitAction(@ModelAttribute("command") @Validated UserSettingsCommand command, RedirectAttributes redirectAttributes) throws Exception { protected String doSubmitAction(@ModelAttribute("command") @Validated UserSettingsCommand command, BindingResult bindingResult, RedirectAttributes redirectAttributes) throws Exception {
if (command.isDeleteUser()) { if(!bindingResult.hasErrors()) {
deleteUser(command); if (command.isDeleteUser()) {
} else if (command.isNewUser()) { deleteUser(command);
createUser(command); } else if (command.isNewUser()) {
createUser(command);
} else {
updateUser(command);
}
redirectAttributes.addFlashAttribute("settings_reload", true);
redirectAttributes.addFlashAttribute("settings_toast", true);
} else { } else {
updateUser(command); redirectAttributes.addFlashAttribute("command", command);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.command", bindingResult);
redirectAttributes.addFlashAttribute("userIndex", getUserIndex(command));
} }
redirectAttributes.addFlashAttribute("settings_reload", true);
redirectAttributes.addFlashAttribute("settings_toast", true);
return "redirect:userSettings.view"; return "redirect:userSettings.view";
} }
private Integer getUserIndex(UserSettingsCommand command) {
List<User> allUsers = securityService.getAllUsers();
for (int i = 0; i < allUsers.size(); i++) {
if(StringUtils.equalsIgnoreCase(allUsers.get(i).getUsername(), command.getUsername())) {
return i;
}
}
return null;
}
private void deleteUser(UserSettingsCommand command) { private void deleteUser(UserSettingsCommand command) {
securityService.deleteUser(command.getUsername()); securityService.deleteUser(command.getUsername());
} }

Loading…
Cancel
Save