Commit a66c7405 authored by Matija Obreza's avatar Matija Obreza
Browse files

Refactored for Genesys

parent 9e3f5da5
...@@ -32,14 +32,6 @@ public interface ClassPKService { ...@@ -32,14 +32,6 @@ public interface ClassPKService {
*/ */
ClassPK getClassPk(Class<?> class1); ClassPK getClassPk(Class<?> class1);
/**
* Get classPk id
*
* @param classname
* @return id of persisted class
*/
Long getClassPkId(String classname);
/** /**
* Get classPk classname * Get classPk classname
* *
...@@ -48,4 +40,6 @@ public interface ClassPKService { ...@@ -48,4 +40,6 @@ public interface ClassPKService {
*/ */
String getClassName(Long id); String getClassName(Long id);
ClassPK findByShortName(String classPKShortName);
} }
...@@ -15,9 +15,11 @@ ...@@ -15,9 +15,11 @@
*/ */
package org.genesys.blocks.auditlog.service.impl; package org.genesys.blocks.auditlog.service.impl;
import org.genesys.blocks.auditlog.persistence.ClassPKRepository; import javax.persistence.PersistenceException;
import org.genesys.blocks.auditlog.service.ClassPKService; import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.model.ClassPK; import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.persistence.ClassPKRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -53,6 +55,7 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -53,6 +55,7 @@ public class ClassPKServiceImpl implements ClassPKService {
} }
classPk = new ClassPK(); classPk = new ClassPK();
classPk.setClassname(class1.getName()); classPk.setClassname(class1.getName());
classPk.setShortName(generateShortPkName(class1.getSimpleName().toLowerCase()));
if (LOG.isTraceEnabled()) { if (LOG.isTraceEnabled()) {
LOG.trace("Persisting new ClassPK for classname={}", classPk.getClassname()); LOG.trace("Persisting new ClassPK for classname={}", classPk.getClassname());
...@@ -61,23 +64,27 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -61,23 +64,27 @@ public class ClassPKServiceImpl implements ClassPKService {
return classPkRepository.save(classPk); return classPkRepository.save(classPk);
} }
@Override private String generateShortPkName(String shortName) {
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW) ClassPK classPk = null;
public Long getClassPkId(final String classname) {
ClassPK classPK = classPkRepository.getByClassname(classname);
if (classPK != null) {
return classPK.getId();
}
classPK = new ClassPK();
classPK.setClassname(classname);
if (LOG.isTraceEnabled()) { String candidate = shortName;
LOG.trace("Persisting new ClassPK for classname={}", classname); int i = 0;
}
classPK = classPkRepository.save(classPK); do {
return classPK.getId(); classPk = classPkRepository.findByShortName(candidate);
if (classPk == null) {
// No other candidate, we're good!
return candidate;
}
i++;
// "shortName1", "shortName2"...
candidate = shortName + i;
} while (i < 10);
throw new PersistenceException("Can't generate a shortName for ClassPK for " + shortName);
} }
@Override @Override
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW) @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRES_NEW)
public String getClassName(final Long id) { public String getClassName(final Long id) {
...@@ -85,4 +92,9 @@ public class ClassPKServiceImpl implements ClassPKService { ...@@ -85,4 +92,9 @@ public class ClassPKServiceImpl implements ClassPKService {
return classPK.getClassname(); return classPK.getClassname();
} }
@Override
public ClassPK findByShortName(String classPKShortName) {
return classPkRepository.findByShortName(classPKShortName);
}
} }
...@@ -23,9 +23,9 @@ import java.util.Date; ...@@ -23,9 +23,9 @@ import java.util.Date;
import org.genesys.blocks.auditlog.model.AuditAction; import org.genesys.blocks.auditlog.model.AuditAction;
import org.genesys.blocks.auditlog.model.AuditLog; import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.persistence.AuditLogRepository; import org.genesys.blocks.auditlog.persistence.AuditLogRepository;
import org.genesys.blocks.auditlog.persistence.ClassPKRepository;
import org.genesys.blocks.auditlog.test.EntityTest; import org.genesys.blocks.auditlog.test.EntityTest;
import org.genesys.blocks.model.ClassPK; import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.persistence.ClassPKRepository;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
......
...@@ -18,9 +18,9 @@ package org.genesys.blocks.auditlog.model; ...@@ -18,9 +18,9 @@ package org.genesys.blocks.auditlog.model;
import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.genesys.blocks.auditlog.persistence.ClassPKRepository;
import org.genesys.blocks.auditlog.test.EntityTest; import org.genesys.blocks.auditlog.test.EntityTest;
import org.genesys.blocks.model.ClassPK; import org.genesys.blocks.model.ClassPK;
import org.genesys.blocks.persistence.ClassPKRepository;
import org.junit.Test; import org.junit.Test;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
......
...@@ -23,9 +23,9 @@ import java.util.List; ...@@ -23,9 +23,9 @@ import java.util.List;
import org.genesys.blocks.auditlog.model.AuditLog; import org.genesys.blocks.auditlog.model.AuditLog;
import org.genesys.blocks.auditlog.persistence.AuditLogRepository; import org.genesys.blocks.auditlog.persistence.AuditLogRepository;
import org.genesys.blocks.auditlog.persistence.ClassPKRepository;
import org.genesys.blocks.auditlog.service.ClassPKService; import org.genesys.blocks.auditlog.service.ClassPKService;
import org.genesys.blocks.model.EntityId; import org.genesys.blocks.model.EntityId;
import org.genesys.blocks.persistence.ClassPKRepository;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
......
...@@ -48,7 +48,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; ...@@ -48,7 +48,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration @Configuration
@EnableJpaRepositories(basePackages = { @EnableJpaRepositories(basePackages = {
"org.genesys.blocks.security.persistence", "org.genesys.blocks.auditlog.persistence" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager") "org.genesys.blocks.persistence", "org.genesys.blocks.security.persistence", "org.genesys.blocks.auditlog.persistence" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaAuditing(auditorAwareRef = "auditorAware", modifyOnCreate = true) @EnableJpaAuditing(auditorAwareRef = "auditorAware", modifyOnCreate = true)
public class DatabaseConfig { public class DatabaseConfig {
......
...@@ -48,6 +48,17 @@ public class ClassPK implements Serializable { ...@@ -48,6 +48,17 @@ public class ClassPK implements Serializable {
@Column(length = 200, updatable = false, unique = true, nullable = false) @Column(length = 200, updatable = false, unique = true, nullable = false)
protected String classname; protected String classname;
@Column(length = 50, unique = true)
private String shortName;
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
/** /**
* Gets the id. * Gets the id.
* *
......
...@@ -15,10 +15,8 @@ ...@@ -15,10 +15,8 @@
*/ */
package org.genesys.blocks.model; package org.genesys.blocks.model;
import java.io.Serializable;
public interface EntityId { public interface EntityId {
Serializable getId(); Long getId();
} }
...@@ -43,6 +43,6 @@ import java.util.UUID; ...@@ -43,6 +43,6 @@ import java.util.UUID;
public interface IdUUID { public interface IdUUID {
UUID getUuid(); UUID getUuid();
void setUuid(UUID uuid); // void setUuid(UUID uuid);
} }
...@@ -43,7 +43,6 @@ public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID ...@@ -43,7 +43,6 @@ public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID
return this.uuid; return this.uuid;
} }
@Override
public void setUuid(UUID uuid) { public void setUuid(UUID uuid) {
this.uuid = uuid; this.uuid = uuid;
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.genesys.blocks.auditlog.persistence; package org.genesys.blocks.persistence;
import org.genesys.blocks.model.ClassPK; import org.genesys.blocks.model.ClassPK;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
...@@ -35,4 +35,6 @@ public interface ClassPKRepository extends JpaRepository<ClassPK, Long> { ...@@ -35,4 +35,6 @@ public interface ClassPKRepository extends JpaRepository<ClassPK, Long> {
*/ */
ClassPK getByClassname(String classname); ClassPK getByClassname(String classname);
ClassPK findByShortName(String shortName);
} }
/*
* Copyright 2017 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.security.model; package org.genesys.blocks.security.model;
import java.io.Serializable; import java.io.Serializable;
import org.genesys.blocks.model.EntityId; import org.genesys.blocks.model.EntityId;
/**
* Interface label for entities that require ACL security
*/
public interface AclAwareModel extends Serializable, EntityId { public interface AclAwareModel extends Serializable, EntityId {
} }
/*
* Copyright 2017 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.security.model; package org.genesys.blocks.security.model;
import javax.persistence.Column; import javax.persistence.Column;
......
...@@ -8,6 +8,7 @@ import org.genesys.blocks.security.model.AclEntry; ...@@ -8,6 +8,7 @@ import org.genesys.blocks.security.model.AclEntry;
import org.genesys.blocks.security.model.AclObjectIdentity; import org.genesys.blocks.security.model.AclObjectIdentity;
import org.genesys.blocks.security.model.AclSid; import org.genesys.blocks.security.model.AclSid;
import org.springframework.security.acls.model.Permission; import org.springframework.security.acls.model.Permission;
import org.springframework.security.core.userdetails.UserDetails;
public interface CustomAclService { public interface CustomAclService {
...@@ -44,4 +45,6 @@ public interface CustomAclService { ...@@ -44,4 +45,6 @@ public interface CustomAclService {
AclObjectIdentity ensureObjectIdentity(String className, long objectIdIdentity); AclObjectIdentity ensureObjectIdentity(String className, long objectIdIdentity);
List<Integer> permissionsBySid(String className, Long id, String sid); List<Integer> permissionsBySid(String className, Long id, String sid);
List<Long> listIdentitiesForSid(Class<? extends AclAwareModel> clazz, UserDetails authUser, Permission permission);
} }
...@@ -23,6 +23,7 @@ import org.springframework.cache.CacheManager; ...@@ -23,6 +23,7 @@ import org.springframework.cache.CacheManager;
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;
import org.springframework.security.acls.model.Permission; import org.springframework.security.acls.model.Permission;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -315,4 +316,13 @@ public class CustomAclServiceImpl implements CustomAclService { ...@@ -315,4 +316,13 @@ public class CustomAclServiceImpl implements CustomAclService {
} }
return list; return list;
} }
@Override
@Transactional(readOnly = true)
public List<Long> listIdentitiesForSid(Class<? extends AclAwareModel> clazz, UserDetails authUser, Permission permission) {
return aclEntryPersistence.findObjectIdentitiesBySidAndAclClassAndMask(authUser.getUsername(), clazz.getName(), permission.getMask());
}
} }
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