All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.service.RoleServices Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
 * one or more contributor license agreements. See the NOTICE file distributed
 * with this work for additional information regarding copyright ownership.
 * Licensed under the Camunda License 1.0. You may not use this file
 * except in compliance with the Camunda License 1.0.
 */
package io.camunda.service;

import io.camunda.search.clients.RoleSearchClient;
import io.camunda.search.entities.RoleEntity;
import io.camunda.search.exception.NotFoundException;
import io.camunda.search.query.RoleQuery;
import io.camunda.search.query.SearchQueryBuilders;
import io.camunda.search.query.SearchQueryResult;
import io.camunda.security.auth.Authentication;
import io.camunda.security.auth.SecurityContext;
import io.camunda.security.configuration.SecurityConfiguration;
import io.camunda.service.search.core.SearchQueryService;
import io.camunda.zeebe.broker.client.api.BrokerClient;
import io.camunda.zeebe.gateway.impl.broker.request.role.BrokerRoleCreateRequest;
import io.camunda.zeebe.protocol.impl.record.value.authorization.RoleRecord;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

public class RoleServices extends SearchQueryService {

  private final RoleSearchClient roleSearchClient;

  public RoleServices(
      final BrokerClient brokerClient,
      final SecurityConfiguration securityConfiguration,
      final RoleSearchClient roleSearchClient,
      final Authentication authentication) {
    super(brokerClient, securityConfiguration, authentication);
    this.roleSearchClient = roleSearchClient;
  }

  @Override
  public SearchQueryResult search(final RoleQuery query) {
    return roleSearchClient.searchRoles(
        query, SecurityContext.of(s -> s.withAuthentication(authentication)));
  }

  @Override
  public RoleServices withAuthentication(final Authentication authentication) {
    return new RoleServices(brokerClient, securityConfiguration, roleSearchClient, authentication);
  }

  public CompletableFuture createRole(final RoleDTO request) {
    return sendBrokerRequest(new BrokerRoleCreateRequest().setName(request.name()));
  }

  public RoleEntity getByRoleKey(final Long roleKey) {
    final SearchQueryResult result =
        search(SearchQueryBuilders.roleSearchQuery().filter(f -> f.roleKey(roleKey)).build());
    if (result.total() < 1) {
      throw new NotFoundException(String.format("Role with roleKey %d not found", roleKey));
    } else {
      return result.items().stream().findFirst().orElseThrow();
    }
  }

  public record RoleDTO(long roleKey, String name, Set assignedMemberKeys) {}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy