Skip to content

Commit

Permalink
[Fixes #9550] Show creator/editor in 'Edit properties' (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
afabiani authored Feb 2, 2024
1 parent 5a546d1 commit b3be458
Show file tree
Hide file tree
Showing 27 changed files with 561 additions and 191 deletions.
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),
advertised bool not null default true,
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),
advertised bool not null default true,
primary key (id),
unique (name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ create index idx_user_group_attr_text on gs_user_group_attribute (string);

create index idx_attr_user_group on gs_user_group_attribute (userGroup_id);

alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup;
alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup;
12 changes: 11 additions & 1 deletion 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
@@ -1 +1,11 @@
alter table gs_resource add column advertised bool not null default true;
alter table gs_resource add column creator varchar(255);
alter table gs_resource add column editor varchar(255);
alter table gs_resource add column advertised bool not null default true;

-- Set the Resource Creator whether this is NULL
update gs_resource as gsr
set creator = subquery.name
from
(select gsu.name, gss.resource_id from geostore.gs_security as gss join geostore.gs_user as gsu on (gss.user_id = gsu.id)
where gss.user_id IS NOT NULL) as subquery
where gsr.id = subquery.resource_id and gsr.creator IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ create index idx_user_group_attr_text on gs_user_group_attribute (string);

create index idx_attr_user_group on gs_user_group_attribute (userGroup_id);

alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup;
alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup;
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
alter table gs_resource add column advertised bool not null default true;
alter table gs_resource add column creator varchar2(255 char);
alter table gs_resource add column editor varchar2(255 char);
alter table gs_resource add column advertised bool not null default true;

-- Set the Resource Creator whether this is NULL
update gs_resource as gsr
set creator = subquery.name
from
(select gsu.name, gss.resource_id from geostore.gs_security as gss join geostore.gs_user as gsu on (gss.user_id = gsu.id)
where gss.user_id IS NOT NULL) as subquery
where gsr.id = subquery.resource_id and gsr.creator IS NULL;
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
alter table gs_resource add column advertised bool not null default true;
alter table gs_resource add column creator varchar(255);
alter table gs_resource add column editor varchar(255);
alter table gs_resource add column advertised bool not null default true;

-- Set the Resource Creator whether this is NULL
update gs_resource as gsr
set creator = subquery.name
from
(select gsu.name, gss.resource_id from geostore.gs_security as gss join geostore.gs_user as gsu on (gss.user_id = gsu.id)
where gss.user_id IS NOT NULL) as subquery
where gsr.id = subquery.resource_id and gsr.creator IS NULL;
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,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, columnDefinition = "bool default true")
private Boolean advertised = true;

Expand Down Expand Up @@ -274,6 +280,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 @@ -308,17 +342,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);
}

if (advertised != null) {
Expand Down Expand Up @@ -348,6 +392,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());
result = (prime * result) + ((advertised == null) ? 0 : advertised.hashCode());

return result;
Expand Down Expand Up @@ -440,12 +486,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 @@ -464,7 +518,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 @@ -50,7 +50,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 @@ -272,4 +298,11 @@ 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
Loading

0 comments on commit b3be458

Please sign in to comment.