From e136701bbd4aac80ceb98482aa88e0c192c3925e Mon Sep 17 00:00:00 2001 From: Matija Obreza Date: Mon, 24 Dec 2018 17:16:23 +0100 Subject: [PATCH] Serialize Audited `lastModifiedBy` and `createdBy` using JsonSidConverter - Fields are marked as not indexed in ES --- .../blocks/model/AuditedVersionedModel.java | 12 +++- .../model/AuditedVersionedModelWithoutId.java | 8 +++ .../genesys/blocks/util/JsonSidConverter.java | 55 +++++++++++++++++++ parent/pom.xml | 13 +++++ .../security/service/CustomAclService.java | 11 +++- .../service/impl/CustomAclServiceImpl.java | 12 ++++ 6 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 core/src/main/java/org/genesys/blocks/util/JsonSidConverter.java diff --git a/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModel.java b/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModel.java index 7430d0a..e5420de 100644 --- a/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModel.java +++ b/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModel.java @@ -20,12 +20,16 @@ import java.util.Date; import javax.persistence.Column; import javax.persistence.MappedSuperclass; -import com.fasterxml.jackson.annotation.JsonView; - +import org.genesys.blocks.util.JsonSidConverter; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldIndex; + +import com.fasterxml.jackson.annotation.JsonView; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; /** * The Class AuditedVersionedModel. @@ -40,6 +44,8 @@ public abstract class AuditedVersionedModel extends VersionedModel { @JsonView(JsonViews.Protected.class) @CreatedBy @Column(updatable = false) + @JsonSerialize(converter = JsonSidConverter.class) + @Field(index = FieldIndex.no) private Long createdBy; /** The created date. */ @@ -51,6 +57,8 @@ public abstract class AuditedVersionedModel extends VersionedModel { /** The last modified by. */ @JsonView(JsonViews.Protected.class) @LastModifiedBy + @JsonSerialize(converter = JsonSidConverter.class) + @Field(index = FieldIndex.no) private Long lastModifiedBy; /** The last modified date. */ diff --git a/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModelWithoutId.java b/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModelWithoutId.java index 6d64ee5..894c4c3 100644 --- a/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModelWithoutId.java +++ b/core/src/main/java/org/genesys/blocks/model/AuditedVersionedModelWithoutId.java @@ -21,11 +21,15 @@ import javax.persistence.Column; import javax.persistence.MappedSuperclass; import com.fasterxml.jackson.annotation.JsonView; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import org.genesys.blocks.util.JsonSidConverter; import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.elasticsearch.annotations.Field; +import org.springframework.data.elasticsearch.annotations.FieldIndex; /** * The Class AuditedVersionedModelWithoutId. @@ -40,6 +44,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho @JsonView(JsonViews.Protected.class) @CreatedBy @Column(updatable = false) + @JsonSerialize(converter = JsonSidConverter.class) + @Field(index = FieldIndex.no) private Long createdBy; /** The created date. */ @@ -51,6 +57,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho /** The last modified by. */ @JsonView(JsonViews.Protected.class) @LastModifiedBy + @JsonSerialize(converter = JsonSidConverter.class) + @Field(index = FieldIndex.no) private Long lastModifiedBy; /** The last modified date. */ diff --git a/core/src/main/java/org/genesys/blocks/util/JsonSidConverter.java b/core/src/main/java/org/genesys/blocks/util/JsonSidConverter.java new file mode 100644 index 0000000..301df9d --- /dev/null +++ b/core/src/main/java/org/genesys/blocks/util/JsonSidConverter.java @@ -0,0 +1,55 @@ +/* + * Copyright 2018 Global Crop Diversity Trust + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.genesys.blocks.util; + +import com.fasterxml.jackson.databind.util.StdConverter; + +/** + * The JsonSidConverter converts SID IDs to SID names. + */ +public class JsonSidConverter extends StdConverter { + + private static SidProvider SID_PROVIDER; + + public static interface SidProvider { + String getSidName(long id); + } + + /** + * Sets the sid provider. + * + * @param sidProvider the new sid provider + */ + public static void setSidProvider(SidProvider sidProvider) { + SID_PROVIDER = sidProvider; + } + + /** + * Convert SID ID to SID name using SID_PROVIDER (when available) + */ + @Override + public Object convert(Long value) { + if (value == null) { + return null; + } else { + if (SID_PROVIDER == null) { + return value; + } else { + return SID_PROVIDER.getSidName(value); + } + } + } +} diff --git a/parent/pom.xml b/parent/pom.xml index 3e099b9..fd97ef2 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -218,6 +218,19 @@ test + + org.springframework.data + spring-data-elasticsearch + 2.1.15.RELEASE + provided + + + org.elasticsearch + elasticsearch + + + + junit diff --git a/security/src/main/java/org/genesys/blocks/security/service/CustomAclService.java b/security/src/main/java/org/genesys/blocks/security/service/CustomAclService.java index 0899f90..737d83f 100644 --- a/security/src/main/java/org/genesys/blocks/security/service/CustomAclService.java +++ b/security/src/main/java/org/genesys/blocks/security/service/CustomAclService.java @@ -17,6 +17,7 @@ package org.genesys.blocks.security.service; import java.util.List; +import org.genesys.blocks.util.JsonSidConverter; import org.genesys.blocks.security.model.AclAwareModel; import org.genesys.blocks.security.model.AclClass; import org.genesys.blocks.security.model.AclEntry; @@ -29,7 +30,7 @@ import org.springframework.security.acls.model.Permission; /** * The Interface CustomAclService. */ -public interface CustomAclService { +public interface CustomAclService extends JsonSidConverter.SidProvider { /** * Gets the available permissions. @@ -236,4 +237,12 @@ public interface CustomAclService { * @return the sid id */ Long getSidId(String sid); + + /** + * Gets the sid name. + * + * @param id the id + * @return the sid name + */ + String getSidName(long id); } diff --git a/security/src/main/java/org/genesys/blocks/security/service/impl/CustomAclServiceImpl.java b/security/src/main/java/org/genesys/blocks/security/service/impl/CustomAclServiceImpl.java index dfc7b5d..824696d 100644 --- a/security/src/main/java/org/genesys/blocks/security/service/impl/CustomAclServiceImpl.java +++ b/security/src/main/java/org/genesys/blocks/security/service/impl/CustomAclServiceImpl.java @@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.Cacheable; import org.springframework.security.access.prepost.PostAuthorize; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.acls.domain.BasePermission; @@ -94,9 +95,19 @@ public class CustomAclServiceImpl implements CustomAclService { public AclSid getSid(Long id) { return aclSidPersistence.findOne(id); } + + + @Override + @Transactional(readOnly = true) + @Cacheable(cacheNames = { "aclSidNames" }, key = "#id", unless = "#result == null") + public String getSidName(long id) { + AclSid sid = aclSidPersistence.findOne(id); + return sid == null ? null : sid.getSid(); + } @Override @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED) + @Cacheable(cacheNames = { "aclSidNames" }, key = "#sid", unless = "#result == null") public Long getSidId(String sid) { return aclSidPersistence.getSidId(sid); } @@ -688,4 +699,5 @@ public class CustomAclServiceImpl implements CustomAclService { } LOG.warn("Done cleaning ACL for {} ACL classes", aclClasses.size()); } + } -- GitLab