Search in sources :

Example 1 with UserSettingsCommand

use of org.libresonic.player.command.UserSettingsCommand in project libresonic by Libresonic.

the class RESTController method updateUser.

@RequestMapping(value = "/rest/updateUser", method = { RequestMethod.GET, RequestMethod.POST })
public void updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request = wrapRequest(request);
    User user = securityService.getCurrentUser(request);
    if (!user.isAdminRole()) {
        error(request, response, ErrorCode.NOT_AUTHORIZED, user.getUsername() + " is not authorized to update users.");
        return;
    }
    String username = getRequiredStringParameter(request, "username");
    User u = securityService.getUserByName(username);
    UserSettings s = settingsService.getUserSettings(username);
    if (u == null) {
        error(request, response, ErrorCode.NOT_FOUND, "No such user: " + username);
        return;
    } else if (User.USERNAME_ADMIN.equals(username)) {
        error(request, response, ErrorCode.NOT_AUTHORIZED, "Not allowed to change admin user");
        return;
    }
    UserSettingsCommand command = new UserSettingsCommand();
    command.setUsername(username);
    command.setEmail(getStringParameter(request, "email", u.getEmail()));
    command.setLdapAuthenticated(getBooleanParameter(request, "ldapAuthenticated", u.isLdapAuthenticated()));
    command.setAdminRole(getBooleanParameter(request, "adminRole", u.isAdminRole()));
    command.setCommentRole(getBooleanParameter(request, "commentRole", u.isCommentRole()));
    command.setCoverArtRole(getBooleanParameter(request, "coverArtRole", u.isCoverArtRole()));
    command.setDownloadRole(getBooleanParameter(request, "downloadRole", u.isDownloadRole()));
    command.setStreamRole(getBooleanParameter(request, "streamRole", u.isDownloadRole()));
    command.setUploadRole(getBooleanParameter(request, "uploadRole", u.isUploadRole()));
    command.setJukeboxRole(getBooleanParameter(request, "jukeboxRole", u.isJukeboxRole()));
    command.setPodcastRole(getBooleanParameter(request, "podcastRole", u.isPodcastRole()));
    command.setSettingsRole(getBooleanParameter(request, "settingsRole", u.isSettingsRole()));
    command.setShareRole(getBooleanParameter(request, "shareRole", u.isShareRole()));
    int maxBitRate = getIntParameter(request, "maxBitRate", s.getTranscodeScheme().getMaxBitRate());
    command.setTranscodeSchemeName(TranscodeScheme.fromMaxBitRate(maxBitRate).name());
    if (hasParameter(request, "password")) {
        command.setPassword(decrypt(getRequiredStringParameter(request, "password")));
        command.setPasswordChange(true);
    }
    int[] folderIds = ServletRequestUtils.getIntParameters(request, "musicFolderId");
    if (folderIds.length == 0) {
        folderIds = Util.toIntArray(MusicFolder.toIdList(settingsService.getMusicFoldersForUser(username)));
    }
    command.setAllowedMusicFolderIds(folderIds);
    userSettingsController.updateUser(command);
    writeEmptyResponse(request, response);
}
Also used : User(org.libresonic.player.domain.User) UserSettingsCommand(org.libresonic.player.command.UserSettingsCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UserSettingsCommand

use of org.libresonic.player.command.UserSettingsCommand in project libresonic by Libresonic.

the class RESTController method createUser.

@RequestMapping(value = "/rest/createUser", method = { RequestMethod.GET, RequestMethod.POST })
public void createUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request = wrapRequest(request);
    User user = securityService.getCurrentUser(request);
    if (!user.isAdminRole()) {
        error(request, response, ErrorCode.NOT_AUTHORIZED, user.getUsername() + " is not authorized to create new users.");
        return;
    }
    UserSettingsCommand command = new UserSettingsCommand();
    command.setUsername(getRequiredStringParameter(request, "username"));
    command.setPassword(decrypt(getRequiredStringParameter(request, "password")));
    command.setEmail(getRequiredStringParameter(request, "email"));
    command.setLdapAuthenticated(getBooleanParameter(request, "ldapAuthenticated", false));
    command.setAdminRole(getBooleanParameter(request, "adminRole", false));
    command.setCommentRole(getBooleanParameter(request, "commentRole", false));
    command.setCoverArtRole(getBooleanParameter(request, "coverArtRole", false));
    command.setDownloadRole(getBooleanParameter(request, "downloadRole", false));
    command.setStreamRole(getBooleanParameter(request, "streamRole", true));
    command.setUploadRole(getBooleanParameter(request, "uploadRole", false));
    command.setJukeboxRole(getBooleanParameter(request, "jukeboxRole", false));
    command.setPodcastRole(getBooleanParameter(request, "podcastRole", false));
    command.setSettingsRole(getBooleanParameter(request, "settingsRole", true));
    command.setShareRole(getBooleanParameter(request, "shareRole", false));
    command.setTranscodeSchemeName(TranscodeScheme.OFF.name());
    int[] folderIds = ServletRequestUtils.getIntParameters(request, "musicFolderId");
    if (folderIds.length == 0) {
        folderIds = Util.toIntArray(MusicFolder.toIdList(settingsService.getAllMusicFolders()));
    }
    command.setAllowedMusicFolderIds(folderIds);
    userSettingsController.createUser(command);
    writeEmptyResponse(request, response);
}
Also used : User(org.libresonic.player.domain.User) UserSettingsCommand(org.libresonic.player.command.UserSettingsCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserSettingsCommand

use of org.libresonic.player.command.UserSettingsCommand in project libresonic by Libresonic.

the class UserSettingsController method displayForm.

@RequestMapping(method = RequestMethod.GET)
protected String displayForm(HttpServletRequest request, Model model) throws Exception {
    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.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);
    return "userSettings";
}
Also used : User(org.libresonic.player.domain.User) UserSettings(org.libresonic.player.domain.UserSettings) UserSettingsCommand(org.libresonic.player.command.UserSettingsCommand) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with UserSettingsCommand

