Commit 4a94d934 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'json-serialize-sid-name-not-id' into 'master'

Serialize Audited `lastModifiedBy` and `createdBy` using JsonSidConverter

See merge request genesys-pgr/application-blocks!39
parents a3c4dfd2 e136701b
......@@ -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. */
......
......@@ -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. */
......
/*
* 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 @@
<scope>test</scope>
</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 -->
<dependency>
<groupId>junit</groupId>
......
......@@ -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);
}
......@@ -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());
}
}
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