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. 40
      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,19 +72,14 @@ 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();
command.setUsers(securityService.getAllUsers());
command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
command.setTranscodeSchemes(TranscodeScheme.values());
command.setLdapEnabled(settingsService.isLdapEnabled());
command.setAllMusicFolders(settingsService.getAllMusicFolders());
User user = getUser(request); User user = getUser(request);
if (user != null) { if (user != null) {
command.setUser(user); command.setUser(user);
command.setEmail(user.getEmail()); command.setEmail(user.getEmail());
command.setAdmin(User.USERNAME_ADMIN.equals(user.getUsername()));
UserSettings userSettings = settingsService.getUserSettings(user.getUsername()); UserSettings userSettings = settingsService.getUserSettings(user.getUsername());
command.setTranscodeSchemeName(userSettings.getTranscodeScheme().name()); command.setTranscodeSchemeName(userSettings.getTranscodeScheme().name());
command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user))); command.setAllowedMusicFolderIds(Util.toIntArray(getAllowedMusicFolderIds(user)));
@ -93,6 +89,16 @@ public class UserSettingsController {
command.setSettingsRole(true); command.setSettingsRole(true);
} }
} else {
command = (UserSettingsCommand) model.asMap().get("command");
}
command.setAdmin(User.USERNAME_ADMIN.equals(command.getUsername()));
command.setUsers(securityService.getAllUsers());
command.setTranscodingSupported(transcodingService.isDownsamplingSupported(null));
command.setTranscodeDirectory(transcodingService.getTranscodeDirectory().getPath());
command.setTranscodeSchemes(TranscodeScheme.values());
command.setLdapEnabled(settingsService.isLdapEnabled());
command.setAllMusicFolders(settingsService.getAllMusicFolders());
model.addAttribute("command", command); model.addAttribute("command", command);
return "userSettings"; return "userSettings";
} }
@ -121,8 +127,9 @@ 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(!bindingResult.hasErrors()) {
if (command.isDeleteUser()) { if (command.isDeleteUser()) {
deleteUser(command); deleteUser(command);
} else if (command.isNewUser()) { } else if (command.isNewUser()) {
@ -132,10 +139,25 @@ public class UserSettingsController {
} }
redirectAttributes.addFlashAttribute("settings_reload", true); redirectAttributes.addFlashAttribute("settings_reload", true);
redirectAttributes.addFlashAttribute("settings_toast", true); redirectAttributes.addFlashAttribute("settings_toast", true);
} else {
redirectAttributes.addFlashAttribute("command", command);
redirectAttributes.addFlashAttribute("org.springframework.validation.BindingResult.command", bindingResult);
redirectAttributes.addFlashAttribute("userIndex", getUserIndex(command));
}
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