From d14d812353a90cce3e3a5a79dc19014989f97ae0 Mon Sep 17 00:00:00 2001 From: Dennis Wendland Date: Mon, 2 Oct 2023 10:33:25 +0200 Subject: [PATCH] Adapt repo and mapper --- .gitignore | 1 + .../java/org/fiware/iam/ServiceMapper.java | 56 ++++++++++++++++--- .../org/fiware/iam/repository/Credential.java | 10 ++-- .../org/fiware/iam/repository/Service.java | 8 +-- .../iam/repository/ServiceRepository.java | 11 ++-- .../fiware/iam/repository/ServiceScope.java | 33 +++++++++++ .../fiware/iam/repository/ServiceScopes.java | 19 ------- .../iam/repository/ServiceScopesEntry.java | 21 ------- .../migration/V0_0_1__initial-migration.sql | 16 ++++-- 9 files changed, 108 insertions(+), 67 deletions(-) create mode 100644 .gitignore create mode 100644 src/main/java/org/fiware/iam/repository/ServiceScope.java delete mode 100644 src/main/java/org/fiware/iam/repository/ServiceScopes.java delete mode 100644 src/main/java/org/fiware/iam/repository/ServiceScopesEntry.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90f0431 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/** diff --git a/src/main/java/org/fiware/iam/ServiceMapper.java b/src/main/java/org/fiware/iam/ServiceMapper.java index e89687e..e0c08f1 100644 --- a/src/main/java/org/fiware/iam/ServiceMapper.java +++ b/src/main/java/org/fiware/iam/ServiceMapper.java @@ -8,7 +8,9 @@ import org.mapstruct.Mapper; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.Optional; /** * Responsible for mapping entities from the Service api domain to the internal model. @@ -16,17 +18,48 @@ @Mapper(componentModel = "jsr330") public interface ServiceMapper { - Service map(ServiceVO serviceVO); + default Service map(ServiceVO serviceVO) { + return new Service() + .setDefaultOidcScope(serviceVO.getDefaultOidcScope()) + .setId(serviceVO.getId()) + .setOidcScopes(map(serviceVO.getOidcScopes(), serviceVO.getId())); + } ServiceVO map(Service service); - ServiceScopesEntry map(ServiceScopesEntryVO serviceScopesEntryVO); + default ServiceScope map(ServiceScopesEntryVO serviceScopesEntryVO, String scopeName, String serviceName) { + return new ServiceScope() + .setId("%s_%s".formatted(scopeName, serviceName)) + .setScopeName(scopeName) + .setCredentials(serviceScopesEntryVO.stream().map(this::map).toList()); + } - ServiceScopesEntryVO map(ServiceScopesEntry serviceScopesEntry); + ServiceScopesEntryVO map(ServiceScope serviceScope); - ServiceScopes map(ServiceScopesVO serviceScopesVO); + default Collection map(ServiceScopesVO value, String serviceName) { + if (value.getAdditionalProperties() == null) { + return List.of(); + } + return value + .getAdditionalProperties() + .entrySet() + .stream() + .map(e -> map(e.getValue(), e.getKey(), serviceName)) + .toList(); + } - ServiceScopesVO map(ServiceScopes serviceScopes); + default ServiceScopesVO mapEntries(Collection value) { + ServiceScopesVO mappedScopes = new ServiceScopesVO(); + if (value != null) { + value.forEach(e -> { + ServiceScopesEntryVO scopes = new ServiceScopesEntryVO(); + scopes.addAll(map(e.getCredentials())); + mappedScopes.setAdditionalProperties(e.getScopeName(), scopes); + } + ); + } + return mappedScopes; + } default Credential map(CredentialVO credentialVO) { if (credentialVO == null) { @@ -35,12 +68,19 @@ default Credential map(CredentialVO credentialVO) { Credential credential = new Credential() .setCredentialType(credentialVO.getType()); List trustedList = new ArrayList<>(); - trustedList.addAll(issuersToEntries(credentialVO.getTrustedIssuersLists())); - trustedList.addAll(participantsToEntries(credentialVO.getTrustedParticipantsLists())); + Optional.ofNullable(issuersToEntries(credentialVO.getTrustedIssuersLists())).ifPresent(trustedList::addAll); + Optional.ofNullable(participantsToEntries(credentialVO.getTrustedParticipantsLists())).ifPresent(trustedList::addAll); credential.setTrustedLists(trustedList); return credential; } + default Collection map(Collection credentials) { + if (credentials == null) { + return null; + } + return credentials.stream().map(this::map).toList(); + } + default CredentialVO map(Credential credential) { if (credential == null) { return null; @@ -107,4 +147,4 @@ default List entriesToParticipants(List endpoints) { .toList(); } -} +} \ No newline at end of file diff --git a/src/main/java/org/fiware/iam/repository/Credential.java b/src/main/java/org/fiware/iam/repository/Credential.java index f69ec6e..8a5eeae 100644 --- a/src/main/java/org/fiware/iam/repository/Credential.java +++ b/src/main/java/org/fiware/iam/repository/Credential.java @@ -23,8 +23,8 @@ @Data @Accessors(chain = true) @Entity -@EqualsAndHashCode(exclude = "service") -@ToString(exclude = "service") +@EqualsAndHashCode(exclude = "serviceScope") +@ToString(exclude = "serviceScope") public class Credential { @GeneratedValue @@ -37,10 +37,10 @@ public class Credential { private List trustedLists; @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "service_id") - private Service service; + @JoinColumn(name = "service_scope_id") + private ServiceScope serviceScope; -} +} \ No newline at end of file diff --git a/src/main/java/org/fiware/iam/repository/Service.java b/src/main/java/org/fiware/iam/repository/Service.java index 07c4abf..4e8309c 100644 --- a/src/main/java/org/fiware/iam/repository/Service.java +++ b/src/main/java/org/fiware/iam/repository/Service.java @@ -27,8 +27,6 @@ public class Service { private String defaultOidcScope; - private ServiceScopes oidcScopes; - - //@OneToMany(mappedBy = "service", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) - //private Collection credentials; -} + @OneToMany(mappedBy = "service", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) + private Collection oidcScopes; +} \ No newline at end of file diff --git a/src/main/java/org/fiware/iam/repository/ServiceRepository.java b/src/main/java/org/fiware/iam/repository/ServiceRepository.java index 507c13e..a7566f3 100644 --- a/src/main/java/org/fiware/iam/repository/ServiceRepository.java +++ b/src/main/java/org/fiware/iam/repository/ServiceRepository.java @@ -19,8 +19,9 @@ public interface ServiceRepository extends PageableRepository { * @param id of the service * @return the complete service */ - @Join(value = "credentials", type = Join.Type.LEFT_FETCH) - @Join(value = "credentials.trustedLists", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH) Service getById(String id); /** @@ -30,8 +31,8 @@ public interface ServiceRepository extends PageableRepository { * @return the current page */ @NonNull - @Join(value = "credentials", type = Join.Type.LEFT_FETCH) - @Join(value = "credentials.trustedLists", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes.credentials", type = Join.Type.LEFT_FETCH) + @Join(value = "oidcScopes.credentials.trustedLists", type = Join.Type.LEFT_FETCH) Page findAll(@NonNull Pageable pageable); } - diff --git a/src/main/java/org/fiware/iam/repository/ServiceScope.java b/src/main/java/org/fiware/iam/repository/ServiceScope.java new file mode 100644 index 0000000..77222fc --- /dev/null +++ b/src/main/java/org/fiware/iam/repository/ServiceScope.java @@ -0,0 +1,33 @@ +package org.fiware.iam.repository; + +import io.micronaut.core.annotation.Introspected; +import jakarta.persistence.*; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import lombok.experimental.Accessors; + +import java.util.Collection; +import java.util.UUID; + +@Introspected +@Accessors(chain = true) +@Data +@Entity +@ToString(exclude = "service") +@EqualsAndHashCode(exclude = "service") +public class ServiceScope { + + @Id + private String id; + + private String scopeName; + + @OneToMany(mappedBy = "serviceScope", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) + private Collection credentials; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "service_id") + private Service service; + +} \ No newline at end of file diff --git a/src/main/java/org/fiware/iam/repository/ServiceScopes.java b/src/main/java/org/fiware/iam/repository/ServiceScopes.java deleted file mode 100644 index b4ec1fa..0000000 --- a/src/main/java/org/fiware/iam/repository/ServiceScopes.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.fiware.iam.repository; - -import io.micronaut.core.annotation.Introspected; -import jakarta.persistence.CascadeType; -import jakarta.persistence.Entity; -import jakarta.persistence.FetchType; -import jakarta.persistence.OneToMany; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -import java.util.Map; - -public class ServiceScopes { - - @OneToMany(mappedBy = "serviceScopes", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true) - private Map additionalProperties; - -} diff --git a/src/main/java/org/fiware/iam/repository/ServiceScopesEntry.java b/src/main/java/org/fiware/iam/repository/ServiceScopesEntry.java deleted file mode 100644 index e85796d..0000000 --- a/src/main/java/org/fiware/iam/repository/ServiceScopesEntry.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.fiware.iam.repository; - -import io.micronaut.core.annotation.Introspected; -import jakarta.persistence.Entity; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.experimental.Accessors; - -import java.util.ArrayList; -import java.util.Collection; - -@Introspected -@Accessors(chain = true) -@Data -@Entity -@EqualsAndHashCode -public class ServiceScopesEntry extends ArrayList { - - - -} diff --git a/src/main/resources/db/migration/V0_0_1__initial-migration.sql b/src/main/resources/db/migration/V0_0_1__initial-migration.sql index 8e102d2..d119544 100644 --- a/src/main/resources/db/migration/V0_0_1__initial-migration.sql +++ b/src/main/resources/db/migration/V0_0_1__initial-migration.sql @@ -1,12 +1,20 @@ CREATE TABLE IF NOT EXISTS `service` ( - `id` varchar(255) NOT NULL PRIMARY KEY + `id` varchar(255) NOT NULL PRIMARY KEY, + `default_oidc_scope` varchar(255) NOT NULL +); + +CREATE TABLE IF NOT EXISTS `service_scope` ( + `id` varchar(255) NOT NULL PRIMARY KEY, + `scope_name` varchar(255) NOT NULL, + `service_id` varchar(255) NOT NULL, + CONSTRAINT `fk_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS `credential` ( `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY, `credential_type` varchar(255) NOT NULL, - `service_id` varchar(255) NOT NULL, - CONSTRAINT `fk_service` FOREIGN KEY (`service_id`) REFERENCES `service` (`id`) ON DELETE CASCADE + `service_scope_id` varchar(255) NOT NULL, + CONSTRAINT `fk_scope` FOREIGN KEY (`service_scope_id`) REFERENCES `service_scope` (`id`) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS `endpoint_entry` ( @@ -15,4 +23,4 @@ CREATE TABLE IF NOT EXISTS `endpoint_entry` ( `type` varchar(100) NOT NULL, `credential_id` int NOT NULL, CONSTRAINT `fk_credential` FOREIGN KEY (`credential_id`) REFERENCES `credential` (`id`) ON DELETE CASCADE -); +); \ No newline at end of file