use of org.libresonic.player.command.UserSettingsCommand in project libresonic by Libresonic.

the class UserSettingsValidator method validate.

/**
     * {@inheritDoc}
     */
public void validate(Object obj, Errors errors) {
    UserSettingsCommand command = (UserSettingsCommand) obj;
    String username = command.getUsername();
    String email = StringUtils.trimToNull(command.getEmail());
    String password = StringUtils.trimToNull(command.getPassword());
    String confirmPassword = command.getConfirmPassword();
    if (command.isNewUser()) {
        if (username == null || username.length() == 0) {
            errors.rejectValue("username", "usersettings.nousername");
        } else if (securityService.getUserByName(username) != null) {
            errors.rejectValue("username", "usersettings.useralreadyexists");
        } else if (email == null) {
            errors.rejectValue("email", "usersettings.noemail");
        } else if (command.isLdapAuthenticated() && !settingsService.isLdapEnabled()) {
            errors.rejectValue("password", "usersettings.ldapdisabled");
        } else if (command.isLdapAuthenticated() && password != null) {
            errors.rejectValue("password", "usersettings.passwordnotsupportedforldap");
        }
    }
    if ((command.isNewUser() || command.isPasswordChange()) && !command.isLdapAuthenticated()) {
        if (password == null) {
            errors.rejectValue("password", "usersettings.nopassword");
        } else if (!password.equals(confirmPassword)) {
            errors.rejectValue("password", "usersettings.wrongpassword");
        }
    }
    if (command.isPasswordChange() && command.isLdapAuthenticated()) {
        errors.rejectValue("password", "usersettings.passwordnotsupportedforldap");
    }
}
Also used : UserSettingsCommand(org.libresonic.player.command.UserSettingsCommand)

Aggregations

UserSettingsCommand (org.libresonic.player.command.UserSettingsCommand)4 User (org.libresonic.player.domain.User)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 UserSettings (org.libresonic.player.domain.UserSettings)1