From d17bb5afad26d36f452f56c3ef96514aa3a9ef09 Mon Sep 17 00:00:00 2001 From: Sascha Doemer Date: Tue, 21 May 2024 21:06:34 +0200 Subject: [PATCH] Add tenant deletion functionality (#206) --- .../app/fivegla/business/TenantService.java | 8 +++++ .../controller/global/TenantController.java | 27 ++++++++++++++++ .../fivegla/persistence/TenantRepository.java | 32 +++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/src/main/java/de/app/fivegla/business/TenantService.java b/src/main/java/de/app/fivegla/business/TenantService.java index 74128cbd..1d1f8a23 100644 --- a/src/main/java/de/app/fivegla/business/TenantService.java +++ b/src/main/java/de/app/fivegla/business/TenantService.java @@ -122,6 +122,14 @@ public Optional findTenantByName(String name) { return tenantRepository.getTenant(name); } + /** + * Deletes a tenant by its tenantId. + * @param tenantId The tenantId of the tenant to delete. + */ + public void delete(String tenantId) { + tenantRepository.deleteTenant(tenantId); + } + /** * Represents a combination of Tenant object and an access token. */ diff --git a/src/main/java/de/app/fivegla/controller/global/TenantController.java b/src/main/java/de/app/fivegla/controller/global/TenantController.java index f364cc28..417e85fe 100644 --- a/src/main/java/de/app/fivegla/controller/global/TenantController.java +++ b/src/main/java/de/app/fivegla/controller/global/TenantController.java @@ -136,4 +136,31 @@ public ResponseEntity findAll() { return ResponseEntity.ok(ReadTenantsResponse.builder().tenants(tenants).build()); } + @Operation( + operationId = "tenant.delete", + description = "Delete a tenant based on the provided request.", + tags = BaseMappings.TENANT + ) + @ApiResponse( + responseCode = "204", + description = "The tenant was deleted successfully.", + content = @Content( + mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Response.class) + ) + ) + @ApiResponse( + responseCode = "400", + description = "The request is invalid.", + content = @Content( + mediaType = MediaType.APPLICATION_JSON_VALUE, + schema = @Schema(implementation = Response.class) + ) + ) + @DeleteMapping(value = "/{tenantId}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity delete(@PathVariable String tenantId) { + tenantService.delete(tenantId); + return ResponseEntity.noContent().build(); + } + } diff --git a/src/main/java/de/app/fivegla/persistence/TenantRepository.java b/src/main/java/de/app/fivegla/persistence/TenantRepository.java index f54e3b12..d36e7bcd 100644 --- a/src/main/java/de/app/fivegla/persistence/TenantRepository.java +++ b/src/main/java/de/app/fivegla/persistence/TenantRepository.java @@ -5,12 +5,14 @@ import de.app.fivegla.api.exceptions.BusinessException; import de.app.fivegla.persistence.entity.Tenant; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; import java.util.Optional; +@Slf4j @Component @RequiredArgsConstructor public class TenantRepository { @@ -85,4 +87,34 @@ public Optional getTenant(String tenantId) { public List findTenants() { return applicationData.getTenants(); } + + + /** + * Deletes a tenant with the specified tenant ID. + * + * @param tenantId The ID of the tenant to delete. + */ + public void deleteTenant(String tenantId) { + if (null != applicationData.getTenants()) { + log.info("Deleting tenant with tenantId: {}", tenantId); + applicationData.setTenants(applicationData.getTenants().stream() + .filter(t -> !t.getTenantId().equals(tenantId)) + .toList()); + applicationData.persist(); + } + if (null != applicationData.getGroups()) { + log.info("Deleting groups for tenant with tenantId: {}", tenantId); + applicationData.setGroups(applicationData.getGroups().stream() + .filter(g -> !g.getTenant().getTenantId().equals(tenantId)) + .toList()); + applicationData.persist(); + } + if (null != applicationData.getThirdPartyApiConfigurations()) { + log.info("Deleting third party API configurations for tenant with tenantId: {}", tenantId); + applicationData.setThirdPartyApiConfigurations(applicationData.getThirdPartyApiConfigurations().stream() + .filter(t -> !t.getTenantId().equals(tenantId)) + .toList()); + applicationData.persist(); + } + } }