Search in sources :

Example 6 with YamlMappingBuilder

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

the class UsersFromStorageYaml method removeUserRecord.

/**
 * Removes user record from credentials.yaml.
 * @param username User name to remove
 * @param yaml Credentials mapping
 * @return YamlMappingBuilder without removed user
 */
private static YamlMappingBuilder removeUserRecord(final String username, final YamlMapping yaml) {
    YamlMappingBuilder result = Yaml.createYamlMappingBuilder();
    final YamlMapping credentials = yaml.yamlMapping(UsersFromStorageYaml.CREDENTIALS);
    final List<YamlNode> keep = credentials.keys().stream().filter(node -> !node.asScalar().value().equals(username)).collect(Collectors.toList());
    for (final YamlNode node : keep) {
        result = result.add(node, credentials.value(node));
    }
    return result;
}
Also used : YamlMapping(com.amihaiemil.eoyaml.YamlMapping) Users(com.artipie.auth.Users) AuthFromYaml(com.artipie.auth.AuthFromYaml) Content(com.artipie.asto.Content) Key(com.artipie.asto.Key) Yaml(com.amihaiemil.eoyaml.Yaml) YamlNode(com.amihaiemil.eoyaml.YamlNode) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) YamlSequenceBuilder(com.amihaiemil.eoyaml.YamlSequenceBuilder) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) List(java.util.List) CompletionStage(java.util.concurrent.CompletionStage) Storage(com.artipie.asto.Storage) Locale(java.util.Locale) Optional(java.util.Optional) Authentication(com.artipie.http.auth.Authentication) Collections(java.util.Collections) CredsConfigCache(com.artipie.cache.CredsConfigCache) YamlNode(com.amihaiemil.eoyaml.YamlNode) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlMapping(com.amihaiemil.eoyaml.YamlMapping)

Example 7 with YamlMappingBuilder

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

the class YamlUsers method add.

@Override
public void add(final JsonObject info, final String uid) {
    YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
    final Optional<YamlMapping> users = this.users();
    if (users.isPresent()) {
        for (final YamlNode node : users.get().keys()) {
            final String val = node.asScalar().value();
            builder = builder.add(val, users.get().yamlMapping(val));
        }
    }
    builder = builder.add(uid, new Json2Yaml().apply(info.toString()));
    this.blsto.save(this.key, Yaml.createYamlMappingBuilder().add(ArtipieYaml.NODE_CREDENTIALS, builder.build()).build().toString().getBytes(StandardCharsets.UTF_8));
}
Also used : Json2Yaml(com.artipie.front.misc.Json2Yaml) YamlNode(com.amihaiemil.eoyaml.YamlNode) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlMapping(com.amihaiemil.eoyaml.YamlMapping)

Example 8 with YamlMappingBuilder

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

the class YamlUsers method remove.

@Override
public void remove(final String uid) {
    if (this.users().map(yaml -> yaml.yamlMapping(uid) != null).orElse(false)) {
        YamlMappingBuilder builder = Yaml.createYamlMappingBuilder();
        final YamlMapping users = this.users().get();
        for (final YamlNode node : users.keys()) {
            final String val = node.asScalar().value();
            if (!uid.equals(val)) {
                builder = builder.add(val, users.yamlMapping(val));
            }
        }
        this.blsto.save(this.key, Yaml.createYamlMappingBuilder().add(ArtipieYaml.NODE_CREDENTIALS, builder.build()).build().toString().getBytes(StandardCharsets.UTF_8));
        return;
    }
    throw new NotFoundException(String.format("User %s does not exist", uid));
}
Also used : JsonObject(javax.json.JsonObject) YamlMapping(com.amihaiemil.eoyaml.YamlMapping) Collection(java.util.Collection) IOException(java.io.IOException) Key(com.artipie.asto.Key) BlockingStorage(com.artipie.asto.blocking.BlockingStorage) Yaml(com.amihaiemil.eoyaml.Yaml) YamlNode(com.amihaiemil.eoyaml.YamlNode) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ArtipieYaml(com.artipie.front.settings.ArtipieYaml) UncheckedIOException(java.io.UncheckedIOException) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) Optional(java.util.Optional) NotFoundException(com.artipie.front.api.NotFoundException) Json2Yaml(com.artipie.front.misc.Json2Yaml) Collections(java.util.Collections) YamlNode(com.amihaiemil.eoyaml.YamlNode) NotFoundException(com.artipie.front.api.NotFoundException) YamlMappingBuilder(com.amihaiemil.eoyaml.YamlMappingBuilder) YamlMapping(com.amihaiemil.eoyaml.YamlMapping)

Example 9 with YamlMappingBuilder

use of com.amihaiemil.eoyaml.YamlMappingBuilder 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 10 with YamlMappingBuilder

use of com.amihaiemil.eoyaml.YamlMappingBuilder 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)

Aggregations

YamlMappingBuilder (com.amihaiemil.eoyaml.YamlMappingBuilder)12 YamlMapping (com.amihaiemil.eoyaml.YamlMapping)8 YamlNode (com.amihaiemil.eoyaml.YamlNode)8 YamlSequenceBuilder (com.amihaiemil.eoyaml.YamlSequenceBuilder)6 Yaml (com.amihaiemil.eoyaml.Yaml)4 Key (com.artipie.asto.Key)4 StandardCharsets (java.nio.charset.StandardCharsets)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 NotFoundException (com.artipie.front.api.NotFoundException)3 Json2Yaml (com.artipie.front.misc.Json2Yaml)3 IOException (java.io.IOException)3 UncheckedIOException (java.io.UncheckedIOException)3 Collection (java.util.Collection)3 Content (com.artipie.asto.Content)2 Storage (com.artipie.asto.Storage)2 BlockingStorage (com.artipie.asto.blocking.BlockingStorage)2 List (java.util.List)2 CompletionStage (java.util.concurrent.CompletionStage)2