<commit_before>import org.sonatype.nexus.security.role.NoSuchRoleException;
import org.sonatype.nexus.security.role.RoleIdentifier;
import org.sonatype.nexus.security.user.User;
import org.sonatype.nexus.security.user.UserManager;
import org.sonatype.nexus.security.user.UserNotFoundException;
import org.sonatype.nexus.security.SecuritySystem;

import groovy.json.JsonSlurper;

def params = new JsonSlurper().parseText(args)

authManager = security.getSecuritySystem().getAuthorizationManager(UserManager.DEFAULT_SOURCE);
// Create a list of available from the list of passed roles. May end up empty if no role exists.
Set<RoleIdentifier> roleList = new HashSet<RoleIdentifier>();
params.roles.each { role ->
    try {
        def newRole = authManager.getRole(role);
        roleList.add(newRole);
    } catch (NoSuchRoleException e) {
        log.warn("No such role: ${role}, trying to set for ${params.username} from Chef");
    }
}

try {
    // Update
    User user = security.securitySystem.getUser(params.username, UserManager.DEFAULT_SOURCE);

    user.setFirstName(params.first_name);
    user.setLastName(params.last_name);
    user.setEmailAddress(params.email);
    user.setRoles(roleList);

    security.securitySystem.updateUser(user);
    security.securitySystem.changePassword(params.username, params.password);
    log.info("Updated information for ${params.username} from Chef");
} catch (UserNotFoundException e) {
    // Create
    security.addUser(params.username, params.first_name, params.last_name, params.email, true, params.password, params.roles);
    log.info("Created user ${params.username} from Chef");
}<commit_msg>Use role strings passed to add roles directly to user<commit_after>import org.sonatype.nexus.security.role.NoSuchRoleException;
import org.sonatype.nexus.security.user.User;
import org.sonatype.nexus.security.user.UserManager;
import org.sonatype.nexus.security.user.UserNotFoundException;
import org.sonatype.nexus.security.SecuritySystem;

import groovy.json.JsonSlurper;

def params = new JsonSlurper().parseText(args)

try {
    // Update
    User user = security.securitySystem.getUser(params.username, UserManager.DEFAULT_SOURCE);

    user.setFirstName(params.first_name);
    user.setLastName(params.last_name);
    user.setEmailAddress(params.email);

    security.securitySystem.updateUser(user);
    security.securitySystem.changePassword(params.username, params.password);

    security.setUserRoles(user.getUserId(), params.roles);

    log.info("Updated information for ${params.username} from Chef");
} catch (UserNotFoundException e) {
    // Create
    security.addUser(params.username, params.first_name, params.last_name, params.email, true, params.password, params.roles);
    log.info("Created user ${params.username} from Chef");
}
{'fraction_non_alphanumeric': 0.11567297393787933, 'fraction_numerical': 0.0, 'mean_word_length': 4.961702127659574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7198822', 'n_tokens_mistral': 831, 'n_tokens_neox': 818, 'n_words': 160}