Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fixes #9550] Show creator/editor in 'Edit properties' #333

Merged
merged 7 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/sql/002_create_schema_oracle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
metadata varchar2(4000 char),
name varchar2(255 char) not null,
category_id number(19,0) not null,
creator varchar2(255 char),
editor varchar2(255 char),
primary key (id),
unique (name)
);
Expand Down
2 changes: 2 additions & 0 deletions doc/sql/002_create_schema_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ psql -U geostore_test -d geostore -f 002_create_schema_postgres.sql
metadata varchar(30000),
name varchar(255) not null,
category_id int8 not null,
creator varchar(255),
editor varchar(255),
primary key (id),
unique (name)
);
Expand Down
2 changes: 2 additions & 0 deletions doc/sql/migration/h2/h2-migration-from-v.2.0.0-to-v2.1.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table gs_resource add column creator varchar(255);
alter table gs_resource add column editor varchar(255);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table gs_resource add column creator varchar2(255 char);
alter table gs_resource add column editor varchar2(255 char);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table gs_resource add column creator varchar(255);
alter table gs_resource add column editor varchar(255);
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ public class Resource implements Serializable, CycleRecoverable {
@Temporal(TemporalType.TIMESTAMP)
private Date lastUpdate;

@Column(nullable = true, updatable = true)
private String creator;

@Column(nullable = true, updatable = true)
private String editor;

@Column(nullable = true, updatable = true, length = 30000)
private String metadata;

Expand Down Expand Up @@ -256,6 +262,34 @@ public void setSecurity(List<SecurityRule> security) {
this.security = security;
}

/**
* @return the creator username
*/
public String getCreator() {
return creator;
}

/**
* @param creator the creator username
*/
public void setCreator(String creator) {
this.creator = creator;
}

/**
* @return the editor username
*/
public String getEditor() {
return editor;
}

/**
* @param editor the creator username
*/
public void setEditor(String editor) {
this.editor = editor;
}

/*
* (non-Javadoc) @see java.lang.Object#toString()
*/
Expand Down Expand Up @@ -290,17 +324,27 @@ public String toString() {

if (attribute != null) {
builder.append(", ");
builder.append("attribute=").append(attribute.toString());
builder.append("attribute=").append(attribute);
}

if (data != null) {
builder.append(", ");
builder.append("data=").append(data.toString());
builder.append("data=").append(data);
}

if (category != null) {
builder.append(", ");
builder.append("category=").append(category.toString());
builder.append("category=").append(category);
}

if (creator != null) {
builder.append(", ");
builder.append("creator=").append(creator);
}

if (editor != null) {
builder.append(", ");
builder.append("editor=").append(editor);
}

builder.append(']');
Expand All @@ -325,6 +369,8 @@ public int hashCode() {
result = (prime * result) + ((metadata == null) ? 0 : metadata.hashCode());
result = (prime * result) + ((name == null) ? 0 : name.hashCode());
result = (prime * result) + ((security == null) ? 0 : security.hashCode());
result = (prime * result) + ((creator == null) ? 0 : creator.hashCode());
result = (prime * result) + ((editor == null) ? 0 : editor.hashCode());

return result;
}
Expand Down Expand Up @@ -409,12 +455,20 @@ public boolean equals(Object obj) {
return false;
}
if (security == null) {
if (other.security != null) {
return false;
}
return other.security == null;
} else if (!security.equals(other.security)) {
return false;
}
if (creator == null) {
return other.creator == null;
} else if (!creator.equals(other.creator)) {
return false;
}
if (editor == null) {
return other.editor == null;
} else if (!editor.equals(other.editor)) {
return false;
}

return true;
}
Expand All @@ -432,7 +486,10 @@ public Object onCycleDetected(Context arg0) {
r.setName(this.name);
r.setAttribute(null);
r.setData(null);
r.setCreator(this.creator);
r.setEditor(this.editor);

return r;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface SecurityDAO extends RestrictedGenericDAO<SecurityRule> {

/**
*
* @param userName
* @param groupNames
* @param resourceId
* @return
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;


import it.geosolutions.geostore.core.dao.ResourceDAO;
import it.geosolutions.geostore.core.model.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -48,6 +50,9 @@ public class SecurityDAOImpl extends BaseDAO<SecurityRule, Long> implements Secu
private static final Logger LOGGER = LogManager.getLogger(SecurityDAOImpl.class);

private UserGroupDAO userGroupDAO;

private ResourceDAO resourceDAO;

/*
* (non-Javadoc)
*
Expand All @@ -60,10 +65,11 @@ public void persist(SecurityRule... entities) {
}
for (SecurityRule rule : entities) {
validateGroup(rule);
validateCreatorAndEditor(rule);
}
super.persist(entities);
}

protected void validateGroup(SecurityRule rule) throws InternalError {
if (rule.getGroup() != null) {
UserGroup ug = userGroupDAO.find(rule.getGroup().getId());
Expand All @@ -74,6 +80,26 @@ protected void validateGroup(SecurityRule rule) throws InternalError {
}
}

private void validateCreatorAndEditor(SecurityRule rule) {
if (rule.getResource() != null && (rule.getUser() != null || rule.getUsername() != null)) {
Resource resource = rule.getResource();
boolean updated = false;
if (resource.getCreator() == null) {
resource.setCreator(
rule.getUser() != null ? rule.getUser().getName() : rule.getUsername());
updated = true;
}
if (rule.getUser() != null || !rule.getUsername().isEmpty()) {
resource.setEditor(
rule.getUser() != null ? rule.getUser().getName() : rule.getUsername());
updated = true;
}
if (updated) {
resourceDAO.merge(resource);
}
}
}

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -214,6 +240,12 @@ public UserGroupDAO getUserGroupDAO() {
public void setUserGroupDAO(UserGroupDAO userGroupDAO) {
this.userGroupDAO = userGroupDAO;
}



public ResourceDAO getResourceDAO() {
return resourceDAO;
}

public void setResourceDAO(ResourceDAO resourceDAO) {
this.resourceDAO = resourceDAO;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public void testPersistResource() throws Exception {
resource.setName(NAME1);
resource.setCreation(new Date());
resource.setCategory(category);
resource.setCreator("USER1");
resource.setEditor("USER2");

resourceDAO.persist(resource);
resourceId = resource.getId();
Expand Down Expand Up @@ -103,14 +105,20 @@ public void testPersistResource() throws Exception {
assertNotNull("Can't retrieve resource", loaded);

assertEquals(NAME1, loaded.getName());
assertEquals("USER1", loaded.getCreator());
assertEquals("USER2", loaded.getEditor());
loaded.setName(NAME2);
loaded.setCreator("USER1Updated");
loaded.setEditor("USER2Updated");
resourceDAO.merge(loaded);
}

{
Resource loaded = resourceDAO.find(resourceId);
assertNotNull("Can't retrieve resource", loaded);
assertEquals(NAME2, loaded.getName());
assertEquals("USER1Updated", loaded.getCreator());
assertEquals("USER2Updated", loaded.getEditor());
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class SecurityDAOTest extends BaseDAOTest {
*/
@Test
public void testPersistSecurity() throws Exception {

final String NAME = "NAME";

if (LOGGER.isDebugEnabled()) {
Expand Down Expand Up @@ -81,6 +80,11 @@ public void testPersistSecurity() throws Exception {
assertEquals(1, resourceDAO.count(null));
assertEquals(1, resourceDAO.findAll().size());

Resource loaded = resourceDAO.find(resourceId);
assertNotNull(loaded);
assertNull(loaded.getCreator());
assertNull(loaded.getEditor());

// SecurityRule security = new SecurityRule();
// security.setCanRead(true);
// security.setCanWrite(true);
Expand All @@ -105,16 +109,21 @@ public void testPersistSecurity() throws Exception {
// PERSIST
//
{
Resource loaded = resourceDAO.find(resourceId);
SecurityRule security = new SecurityRule();
security.setCanRead(true);
security.setCanWrite(true);
security.setResource(resourceDAO.find(resourceId));
security.setResource(loaded);

securityDAO.persist(security);
securityId = security.getId();

assertEquals(1, securityDAO.count(null));
assertEquals(1, securityDAO.findAll().size());

loaded = resourceDAO.find(resourceId);
assertNull(loaded.getCreator());
assertNull(loaded.getEditor());
}

//
Expand Down Expand Up @@ -146,7 +155,6 @@ public void testPersistSecurity() throws Exception {

@Test
public void testPersistSecurityUsingNames() throws Exception {

final String NAME = "NAME";
final String USERNAME= "USER";
final String GROUPNAME= "GROUP";
Expand Down Expand Up @@ -200,6 +208,9 @@ public void testPersistSecurityUsingNames() throws Exception {
assertEquals(1, resourceDAO.count(null));
assertEquals(1, resourceDAO.findAll().size());

Resource loaded = resourceDAO.find(resourceId);
assertNull(loaded.getCreator());
assertNull(loaded.getEditor());
}

//
Expand All @@ -219,18 +230,30 @@ public void testPersistSecurityUsingNames() throws Exception {
SecurityRule rule = securityDAO.find(securityId);
assertNotNull(rule);
assertNotNull(rule.getUsername());

Resource loaded = resourceDAO.find(resourceId);
assertNotNull(loaded.getCreator());
assertNotNull(loaded.getEditor());
assertEquals("testuser", loaded.getCreator());
assertEquals("testuser", loaded.getEditor());

securityDAO.removeById(securityId);
}

//
// PERSIST WITH USER
//
{
Resource loaded = resourceDAO.find(resourceId);
loaded.setEditor(null);
resourceDAO.merge(loaded);

SecurityRule security = new SecurityRule();
security.setCanRead(true);
security.setCanWrite(true);
security.setResource(resourceDAO.find(resourceId));
User testUser = new User();
testUser.setName("TheTestUser");
testUser.setId(userId);
security.setUser(testUser);
securityDAO.persist(security);
Expand All @@ -241,6 +264,13 @@ public void testPersistSecurityUsingNames() throws Exception {
SecurityRule rule = securityDAO.find(securityId);
assertNotNull(rule);
assertNotNull(rule.getUser());

loaded = resourceDAO.find(resourceId);
assertNotNull(loaded.getCreator());
assertNotNull(loaded.getEditor());
assertEquals("testuser", loaded.getCreator());
assertEquals(testUser.getName(), loaded.getEditor());

securityDAO.removeById(securityId);
}

Expand Down
Loading
Loading