1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-13 09:46:17 +00:00

Closes #69: Save and display originating user in audit log

This commit is contained in:
jendib
2016-02-15 22:28:13 +01:00
parent 831e2e60ed
commit d8d01b077d
28 changed files with 157 additions and 127 deletions

View File

@@ -26,10 +26,11 @@ public class AclDao {
* Creates a new ACL.
*
* @param acl ACL
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(Acl acl) {
public String create(Acl acl, String userId) {
// Create the UUID
acl.setId(UUID.randomUUID().toString());
@@ -38,7 +39,7 @@ public class AclDao {
em.persist(acl);
// Create audit log
AuditLogUtil.create(acl, AuditLogType.CREATE);
AuditLogUtil.create(acl, AuditLogType.CREATE, userId);
return acl.getId();
}
@@ -125,9 +126,10 @@ public class AclDao {
* @param sourceId Source ID
* @param perm Permission
* @param targetId Target ID
* @param userId User ID
*/
@SuppressWarnings("unchecked")
public void delete(String sourceId, PermType perm, String targetId) {
public void delete(String sourceId, PermType perm, String targetId, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Create audit log
@@ -137,7 +139,7 @@ public class AclDao {
q.setParameter("targetId", targetId);
List<Acl> aclList = q.getResultList();
for (Acl acl : aclList) {
AuditLogUtil.create(acl, AuditLogType.DELETE);
AuditLogUtil.create(acl, AuditLogType.DELETE, userId);
}
// Soft delete the ACLs

View File

@@ -59,12 +59,13 @@ public class AuditLogDao {
public void findByCriteria(PaginatedList<AuditLogDto> paginatedList, AuditLogCriteria criteria, SortCriteria sortCriteria) throws Exception {
Map<String, Object> parameterMap = new HashMap<String, Object>();
String baseQuery = "select l.LOG_ID_C c0, l.LOG_CREATEDATE_D c1, l.LOG_IDENTITY_C c2, l.LOG_CLASSENTITY_C c3, l.LOG_TYPE_C c4, l.LOG_MESSAGE_C c5 from T_AUDIT_LOG l ";
StringBuilder baseQuery = new StringBuilder("select l.LOG_ID_C c0, l.LOG_CREATEDATE_D c1, u.USE_USERNAME_C c2, l.LOG_IDENTITY_C c3, l.LOG_CLASSENTITY_C c4, l.LOG_TYPE_C c5, l.LOG_MESSAGE_C c6 from T_AUDIT_LOG l ");
baseQuery.append(" join T_USER u on l.LOG_IDUSER_C = u.USE_ID_C ");
List<String> queries = Lists.newArrayList();
// Adds search criteria
if (criteria.getDocumentId() != null) {
// ACL on document is not checked here, it's assumed
// ACL on document is not checked here, rights have been checked before
queries.add(baseQuery + " where l.LOG_IDENTITY_C = :documentId ");
queries.add(baseQuery + " where l.LOG_IDENTITY_C in (select f.FIL_ID_C from T_FILE f where f.FIL_IDDOC_C = :documentId) ");
queries.add(baseQuery + " where l.LOG_IDENTITY_C in (select c.COM_ID_C from T_COMMENT c where c.COM_IDDOC_C = :documentId) ");
@@ -73,11 +74,9 @@ public class AuditLogDao {
}
if (criteria.getUserId() != null) {
queries.add(baseQuery + " where l.LOG_IDENTITY_C = :userId ");
queries.add(baseQuery + " where l.LOG_IDENTITY_C in (select t.TAG_ID_C from T_TAG t where t.TAG_IDUSER_C = :userId) ");
// Show only logs from owned documents, ACL are lost on delete
queries.add(baseQuery + " where l.LOG_IDENTITY_C in (select d.DOC_ID_C from T_DOCUMENT d where d.DOC_IDUSER_C = :userId) ");
queries.add(baseQuery + " where l.LOG_IDENTITY_C in (select c.COM_ID_C from T_COMMENT c where c.COM_IDUSER_C = :userId) ");
// Get all logs originating from the user, not necessarly on owned items
// Filter out ACL logs
queries.add(baseQuery + " where l.LOG_IDUSER_C = :userId and l.LOG_CLASSENTITY_C != 'Acl' ");
parameterMap.put("userId", criteria.getUserId());
}
@@ -92,6 +91,7 @@ public class AuditLogDao {
AuditLogDto auditLogDto = new AuditLogDto();
auditLogDto.setId((String) o[i++]);
auditLogDto.setCreateTimestamp(((Timestamp) o[i++]).getTime());
auditLogDto.setUsername((String) o[i++]);
auditLogDto.setEntityId((String) o[i++]);
auditLogDto.setEntityClass((String) o[i++]);
auditLogDto.setType(AuditLogType.valueOf((String) o[i++]));

View File

@@ -26,10 +26,11 @@ public class CommentDao {
* Creates a new comment.
*
* @param comment Comment
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(Comment comment) {
public String create(Comment comment, String userId) {
// Create the UUID
comment.setId(UUID.randomUUID().toString());
@@ -39,7 +40,7 @@ public class CommentDao {
em.persist(comment);
// Create audit log
AuditLogUtil.create(comment, AuditLogType.CREATE);
AuditLogUtil.create(comment, AuditLogType.CREATE, userId);
return comment.getId();
}
@@ -48,8 +49,9 @@ public class CommentDao {
* Deletes a comment.
*
* @param id Comment ID
* @param userId User ID
*/
public void delete(String id) {
public void delete(String id, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the comment
@@ -62,7 +64,7 @@ public class CommentDao {
commentDb.setDeleteDate(dateNow);
// Create audit log
AuditLogUtil.create(commentDb, AuditLogType.DELETE);
AuditLogUtil.create(commentDb, AuditLogType.DELETE, userId);
}
/**

View File

@@ -38,10 +38,11 @@ public class DocumentDao {
* Creates a new document.
*
* @param document Document
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(Document document) {
public String create(Document document, String userId) {
// Create the UUID
document.setId(UUID.randomUUID().toString());
@@ -50,7 +51,7 @@ public class DocumentDao {
em.persist(document);
// Create audit log
AuditLogUtil.create(document, AuditLogType.CREATE);
AuditLogUtil.create(document, AuditLogType.CREATE, userId);
return document.getId();
}
@@ -152,8 +153,9 @@ public class DocumentDao {
* Deletes a document.
*
* @param id Document ID
* @param userId User ID
*/
public void delete(String id) {
public void delete(String id, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the document
@@ -182,7 +184,7 @@ public class DocumentDao {
q.executeUpdate();
// Create audit log
AuditLogUtil.create(documentDb, AuditLogType.DELETE);
AuditLogUtil.create(documentDb, AuditLogType.DELETE, userId);
}
/**
@@ -291,9 +293,10 @@ public class DocumentDao {
* Update a document.
*
* @param document Document to update
* @param userId User ID
* @return Updated document
*/
public Document update(Document document) {
public Document update(Document document, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the document
@@ -316,7 +319,7 @@ public class DocumentDao {
documentFromDb.setLanguage(document.getLanguage());
// Create audit log
AuditLogUtil.create(documentFromDb, AuditLogType.UPDATE);
AuditLogUtil.create(documentFromDb, AuditLogType.UPDATE, userId);
return documentFromDb;
}

View File

@@ -23,10 +23,11 @@ public class FileDao {
* Creates a new file.
*
* @param file File
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(File file) {
public String create(File file, String userId) {
// Create the UUID
file.setId(UUID.randomUUID().toString());
@@ -36,7 +37,7 @@ public class FileDao {
em.persist(file);
// Create audit log
AuditLogUtil.create(file, AuditLogType.CREATE);
AuditLogUtil.create(file, AuditLogType.CREATE, userId);
return file.getId();
}
@@ -107,8 +108,9 @@ public class FileDao {
* Deletes a file.
*
* @param id File ID
* @param userId User ID
*/
public void delete(String id) {
public void delete(String id, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the file
@@ -121,7 +123,7 @@ public class FileDao {
fileDb.setDeleteDate(dateNow);
// Create audit log
AuditLogUtil.create(fileDb, AuditLogType.DELETE);
AuditLogUtil.create(fileDb, AuditLogType.DELETE, userId);
}
/**

View File

@@ -170,10 +170,11 @@ public class TagDao {
* Creates a new tag.
*
* @param tag Tag
* @param userId User ID
* @return New ID
* @throws Exception
*/
public String create(Tag tag) {
public String create(Tag tag, String userId) {
// Create the UUID
tag.setId(UUID.randomUUID().toString());
@@ -183,7 +184,7 @@ public class TagDao {
em.persist(tag);
// Create audit log
AuditLogUtil.create(tag, AuditLogType.CREATE);
AuditLogUtil.create(tag, AuditLogType.CREATE, userId);
return tag.getId();
}
@@ -230,8 +231,9 @@ public class TagDao {
* Deletes a tag.
*
* @param tagId Tag ID
* @param userId User ID
*/
public void delete(String tagId) {
public void delete(String tagId, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the tag
@@ -250,7 +252,7 @@ public class TagDao {
q.executeUpdate();
// Create audit log
AuditLogUtil.create(tagDb, AuditLogType.DELETE);
AuditLogUtil.create(tagDb, AuditLogType.DELETE, userId);
}
/**
@@ -272,9 +274,10 @@ public class TagDao {
* Update a tag.
*
* @param tag Tag to update
* @param userId User ID
* @return Updated tag
*/
public Tag update(Tag tag) {
public Tag update(Tag tag, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the tag
@@ -288,7 +291,7 @@ public class TagDao {
tagFromDb.setParentId(tag.getParentId());
// Create audit log
AuditLogUtil.create(tagFromDb, AuditLogType.UPDATE);
AuditLogUtil.create(tagFromDb, AuditLogType.UPDATE, userId);
return tagFromDb;
}

View File

@@ -58,10 +58,11 @@ public class UserDao {
* Creates a new user.
*
* @param user User to create
* @param userId User ID
* @return User ID
* @throws Exception
*/
public String create(User user) throws Exception {
public String create(User user, String userId) throws Exception {
// Create the user UUID
user.setId(UUID.randomUUID().toString());
@@ -80,7 +81,7 @@ public class UserDao {
em.persist(user);
// Create audit log
AuditLogUtil.create(user, AuditLogType.CREATE);
AuditLogUtil.create(user, AuditLogType.CREATE, userId);
return user.getId();
}
@@ -89,9 +90,10 @@ public class UserDao {
* Updates a user.
*
* @param user User to update
* @param userId User ID
* @return Updated user
*/
public User update(User user) {
public User update(User user, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the user
@@ -99,13 +101,13 @@ public class UserDao {
q.setParameter("id", user.getId());
User userFromDb = (User) q.getSingleResult();
// Update the user
// Update the user (except password)
userFromDb.setEmail(user.getEmail());
userFromDb.setStorageQuota(user.getStorageQuota());
userFromDb.setStorageCurrent(user.getStorageCurrent());
// Create audit log
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE);
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE, userId);
return user;
}
@@ -134,9 +136,10 @@ public class UserDao {
* Update the user password.
*
* @param user User to update
* @param userId User ID
* @return Updated user
*/
public User updatePassword(User user) {
public User updatePassword(User user, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the user
@@ -148,7 +151,7 @@ public class UserDao {
userFromDb.setPassword(hashPassword(user.getPassword()));
// Create audit log
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE);
AuditLogUtil.create(userFromDb, AuditLogType.UPDATE, userId);
return user;
}
@@ -206,8 +209,9 @@ public class UserDao {
* Deletes a user.
*
* @param username User's username
* @param userId User ID
*/
public void delete(String username) {
public void delete(String username, String userId) {
EntityManager em = ThreadLocalContext.get().getEntityManager();
// Get the user
@@ -245,7 +249,7 @@ public class UserDao {
q.executeUpdate();
// Create audit log
AuditLogUtil.create(userFromDb, AuditLogType.DELETE);
AuditLogUtil.create(userFromDb, AuditLogType.DELETE, userId);
}
/**

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
import com.sismics.docs.core.constant.PermType;
/**
@@ -13,7 +11,6 @@ public class AclDto {
/**
* Acl ID.
*/
@Id
private String id;
/**

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
import com.sismics.docs.core.constant.AuditLogType;
/**
@@ -13,9 +11,13 @@ public class AuditLogDto {
/**
* Audit log ID.
*/
@Id
private String id;
/**
* Username.
*/
private String username;
/**
* Entity ID.
*/
@@ -49,6 +51,14 @@ public class AuditLogDto {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEntityId() {
return entityId;
}

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
/**
* Comment DTO.
*
@@ -11,7 +9,6 @@ public class CommentDto {
/**
* Comment ID.
*/
@Id
private String id;
/**

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
/**
* Document DTO.
*
@@ -11,7 +9,6 @@ public class DocumentDto {
/**
* Document ID.
*/
@Id
private String id;
/**

View File

@@ -1,7 +1,5 @@
package com.sismics.docs.core.dao.jpa.dto;
import javax.persistence.Id;
/**
* Tag DTO.
*
@@ -11,7 +9,6 @@ public class TagDto {
/**
* Tag ID.
*/
@Id
private String id;
/**

View File

@@ -37,69 +37,38 @@ public class UserDto {
*/
private Long storageCurrent;
/**
* Getter of id.
*
* @return id
*/
public String getId() {
return id;
}
/**
* Setter of id.
*
* @param id id
*/
public void setId(String id) {
this.id = id;
}
/**
* Getter of username.
*
* @return username
*/
public String getUsername() {
return username;
}
/**
* Setter of username.
*
* @param username username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Getter of email.
*
* @return email
*/
public String getEmail() {
return email;
}
/**
* Setter of email.
*
* @param email email
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Getter of createTimestamp.
*
* @return createTimestamp
*/
public Long getCreateTimestamp() {
return createTimestamp;
}
public void setCreateTimestamp(Long createTimestamp) {
this.createTimestamp = createTimestamp;
}
public Long getStorageQuota() {
return storageQuota;
}
@@ -115,13 +84,4 @@ public class UserDto {
public void setStorageCurrent(Long storageCurrent) {
this.storageCurrent = storageCurrent;
}
/**
* Setter of createTimestamp.
*
* @param createTimestamp createTimestamp
*/
public void setCreateTimestamp(Long createTimestamp) {
this.createTimestamp = createTimestamp;
}
}

View File

@@ -27,6 +27,12 @@ public class AuditLog {
@Column(name = "LOG_ID_C", length = 36)
private String id;
/**
* User ID.
*/
@Column(name = "LOG_IDUSER_C", nullable = false, length = 36)
private String userId;
/**
* Entity ID.
*/
@@ -66,6 +72,14 @@ public class AuditLog {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getEntityId() {
return entityId;
}

View File

@@ -20,7 +20,7 @@ public class AuditLogUtil {
* @param entity Entity
* @param type Audit log type
*/
public static void create(Loggable loggable, AuditLogType type) {
public static void create(Loggable loggable, AuditLogType type, String userId) {
// Get the entity ID
EntityManager em = ThreadLocalContext.get().getEntityManager();
String entityId = (String) em.getEntityManagerFactory().getPersistenceUnitUtil().getIdentifier(loggable);
@@ -28,6 +28,7 @@ public class AuditLogUtil {
// Create the audit log
AuditLogDao auditLogDao = new AuditLogDao();
AuditLog auditLog = new AuditLog();
auditLog.setUserId(userId);
auditLog.setEntityId(entityId);
auditLog.setEntityClass(loggable.getClass().getSimpleName());
auditLog.setType(type);

View File

@@ -6,6 +6,8 @@ alter table T_DOCUMENT add column DOC_SOURCE_C varchar(500);
alter table T_DOCUMENT add column DOC_TYPE_C varchar(500);
alter table T_DOCUMENT add column DOC_COVERAGE_C varchar(500);
alter table T_DOCUMENT add column DOC_RIGHTS_C varchar(500);
alter table T_AUDIT_LOG add column LOG_IDUSER_C varchar(36) not null default 'admin';
create memory table T_VOCABULARY ( VOC_ID_C varchar(36) not null, VOC_NAME_C varchar(50) not null, VOC_VALUE_C varchar(500) not null, VOC_ORDER_N int not null, primary key (VOC_ID_C) );
insert into T_VOCABULARY(VOC_ID_C, VOC_NAME_C, VOC_VALUE_C, VOC_ORDER_N) values('type-collection', 'type', 'Collection', 0);

View File

@@ -23,7 +23,7 @@ public class TestJpa extends BaseTransactionalTest {
user.setStorageCurrent(0l);
user.setStorageQuota(10l);
user.setPrivateKey("AwesomePrivateKey");
String id = userDao.create(user);
String id = userDao.create(user, "me");
TransactionUtil.commit();