Commit e136701b authored by Matija Obreza's avatar Matija Obreza

Serialize Audited `lastModifiedBy` and `createdBy` using JsonSidConverter

- Fields are marked as not indexed in ES
parent a3c4dfd2
...@@ -20,12 +20,16 @@ import java.util.Date; ...@@ -20,12 +20,16 @@ import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.MappedSuperclass; 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.CreatedBy;
import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate; 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. * The Class AuditedVersionedModel.
...@@ -40,6 +44,8 @@ public abstract class AuditedVersionedModel extends VersionedModel { ...@@ -40,6 +44,8 @@ public abstract class AuditedVersionedModel extends VersionedModel {
@JsonView(JsonViews.Protected.class) @JsonView(JsonViews.Protected.class)
@CreatedBy @CreatedBy
@Column(updatable = false) @Column(updatable = false)
@JsonSerialize(converter = JsonSidConverter.class)
@Field(index = FieldIndex.no)
private Long createdBy; private Long createdBy;
/** The created date. */ /** The created date. */
...@@ -51,6 +57,8 @@ public abstract class AuditedVersionedModel extends VersionedModel { ...@@ -51,6 +57,8 @@ public abstract class AuditedVersionedModel extends VersionedModel {
/** The last modified by. */ /** The last modified by. */
@JsonView(JsonViews.Protected.class) @JsonView(JsonViews.Protected.class)
@LastModifiedBy @LastModifiedBy
@JsonSerialize(converter = JsonSidConverter.class)
@Field(index = FieldIndex.no)
private Long lastModifiedBy; private Long lastModifiedBy;
/** The last modified date. */ /** The last modified date. */
......
...@@ -21,11 +21,15 @@ import javax.persistence.Column; ...@@ -21,11 +21,15 @@ import javax.persistence.Column;
import javax.persistence.MappedSuperclass; import javax.persistence.MappedSuperclass;
import com.fasterxml.jackson.annotation.JsonView; 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.CreatedBy;
import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
/** /**
* The Class AuditedVersionedModelWithoutId. * The Class AuditedVersionedModelWithoutId.
...@@ -40,6 +44,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho ...@@ -40,6 +44,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho
@JsonView(JsonViews.Protected.class) @JsonView(JsonViews.Protected.class)
@CreatedBy @CreatedBy
@Column(updatable = false) @Column(updatable = false)
@JsonSerialize(converter = JsonSidConverter.class)
@Field(index = FieldIndex.no)
private Long createdBy; private Long createdBy;
/** The created date. */ /** The created date. */
...@@ -51,6 +57,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho ...@@ -51,6 +57,8 @@ public abstract class AuditedVersionedModelWithoutId extends VersionedModelWitho
/** The last modified by. */ /** The last modified by. */
@JsonView(JsonViews.Protected.class) @JsonView(JsonViews.Protected.class)
@LastModifiedBy @LastModifiedBy
@JsonSerialize(converter = JsonSidConverter.class)
@Field(index = FieldIndex.no)
private Long lastModifiedBy; private Long lastModifiedBy;
/** The last modified date. */ /** The last modified date. */
......
/*
* 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<Long, Object> {
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);
}
}
}
}
...@@ -218,6 +218,19 @@ ...@@ -218,6 +218,19 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>2.1.15.RELEASE</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Test dependencies --> <!-- Test dependencies -->
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
......
...@@ -17,6 +17,7 @@ package org.genesys.blocks.security.service; ...@@ -17,6 +17,7 @@ package org.genesys.blocks.security.service;
import java.util.List; import java.util.List;
import org.genesys.blocks.util.JsonSidConverter;
import org.genesys.blocks.security.model.AclAwareModel; import org.genesys.blocks.security.model.AclAwareModel;
import org.genesys.blocks.security.model.AclClass; import org.genesys.blocks.security.model.AclClass;
import org.genesys.blocks.security.model.AclEntry; import org.genesys.blocks.security.model.AclEntry;
...@@ -29,7 +30,7 @@ import org.springframework.security.acls.model.Permission; ...@@ -29,7 +30,7 @@ import org.springframework.security.acls.model.Permission;
/** /**
* The Interface CustomAclService. * The Interface CustomAclService.
*/ */
public interface CustomAclService { public interface CustomAclService extends JsonSidConverter.SidProvider {
/** /**
* Gets the available permissions. * Gets the available permissions.
...@@ -236,4 +237,12 @@ public interface CustomAclService { ...@@ -236,4 +237,12 @@ public interface CustomAclService {
* @return the sid id * @return the sid id
*/ */
Long getSidId(String sid); Long getSidId(String sid);
/**
* Gets the sid name.
*
* @param id the id
* @return the sid name
*/
String getSidName(long id);
} }
...@@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory; ...@@ -39,6 +39,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache; import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager; import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PostAuthorize; import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission; import org.springframework.security.acls.domain.BasePermission;
...@@ -94,9 +95,19 @@ public class CustomAclServiceImpl implements CustomAclService { ...@@ -94,9 +95,19 @@ public class CustomAclServiceImpl implements CustomAclService {
public AclSid getSid(Long id) { public AclSid getSid(Long id) {
return aclSidPersistence.findOne(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 @Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED) @Transactional(readOnly = true, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.READ_UNCOMMITTED)
@Cacheable(cacheNames = { "aclSidNames" }, key = "#sid", unless = "#result == null")
public Long getSidId(String sid) { public Long getSidId(String sid) {
return aclSidPersistence.getSidId(sid); return aclSidPersistence.getSidId(sid);
} }
...@@ -688,4 +699,5 @@ public class CustomAclServiceImpl implements CustomAclService { ...@@ -688,4 +699,5 @@ public class CustomAclServiceImpl implements CustomAclService {
} }
LOG.warn("Done cleaning ACL for {} ACL classes", aclClasses.size()); LOG.warn("Done cleaning ACL for {} ACL classes", aclClasses.size());
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment