Search in sources :

Example 1 with YamlSequenceBuilder

use of com.amihaiemil.eoyaml.YamlSequenceBuilder in project front by artipie.

the class YamlRepoPermissions method add.

@Override
public void add(final String repo, final String uid, final JsonArray perms) {
    final Optional<YamlMapping> creds = this.permsYaml(repo);
    YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
    if (creds.isPresent()) {
        for (final YamlNode node : creds.get().keys()) {
            final String usr = node.asScalar().value();
            if (!uid.equals(usr)) {
                builder = builder.add(usr, creds.get().yamlSequence(usr));
            }
        }
    }
    YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
    for (final String item : perms.getValuesAs(JsonValue::toString)) {
        seq = seq.add(item);
    }
    builder = builder.add(uid, seq.build());
    this.updateRepoSettings(repo, builder.build());
}
Also used : YamlNode(com.amihaiemil.eoyaml.YamlNode) JsonValue(javax.json.JsonValue) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder)

Example 2 with YamlSequenceBuilder

use of com.amihaiemil.eoyaml.YamlSequenceBuilder in project artipie by artipie.

the class CredsConfigYaml method withFullInfo.

/**
 * Adds user with full info: name, password, email and groups.
 * @param username Name
 * @param format Password format
 * @param pswd Password
 * @param email Email
 * @param groups Groups
 * @return Itself
 * @checkstyle ParameterNumberCheck (500 lines)
 */
public CredsConfigYaml withFullInfo(final String username, final Users.PasswordFormat format, final String pswd, final String email, final Set<String> groups) {
    String pass = pswd;
    if (format == Users.PasswordFormat.SHA256) {
        pass = DigestUtils.sha256Hex(pass);
    }
    YamlMappingBuilder user = Yaml.createYamlMappingBuilder().add("type", format.name().toLowerCase(Locale.US)).add("pass", pass).add("email", email);
    if (!groups.isEmpty()) {
        YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
        for (final String group : groups) {
            seq = seq.add(group);
        }
        user = user.add("groups", seq.build());
    }
    this.builder = this.builder.add(username, user.build());
    return this;
}
Also used : YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder)

Example 3 with YamlSequenceBuilder

use of com.amihaiemil.eoyaml.YamlSequenceBuilder in project artipie by artipie.

the class CredsConfigYaml method withUserAndGroups.

/**
 * Adds user with groups to credentials config.
 * @param username Name of the user
 * @param groups Groups list
 * @return Itself
 */
public CredsConfigYaml withUserAndGroups(final String username, final List<String> groups) {
    YamlMappingBuilder user = Yaml.createYamlMappingBuilder().add("pass", "123").add("type", "plain");
    if (!groups.isEmpty()) {
        YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
        for (final String group : groups) {
            seq = seq.add(group);
        }
        user = user.add("groups", seq.build());
    }
    this.builder = this.builder.add(username, user.build());
    return this;
}
Also used : YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder)

Example 4 with YamlSequenceBuilder

use of com.amihaiemil.eoyaml.YamlSequenceBuilder in project artipie by artipie.

the class UsersFromStorageYaml method add.

@Override
public CompletionStage<Void> add(final User user, final String pswd, final PasswordFormat format) {
    return this.yaml().thenCompose(yaml -> {
        YamlMappingBuilder result = UsersFromStorageYaml.removeUserRecord(user.name(), yaml);
        YamlMappingBuilder info = Yaml.createYamlMappingBuilder().add("type", format.name().toLowerCase(Locale.US)).add("pass", pswd);
        if (user.email().isPresent()) {
            info = info.add(UsersFromStorageYaml.EMAIL, user.email().get());
        }
        if (!user.groups().isEmpty()) {
            YamlSequenceBuilder seq = Yaml.createYamlSequenceBuilder();
            for (final String group : user.groups()) {
                seq = seq.add(group);
            }
            info = info.add(UsersFromStorageYaml.GROUPS, seq.build());
        }
        result = result.add(user.name(), info.build());
        return this.buildAndSaveCredentials(result);
    });
}
Also used : YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder)

Aggregations

YamlMappingBuilder (com.amihaiemil.eoyaml.YamlMappingBuilder)4 YamlSequenceBuilder (com.amihaiemil.eoyaml.YamlSequenceBuilder)4 YamlMapping (com.amihaiemil.eoyaml.YamlMapping)1 YamlNode (com.amihaiemil.eoyaml.YamlNode)1 JsonValue (javax.json.JsonValue)1