diff --git a/doc/sql/002_create_schema_oracle.sql b/doc/sql/002_create_schema_oracle.sql index 55d420f6..1da10b71 100644 --- a/doc/sql/002_create_schema_oracle.sql +++ b/doc/sql/002_create_schema_oracle.sql @@ -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) diff --git a/doc/sql/002_create_schema_postgres.sql b/doc/sql/002_create_schema_postgres.sql index a6244592..ebc0cd4c 100644 --- a/doc/sql/002_create_schema_postgres.sql +++ b/doc/sql/002_create_schema_postgres.sql @@ -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) diff --git a/doc/sql/migration/h2/h2-migration-from-v.1.5.0-to-v2.0.0.sql b/doc/sql/migration/h2/h2-migration-from-v.1.5.0-to-v2.0.0.sql index 37831c4f..35cbd7b6 100644 --- a/doc/sql/migration/h2/h2-migration-from-v.1.5.0-to-v2.0.0.sql +++ b/doc/sql/migration/h2/h2-migration-from-v.1.5.0-to-v2.0.0.sql @@ -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; \ No newline at end of file +alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup; diff --git a/doc/sql/migration/h2/h2-migration-from-v.2.0.0-to-v2.1.0.sql b/doc/sql/migration/h2/h2-migration-from-v.2.0.0-to-v2.1.0.sql index 441f3836..c87d69ca 100644 --- a/doc/sql/migration/h2/h2-migration-from-v.2.0.0-to-v2.1.0.sql +++ b/doc/sql/migration/h2/h2-migration-from-v.2.0.0-to-v2.1.0.sql @@ -1 +1,11 @@ -alter table gs_resource add column advertised bool not null default true; \ No newline at end of file +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; \ No newline at end of file diff --git a/doc/sql/migration/oracle/oracle-migration-from-v.1.5.0-to-v2.0.0.sql b/doc/sql/migration/oracle/oracle-migration-from-v.1.5.0-to-v2.0.0.sql index 525aa6cc..7a40bc89 100644 --- a/doc/sql/migration/oracle/oracle-migration-from-v.1.5.0-to-v2.0.0.sql +++ b/doc/sql/migration/oracle/oracle-migration-from-v.1.5.0-to-v2.0.0.sql @@ -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; \ No newline at end of file +alter table gs_user_group_attribute add constraint fk_ugattrib_user_group foreign key (userGroup_id) references gs_usergroup; diff --git a/doc/sql/migration/oracle/oracle-migration-from-v.2.0.0-to-v2.1.0.sql b/doc/sql/migration/oracle/oracle-migration-from-v.2.0.0-to-v2.1.0.sql index 441f3836..f1ec4c0a 100644 --- a/doc/sql/migration/oracle/oracle-migration-from-v.2.0.0-to-v2.1.0.sql +++ b/doc/sql/migration/oracle/oracle-migration-from-v.2.0.0-to-v2.1.0.sql @@ -1 +1,11 @@ -alter table gs_resource add column advertised bool not null default true; \ No newline at end of file +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; \ No newline at end of file diff --git a/doc/sql/migration/postgresql/postgresql-migration-from-v.2.0.0-to-v2.1.0.sql b/doc/sql/migration/postgresql/postgresql-migration-from-v.2.0.0-to-v2.1.0.sql index 441f3836..c87d69ca 100644 --- a/doc/sql/migration/postgresql/postgresql-migration-from-v.2.0.0-to-v2.1.0.sql +++ b/doc/sql/migration/postgresql/postgresql-migration-from-v.2.0.0-to-v2.1.0.sql @@ -1 +1,11 @@ -alter table gs_resource add column advertised bool not null default true; \ No newline at end of file +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; \ No newline at end of file diff --git a/src/core/model/src/main/java/it/geosolutions/geostore/core/model/Resource.java b/src/core/model/src/main/java/it/geosolutions/geostore/core/model/Resource.java index 1df24f24..3cbea3a9 100644 --- a/src/core/model/src/main/java/it/geosolutions/geostore/core/model/Resource.java +++ b/src/core/model/src/main/java/it/geosolutions/geostore/core/model/Resource.java @@ -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; @@ -274,6 +280,34 @@ public void setSecurity(List 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() */ @@ -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) { @@ -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; @@ -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; } @@ -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; } + } diff --git a/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/SecurityDAO.java b/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/SecurityDAO.java index a55cf3ab..807594ec 100644 --- a/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/SecurityDAO.java +++ b/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/SecurityDAO.java @@ -50,7 +50,7 @@ public interface SecurityDAO extends RestrictedGenericDAO { /** * - * @param userName + * @param groupNames * @param resourceId * @return */ diff --git a/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/impl/SecurityDAOImpl.java b/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/impl/SecurityDAOImpl.java index 4b68aae2..1b49b00c 100644 --- a/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/impl/SecurityDAOImpl.java +++ b/src/core/persistence/src/main/java/it/geosolutions/geostore/core/dao/impl/SecurityDAOImpl.java @@ -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; @@ -48,6 +50,9 @@ public class SecurityDAOImpl extends BaseDAO implements Secu private static final Logger LOGGER = LogManager.getLogger(SecurityDAOImpl.class); private UserGroupDAO userGroupDAO; + + private ResourceDAO resourceDAO; + /* * (non-Javadoc) * @@ -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()); @@ -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) * @@ -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; + } } diff --git a/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/ResourceDAOTest.java b/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/ResourceDAOTest.java index 5c68e548..aa98ef7f 100644 --- a/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/ResourceDAOTest.java +++ b/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/ResourceDAOTest.java @@ -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(); @@ -103,7 +105,11 @@ 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); } @@ -111,6 +117,8 @@ public void testPersistResource() throws Exception { Resource loaded = resourceDAO.find(resourceId); assertNotNull("Can't retrieve resource", loaded); assertEquals(NAME2, loaded.getName()); + assertEquals("USER1Updated", loaded.getCreator()); + assertEquals("USER2Updated", loaded.getEditor()); } // diff --git a/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/SecurityDAOTest.java b/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/SecurityDAOTest.java index bd949957..37eb3220 100644 --- a/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/SecurityDAOTest.java +++ b/src/core/persistence/src/test/java/it/geosolutions/geostore/core/dao/SecurityDAOTest.java @@ -46,7 +46,6 @@ public class SecurityDAOTest extends BaseDAOTest { */ @Test public void testPersistSecurity() throws Exception { - final String NAME = "NAME"; if (LOGGER.isDebugEnabled()) { @@ -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); @@ -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()); } // @@ -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"; @@ -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()); } // @@ -219,6 +230,13 @@ 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); } @@ -226,11 +244,16 @@ public void testPersistSecurityUsingNames() throws Exception { // 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); @@ -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); } diff --git a/src/core/services-api/src/main/java/it/geosolutions/geostore/services/dto/ShortResource.java b/src/core/services-api/src/main/java/it/geosolutions/geostore/services/dto/ShortResource.java index 7e916641..715868dd 100644 --- a/src/core/services-api/src/main/java/it/geosolutions/geostore/services/dto/ShortResource.java +++ b/src/core/services-api/src/main/java/it/geosolutions/geostore/services/dto/ShortResource.java @@ -60,6 +60,10 @@ public class ShortResource implements Serializable { private boolean canDelete = false; + private String creator; + + private String editor; + private boolean advertised = true; public ShortResource() { @@ -75,6 +79,8 @@ public ShortResource(Resource resource) { this.creation = resource.getCreation(); this.description = resource.getDescription(); this.lastUpdate = resource.getLastUpdate(); + this.creator = resource.getCreator(); + this.editor = resource.getEditor(); this.advertised = resource.isAdvertised(); } @@ -114,7 +120,7 @@ public String getDescription() { } /** - * @param descrition the description to set + * @param description the description to set */ public void setDescription(String description) { this.description = description; @@ -176,6 +182,34 @@ public void setCanDelete(boolean canDelete) { this.canDelete = canDelete; } + /** + * @return + */ + public String getCreator() { + return creator; + } + + /** + * @param creator + */ + public void setCreator(String creator) { + this.creator = creator; + } + + /** + * @return + */ + public String getEditor() { + return editor; + } + + /** + * @param editor + */ + public void setEditor(String editor) { + this.editor = editor; + } + /** * @return the advertised */ @@ -213,10 +247,16 @@ public String toString() { builder.append("lastUpdate=").append(lastUpdate).append(", "); if (canEdit) - builder.append("canEdit=").append(canEdit).append(", "); + builder.append("canEdit=").append(true).append(", "); if (canDelete) - builder.append("canDelete=").append(canDelete); + builder.append("canDelete=").append(true); + + if (creator != null) + builder.append("creator=").append(creator).append(", "); + + if (editor != null) + builder.append("editor=").append(editor).append(", "); if (advertised) builder.append("advertised=").append(advertised); diff --git a/src/core/services-impl/src/main/java/it/geosolutions/geostore/services/ResourceServiceImpl.java b/src/core/services-impl/src/main/java/it/geosolutions/geostore/services/ResourceServiceImpl.java index 30f9c373..ce650ef2 100644 --- a/src/core/services-impl/src/main/java/it/geosolutions/geostore/services/ResourceServiceImpl.java +++ b/src/core/services-impl/src/main/java/it/geosolutions/geostore/services/ResourceServiceImpl.java @@ -189,6 +189,8 @@ public long insert(Resource resource) throws BadRequestServiceEx, NotFoundServic r.setMetadata(resource.getMetadata()); r.setName(resource.getName()); r.setCategory(loadedCategory); + r.setCreator(resource.getCreator()); + r.setEditor(resource.getEditor()); r.setAdvertised(resource.isAdvertised()); try { @@ -797,6 +799,8 @@ private List configResourceList(List list, boolean includeAt res.setId(resource.getId()); res.setLastUpdate(resource.getLastUpdate()); res.setName(resource.getName()); + res.setCreator(resource.getCreator()); + res.setEditor(resource.getEditor()); if (includeData) { res.setData(resource.getData()); diff --git a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ResourceServiceImplTest.java b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ResourceServiceImplTest.java index 5a624479..7b73f63c 100644 --- a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ResourceServiceImplTest.java +++ b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ResourceServiceImplTest.java @@ -60,7 +60,6 @@ public ResourceServiceImplTest() { @Test public void testInsertDeleteResource() throws Exception { - long resourceId = createResource("name1", "description1", "MAP"); assertEquals(1, resourceService.getCount(null)); @@ -174,7 +173,6 @@ public void testSorting() throws Exception { // 4 resources contain 1 in the name: "FIRST SET - 2" + "FIRST SET - 12" assertEquals(4, nameContain2Result.size()); assertTrue(isSorted(nameContain2Result)); - } /** @@ -283,7 +281,6 @@ public void testGetSecurityRules() throws Exception { assertNull(groupRule.getUser()); assertEquals((Long)groupId, groupRule.getGroup().getId()); assertEquals((Long)resourceId, groupRule.getResource().getId()); - } @Test @@ -450,6 +447,31 @@ public void testInsertUpdateDuplicatedResource() throws Exception { assertEquals(0, resourceService.getCount(null)); } + @Test + public void testInsertUpdateCreatorAndEditor() throws Exception { + final String ORIG_RES_NAME = "testRes"; + final String DESCRIPTION = "description"; + final String CATEGORY_NAME = "MAP"; + + long origResourceId = createResource(ORIG_RES_NAME, DESCRIPTION, CATEGORY_NAME); + Category category = categoryService.get(CATEGORY_NAME); + + assertEquals(1, resourceService.getCount(null)); + assertNotNull(category); + + Resource resource = resourceService.get(origResourceId); + assertEquals("USER1", resource.getCreator()); + assertEquals("USER2", resource.getEditor()); + + resource.setCreator("USER1Updated"); + resource.setEditor("USER1Updated"); + resourceService.update(resource); + + resource = resourceService.get(origResourceId); + assertEquals("USER1Updated", resource.getCreator()); + assertEquals("USER1Updated", resource.getEditor()); + } + @Test public void testUnadvertisedResources() throws Exception { long groupId = createGroup("group1"); diff --git a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/SearchConverterTest.java b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/SearchConverterTest.java index 0fe01827..7108b19f 100644 --- a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/SearchConverterTest.java +++ b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/SearchConverterTest.java @@ -102,23 +102,14 @@ public void testFilterConverter() throws Exception { assertEquals(3, sAttributes.size()); assertNotNull(resourceService.get(resourceId)); - assertTrue(resourceService.getAttributes(resourceId).size() == 3); + assertEquals(3, resourceService.getAttributes(resourceId).size()); } // // Complex filter with AND // { - String xmlFilter = "" + "" + "NAME" - + "LIKE" + "%resource%" + "" - + "" + "" + "attr1" - + "EQUAL_TO" + "STRING" - + "value2" + "" + "" - + "attr2" + "GREATER_THAN" - + "NUMBER" + "1.0" + "" + "" - + ""; - - StringReader reader = new StringReader(xmlFilter); + StringReader reader = getFilterConverterFilterAND(); AndFilter searchFilter = JAXB.unmarshal(reader, AndFilter.class); assertNotNull(searchFilter); @@ -130,18 +121,7 @@ public void testFilterConverter() throws Exception { // Complex filter with AND OR // { - String xmlFilter = "" + "" + "NAME" - + "LIKE" + "%resource%" + "" - + "" + "" + "attr2" - + "GREATER_THAN" + "NUMBER" - + "1.0" + "" + "" + "" - + "attr1" + "EQUAL_TO" - + "STRING" + "value2" + "" - + "" + "attr1" + "EQUAL_TO" - + "STRING" + "value3" + "" + "" - + "" + ""; - - StringReader reader = new StringReader(xmlFilter); + StringReader reader = getFilterConverterFilterANDOR(); AndFilter searchFilter = JAXB.unmarshal(reader, AndFilter.class); assertNotNull(searchFilter); @@ -150,6 +130,34 @@ public void testFilterConverter() throws Exception { } } + private static StringReader getFilterConverterFilterAND() { + String xmlFilter = "" + "" + "NAME" + + "LIKE" + "%resource%" + "" + + "" + "" + "attr1" + + "EQUAL_TO" + "STRING" + + "value2" + "" + "" + + "attr2" + "GREATER_THAN" + + "NUMBER" + "1.0" + "" + "" + + ""; + + return new StringReader(xmlFilter); + } + + private static StringReader getFilterConverterFilterANDOR() { + String xmlFilter = "" + "" + "NAME" + + "LIKE" + "%resource%" + "" + + "" + "" + "attr2" + + "GREATER_THAN" + "NUMBER" + + "1.0" + "" + "" + "" + + "attr1" + "EQUAL_TO" + + "STRING" + "value2" + "" + + "" + "attr1" + "EQUAL_TO" + + "STRING" + "value3" + "" + "" + + "" + ""; + + return new StringReader(xmlFilter); + } + @Test public void testSearch() throws Exception { // @@ -177,7 +185,7 @@ public void testSearch() throws Exception { Attribute attr2 = new Attribute(); attr2.setName("attr2"); - attr2.setNumberValue(Double.valueOf(i)); + attr2.setNumberValue((double) i); attr2.setType(DataType.NUMBER); attributes.add(attr2); @@ -193,10 +201,10 @@ public void testSearch() throws Exception { List sAttributes = resourceService.getAttributes(resourceId); assertNotNull(sAttributes); - assertTrue(sAttributes.size() == 3); + assertEquals(3, sAttributes.size()); assertNotNull(resourceService.get(resourceId)); - assertTrue(resourceService.getAttributes(resourceId).size() == 3); + assertEquals(3, resourceService.getAttributes(resourceId).size()); long id = createData("data" + i, resourceService.get(resourceId)); @@ -224,7 +232,7 @@ public void testSearch() throws Exception { Resource res = resources.get(0); assertNotNull(res.getAttribute()); - assertTrue(res.getAttribute().size() == 3); + assertEquals(3, res.getAttribute().size()); assertNull(res.getData()); } @@ -272,7 +280,7 @@ public void testSearch() throws Exception { assertNotNull(res.getData()); assertNotNull(res.getAttribute()); - assertTrue(res.getAttribute().size() == 3); + assertEquals(3, res.getAttribute().size()); } } } diff --git a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ServiceTestBase.java b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ServiceTestBase.java index 0b0fb943..a9f972a1 100644 --- a/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ServiceTestBase.java +++ b/src/core/services-impl/src/test/java/it/geosolutions/geostore/services/ServiceTestBase.java @@ -216,7 +216,6 @@ protected long createData(String data, Resource resource) throws Exception { * @throws Exception */ protected long createResource(String name, String description, String catName) throws Exception { - Category category = new Category(); category.setName(catName); @@ -226,6 +225,8 @@ protected long createResource(String name, String description, String catName) t resource.setName(name); resource.setDescription(description); resource.setCategory(category); + resource.setCreator("USER1"); + resource.setEditor("USER2"); return resourceService.insert(resource); } @@ -239,7 +240,6 @@ protected long createResource(String name, String description, String catName) t * @throws Exception */ protected long createResource(String name, String description, String catName, String data) throws Exception { - Category category = new Category(); category.setName(catName); @@ -252,6 +252,8 @@ protected long createResource(String name, String description, String catName, S StoredData storedData = new StoredData(); storedData.setData(data); resource.setData(storedData); + resource.setCreator("USER1"); + resource.setEditor("USER2"); return resourceService.insert(resource); } @@ -286,7 +288,7 @@ protected long createResource(String name, String description, String catName, L * @param catName * @param advertised * @param rules - * @return + * @return long * @throws Exception */ protected long createResource(String name, String description, String catName, boolean advertised, List rules) throws Exception { @@ -301,6 +303,8 @@ protected long createResource(String name, String description, String catName, b resource.setCategory(category); resource.setAdvertised(advertised); resource.setSecurity(rules); + resource.setCreator("USER1"); + resource.setEditor("USER2"); return resourceService.insert(resource); } @@ -315,11 +319,12 @@ protected long createResource(String name, String description, String catName, b */ protected long createResource(String name, String description, Category category) throws Exception { - Resource resource = new Resource(); resource.setName(name); resource.setDescription(description); resource.setCategory(category); + resource.setCreator("USER1"); + resource.setEditor("USER2"); return resourceService.insert(resource); } diff --git a/src/modules/rest/api/src/main/java/it/geosolutions/geostore/services/rest/model/RESTResource.java b/src/modules/rest/api/src/main/java/it/geosolutions/geostore/services/rest/model/RESTResource.java index ddf045ac..b55a23e6 100644 --- a/src/modules/rest/api/src/main/java/it/geosolutions/geostore/services/rest/model/RESTResource.java +++ b/src/modules/rest/api/src/main/java/it/geosolutions/geostore/services/rest/model/RESTResource.java @@ -62,6 +62,10 @@ public class RESTResource implements Serializable { private String metadata; + private String creator; + + private String editor; + private List attribute; private RESTStoredData store; @@ -212,6 +216,34 @@ public void setCategory(RESTCategory category) { this.category = category; } + /** + * @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; + } + /** * @param advertised weather the resource is advertised or not */ @@ -262,8 +294,13 @@ public String toString() { if (category != null) builder.append(", cat=").append(category); - builder.append(", advertised=").append(advertised); + if (creator != null) + builder.append(", creator=").append(creator); + if (editor != null) + builder.append(", editor=").append(editor); + + builder.append(", advertised=").append(advertised); builder.append(']'); return builder.toString(); } diff --git a/src/modules/rest/api/src/test/java/it/geosolutions/geostore/services/rest/utils/FilterUnmarshalTest.java b/src/modules/rest/api/src/test/java/it/geosolutions/geostore/services/rest/utils/FilterUnmarshalTest.java index 32cdc582..eb837c27 100644 --- a/src/modules/rest/api/src/test/java/it/geosolutions/geostore/services/rest/utils/FilterUnmarshalTest.java +++ b/src/modules/rest/api/src/test/java/it/geosolutions/geostore/services/rest/utils/FilterUnmarshalTest.java @@ -103,6 +103,8 @@ public void testPrintRESTResource() throws JDOMException, IOException { rr.setCategory(new RESTCategory("TestCategory")); rr.setData("Sample data content"); rr.setMetadata("Sample metadata content"); + rr.setCreator("User1"); + rr.setEditor("User2"); List attr = new ArrayList(); attr.add(new ShortAttribute("attname1", "attvalue1", DataType.STRING)); diff --git a/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/AdministratorGeostoreClientTest.java b/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/AdministratorGeostoreClientTest.java index 1f02f50b..b5f58f00 100644 --- a/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/AdministratorGeostoreClientTest.java +++ b/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/AdministratorGeostoreClientTest.java @@ -954,8 +954,6 @@ protected ShortResource createAResource(){ String timeid = Long.toString(System.currentTimeMillis()); - - RESTResource origResource = new RESTResource(); origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME)); origResource.setName("rest_test_resource_" + timeid); diff --git a/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/GeoStoreClientTest.java b/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/GeoStoreClientTest.java index 1d6a9be2..f3782f1f 100644 --- a/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/GeoStoreClientTest.java +++ b/src/modules/rest/client/src/test/java/it/geosolutions/geostore/services/rest/GeoStoreClientTest.java @@ -163,6 +163,8 @@ public void testInsertResource() { origResource.setName("rest_test_resource_" + timeid); origResource.setStore(storedData); origResource.setAttribute(attrList); + origResource.setCreator("USER1"); + origResource.setEditor("USER2"); Long rid = client.insert(origResource); System.out.println("RESOURCE has ID " + rid); @@ -233,6 +235,8 @@ public void testUpdateResource() { origResource.setName("rest_test_resource_" + timeid); origResource.setStore(storedData); origResource.setAttribute(attrList); + origResource.setCreator("USER1"); + origResource.setEditor("USER2"); rid = client.insert(origResource); } @@ -251,6 +255,8 @@ public void testUpdateResource() { attrList.add(new ShortAttribute("string4", "value4", DataType.STRING)); // added updResource.setAttribute(attrList); + updResource.setCreator("USER1Updated"); + updResource.setEditor("USER2Updated"); client.updateResource(rid, updResource); } @@ -270,6 +276,9 @@ public void testUpdateResource() { assertEquals("value1", attMap.get("string1")); assertEquals("value2.2", attMap.get("string2")); assertEquals("value4", attMap.get("string4")); + + assertEquals("USER1Updated", loaded.getCreator()); + assertEquals("USER2Updated", loaded.getEditor()); } // try bad update @@ -289,7 +298,6 @@ public void testUpdateResource() { LOGGER.info("Error condition successfully detected: " + response); } catch (Exception e) { LOGGER.info("Error condition successfully detected:" + e.getMessage(), e); - } } @@ -360,6 +368,8 @@ public void testGetResourceFull() { origResource.setCategory(new RESTCategory(DEFAULTCATEGORYNAME)); origResource.setName("rest_test_resource_getFull"); origResource.setStore(storedData); + origResource.setCreator("USER1"); + origResource.setEditor("USER2"); Long rid = client.insert(origResource); System.out.println("RESOURCE has ID " + rid); diff --git a/src/modules/rest/extjs/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTExtJsServiceImpl.java b/src/modules/rest/extjs/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTExtJsServiceImpl.java index 6efa81e9..aa41fd56 100644 --- a/src/modules/rest/extjs/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTExtJsServiceImpl.java +++ b/src/modules/rest/extjs/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTExtJsServiceImpl.java @@ -530,6 +530,16 @@ private JSONObject makeGeneralizedJSONResult(boolean success, long count, List(), "p0"); + long u0 = restCreateUser("u0", Role.USER, new HashSet<>(), "p0"); + + createCategory(CAT0_NAME); + long r0Id = restCreateResource(RES_NAME, "x", CAT0_NAME, u0, true); + + { + SecurityContext sc = new SimpleSecurityContext(u0); + String response = restExtJsService.getAllResources(sc, "*mIxEd*", 0, 1000); + + System.out.println("JSON " + response); + + JSONResult result = parse(response); + assertEquals(1, result.total); + assertEquals(1, result.returnedCount); + + ShortResource resource = restExtJsService.getResource(sc, r0Id); + assertEquals(RES_NAME, resource.getName()); + assertEquals("u0", resource.getCreator()); + assertEquals("u0", resource.getEditor()); + } + + { + SecurityContext sc = new SimpleSecurityContext(a0); + Resource realResource = resourceService.get(r0Id); + realResource.setName("new name"); + restResourceService.update(sc, r0Id, createRESTResource(realResource)); + + ShortResource resource = restExtJsService.getResource(sc, r0Id); + assertEquals(realResource.getName(), resource.getName()); + assertEquals("u0", resource.getCreator()); + assertEquals("a0", resource.getEditor()); + } + } + @Test public void testGetAllResources_unadvertised() throws Exception { @@ -320,7 +364,7 @@ public void testGetAllResources_unadvertised() throws Exception assertEquals(2, result.returnedCount); } } - + private JSONResult parse(String jsonString) { JSONResult ret = new JSONResult(); @@ -371,6 +415,4 @@ static class JSONResult { int returnedCount; Set names; } - - } diff --git a/src/modules/rest/extjs/src/test/java/it/geosolutions/geostore/services/rest/impl/ServiceTestBase.java b/src/modules/rest/extjs/src/test/java/it/geosolutions/geostore/services/rest/impl/ServiceTestBase.java index 447ff221..ca648085 100644 --- a/src/modules/rest/extjs/src/test/java/it/geosolutions/geostore/services/rest/impl/ServiceTestBase.java +++ b/src/modules/rest/extjs/src/test/java/it/geosolutions/geostore/services/rest/impl/ServiceTestBase.java @@ -48,7 +48,8 @@ import java.util.Set; import javax.ws.rs.core.SecurityContext; - +import it.geosolutions.geostore.services.rest.utils.Convert; +import org.apache.commons.collections.CollectionUtils; import it.geosolutions.geostore.services.rest.model.SecurityRuleList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -246,13 +247,13 @@ protected long createData(String data, Resource resource) throws Exception { * @param name * @param description * @param catName + * @return long + * @throws Exception * @param advertised - * * @return long * @throws Exception */ protected long createResource(String name, String description, String catName, boolean advertised) throws Exception { - Category category = new Category(); category.setName(catName); @@ -262,13 +263,14 @@ protected long createResource(String name, String description, String catName, b resource.setName(name); resource.setDescription(description); resource.setCategory(category); + resource.setCreator("USER1"); + resource.setEditor("USER2"); resource.setAdvertised(advertised); return resourceService.insert(resource); } protected long restCreateResource(String name, String description, String catName, long userId, boolean advertised) throws Exception { - RESTResource resource = new RESTResource(); resource.setName(name); resource.setDescription(description); @@ -290,11 +292,12 @@ protected long restCreateResource(String name, String description, String catNam } protected long createResource(String name, String description, Category category, boolean advertised) throws Exception { - Resource resource = new Resource(); resource.setName(name); resource.setDescription(description); resource.setCategory(category); + resource.setCreator("USER1"); + resource.setEditor("USER2"); resource.setAdvertised(advertised); return resourceService.insert(resource); @@ -306,7 +309,6 @@ protected long createResource(String name, String description, Category category * @param catName * @param rules * @param advertised - * * @return long * @throws Exception */ @@ -322,6 +324,8 @@ protected long createResource(String name, String description, String catName, L resource.setDescription(description); resource.setCategory(category); resource.setSecurity(rules); + resource.setCreator("USER1"); + resource.setEditor("USER2"); resource.setAdvertised(advertised); return resourceService.insert(resource); @@ -409,6 +413,21 @@ protected long createGroup(String name) throws Exception { return userGroupService.insert(group); } + protected RESTResource createRESTResource(Resource resource) { + RESTResource ret = new RESTResource(); + ret.setCategory(new RESTCategory(resource.getCategory().getName())); + ret.setName(resource.getName()); + ret.setDescription(resource.getDescription()); + ret.setMetadata(resource.getMetadata()); + ret.setCreator(resource.getCreator()); + ret.setEditor(resource.getEditor()); + if (resource.getData() != null) + ret.setData(resource.getData().getData()); + if (CollectionUtils.isNotEmpty(resource.getAttribute())) + ret.setAttribute(Convert.convertToShortAttributeList(resource.getAttribute())); + return ret; + } + class SimpleSecurityContext implements SecurityContext { private Principal userPrincipal; diff --git a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTBackupServiceImpl.java b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTBackupServiceImpl.java index 14a761bc..54e6e60f 100644 --- a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTBackupServiceImpl.java +++ b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTBackupServiceImpl.java @@ -198,7 +198,10 @@ private RESTResource createRESTResource(Resource resource) { ret.setName(resource.getName()); ret.setDescription(resource.getDescription()); ret.setMetadata(resource.getMetadata()); + ret.setCreator(resource.getCreator()); + ret.setEditor(resource.getEditor()); ret.setAdvertised(resource.isAdvertised()); + if (resource.getData() != null) ret.setData(resource.getData().getData()); if (CollectionUtils.isNotEmpty(resource.getAttribute())) diff --git a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTResourceServiceImpl.java b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTResourceServiceImpl.java index 3d9d41b6..a52d51d8 100644 --- a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTResourceServiceImpl.java +++ b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/impl/RESTResourceServiceImpl.java @@ -28,11 +28,7 @@ package it.geosolutions.geostore.services.rest.impl; -import it.geosolutions.geostore.core.model.Attribute; -import it.geosolutions.geostore.core.model.Category; -import it.geosolutions.geostore.core.model.Resource; -import it.geosolutions.geostore.core.model.SecurityRule; -import it.geosolutions.geostore.core.model.User; +import it.geosolutions.geostore.core.model.*; import it.geosolutions.geostore.core.model.enums.DataType; import it.geosolutions.geostore.core.model.enums.Role; import it.geosolutions.geostore.services.ResourceService; @@ -47,33 +43,20 @@ import it.geosolutions.geostore.services.exception.InternalErrorServiceEx; import it.geosolutions.geostore.services.exception.NotFoundServiceEx; import it.geosolutions.geostore.services.rest.RESTResourceService; -import it.geosolutions.geostore.services.rest.exception.BadRequestWebEx; -import it.geosolutions.geostore.services.rest.exception.ConflictWebEx; -import it.geosolutions.geostore.services.rest.exception.ForbiddenErrorWebEx; -import it.geosolutions.geostore.services.rest.exception.InternalErrorWebEx; -import it.geosolutions.geostore.services.rest.exception.NotFoundWebEx; -import it.geosolutions.geostore.services.rest.model.RESTAttribute; -import it.geosolutions.geostore.services.rest.model.RESTCategory; -import it.geosolutions.geostore.services.rest.model.RESTResource; -import it.geosolutions.geostore.services.rest.model.ResourceList; -import it.geosolutions.geostore.services.rest.model.SecurityRuleList; -import it.geosolutions.geostore.services.rest.model.ShortAttributeList; -import it.geosolutions.geostore.services.rest.model.ShortResourceList; +import it.geosolutions.geostore.services.rest.exception.*; +import it.geosolutions.geostore.services.rest.model.*; import it.geosolutions.geostore.services.rest.utils.Convert; - -import java.util.ArrayList; -import java.util.List; - -import javax.ws.rs.core.SecurityContext; - import org.apache.logging.log4j.Level; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import javax.ws.rs.core.SecurityContext; +import java.util.ArrayList; +import java.util.List; + /** * Class RESTResourceServiceImpl. - * + * * @author ETj (etj at geo-solutions.it) * @author Tobia di Pisa (tobia.dipisa at geo-solutions.it) * @author DamianoG @@ -83,7 +66,7 @@ public class RESTResourceServiceImpl extends RESTServiceImpl implements RESTReso private final static Logger LOGGER = LogManager.getLogger(RESTResourceServiceImpl.class); private ResourceService resourceService; - + /** * @param resourceService */ @@ -101,7 +84,7 @@ protected SecurityService getSecurityService() { /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#insert(it.geosolutions.geostore.core.model.Resource) */ @Override @@ -146,9 +129,9 @@ public long insert(SecurityContext sc, RESTResource resource) { * Updates a resource. Name, Description and Metadata will be replaced if not null.
* Category can not be changed; category element may exist in the input resource provided it is the same as in the original resource.
* Attribute list will be updated only if it exists in the input resource.
- *

+ *

* TODO: attribute list behaviour should be checked: read comments in source. - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#update(long, it.geosolutions.geostore.core.model.Resource) */ @Override @@ -189,6 +172,9 @@ public long update(SecurityContext sc, long id, RESTResource resource) throws No old.setName(resource.getName()); if (resource.getMetadata() != null) old.setMetadata(resource.getMetadata()); + if (resource.getCreator() != null) + old.setCreator(resource.getCreator()); + old.setEditor(authUser.getName()); old.setAdvertised(resource.isAdvertised()); try { @@ -226,7 +212,7 @@ public long update(SecurityContext sc, long id, RESTResource resource) throws No /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#delete(long) */ @Override @@ -248,7 +234,7 @@ public void delete(SecurityContext sc, long id) throws NotFoundWebEx { /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#delete(long) */ @Override @@ -276,10 +262,10 @@ public Resource get(SecurityContext sc, long id, boolean fullResource) throws No boolean canRead = false; User authUser = extractAuthUser(sc); canRead = resourceAccessRead(authUser, id); - if(!canRead){ + if (!canRead) { throw new ForbiddenErrorWebEx("This user cannot read this resource !"); } - + if (fullResource) { if (LOGGER.isDebugEnabled()) LOGGER.debug("Retrieving a full resource"); @@ -318,12 +304,12 @@ public Resource get(SecurityContext sc, long id, boolean fullResource) throws No /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getList(java.lang.String, java.lang.Integer, java.lang.Integer) */ @Override public ShortResourceList getList(SecurityContext sc, String nameLike, Integer page, - Integer entries) throws BadRequestWebEx { + Integer entries) throws BadRequestWebEx { User authUser = extractAuthUser(sc); nameLike = nameLike.replaceAll("[*]", "%"); @@ -336,7 +322,7 @@ public ShortResourceList getList(SecurityContext sc, String nameLike, Integer pa /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getList(java.lang.Integer, java.lang.Integer) */ @Override @@ -353,7 +339,7 @@ public ShortResourceList getAll(SecurityContext sc, Integer page, Integer entrie /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getCount(java.lang.String) */ @Override @@ -379,7 +365,7 @@ private long parseId(SecurityContext sc, String id) throws BadRequestWebEx { /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getAttributes(long) */ @Override @@ -406,7 +392,7 @@ public ShortAttributeList getAttributes(SecurityContext sc, long id) throws NotF /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getAttribute(long, java.lang.String) */ @Override @@ -424,20 +410,20 @@ public String getAttribute(SecurityContext sc, long id, String name) throws NotF if (canRead) { ShortAttribute shAttribute = resourceService.getAttribute(id, name); - + if (shAttribute == null) throw new NotFoundWebEx("Resource attribute not found"); - + return shAttribute.getValue(); } else { throw new ForbiddenErrorWebEx( "This user cannot read this resource so neither its attributes!"); - } + } } /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#insertAttribute(long, java.lang.String, java.lang.String,it.geosolutions.geostore.core.model.enums.DataType) */ @Override @@ -450,57 +436,59 @@ public long updateAttribute(SecurityContext sc, long id, String name, String val // // Authorization check. // + long attributeId; boolean canUpdate = false; try { User authUser = extractAuthUser(sc); canUpdate = resourceAccessWrite(authUser, resource.getId()); - if (canUpdate){ - - ShortAttribute a = resourceService.getAttribute(id, name); - //if the attribute exists, will be updated - if(a!=null){ - return resourceService.updateAttribute(id, name, value); - }else{ - //create the attribute if missing - if(type != null){ - return resourceService.insertAttribute(id, name, value, type); - }else{ - return resourceService.insertAttribute(id, name, value, DataType.STRING); - } - - } - + if (canUpdate) { + ShortAttribute a = resourceService.getAttribute(id, name); + //if the attribute exists, will be updated + if (a != null) { + attributeId = resourceService.updateAttribute(id, name, value); + } else { + //create the attribute if missing + if (type != null) { + attributeId = resourceService.insertAttribute(id, name, value, type); + } else { + attributeId = resourceService.insertAttribute(id, name, value, DataType.STRING); + } + } } else { throw new InternalErrorServiceEx("This user cannot access this resource !"); } - } catch (InternalErrorServiceEx ex) { - + resource.setEditor(authUser.getName()); + resourceService.update(resource); + } catch (InternalErrorServiceEx ex) { throw new InternalErrorWebEx(ex.getMessage()); + } catch (DuplicatedResourceNameServiceEx | NotFoundServiceEx e) { + throw new RuntimeException(e); } + return attributeId; } - + /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#insertAttribute(long, java.lang.String, java.lang.String) */ - public long updateAttribute(SecurityContext sc, long id,String name, String value) { - return updateAttribute(sc, id, name, value, null); - } + public long updateAttribute(SecurityContext sc, long id, String name, String value) { + return updateAttribute(sc, id, name, value, null); + } @Override - public long updateAttribute(SecurityContext sc, long id, RESTAttribute content) { - if (content != null && content.getName() != null) { - // TODO: type - return updateAttribute(sc, id, content.getName(), content.getValue(), content.getType()); - } - throw new BadRequestWebEx("missing attribute content or attribute name in request"); - } + public long updateAttribute(SecurityContext sc, long id, RESTAttribute content) { + if (content != null && content.getName() != null) { + // TODO: type + return updateAttribute(sc, id, content.getName(), content.getValue(), content.getType()); + } + throw new BadRequestWebEx("missing attribute content or attribute name in request"); + } /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getResourceByFilter(it.geosolutions.geostore.services.dto.SearchFilter) */ @Override @@ -522,13 +510,13 @@ public ShortResourceList getResources(SecurityContext sc, SearchFilter filter) { /* * (non-Javadoc) - * + * * @see it.geosolutions.geostore.services.rest.RESTResourceService#getResourcesList(javax.ws.rs.core.SecurityContext, * it.geosolutions.geostore.services.dto.search.SearchFilter, java.lang.Integer, java.lang.Integer, boolean, boolean) */ @Override public ResourceList getResourcesList(SecurityContext sc, Integer page, Integer entries, - boolean includeAttributes, boolean includeData, SearchFilter filter) { + boolean includeAttributes, boolean includeData, SearchFilter filter) { User authUser = extractAuthUser(sc); try { return new ResourceList(resourceService.getResources(filter, page, entries, includeAttributes, includeData, authUser)); @@ -543,10 +531,10 @@ public ResourceList getResourcesList(SecurityContext sc, Integer page, Integer e } } - @Override - public void updateSecurityRules(SecurityContext sc, long id, - SecurityRuleList securityRules) { - // + @Override + public void updateSecurityRules(SecurityContext sc, long id, + SecurityRuleList securityRules) { + // // Authorization check. // boolean canWrite = false; @@ -554,37 +542,37 @@ public void updateSecurityRules(SecurityContext sc, long id, canWrite = resourceAccessWrite(authUser, id); if (canWrite) { - ShortAttribute owner = resourceService.getAttribute(id, "owner"); - if((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { - try { - resourceService.updateSecurityRules(id, Convert - .convertSecurityRuleList(securityRules.getList(), id)); - } catch (BadRequestServiceEx e) { - if (LOGGER.isInfoEnabled()) - LOGGER.info(e.getMessage()); - throw new BadRequestWebEx(e.getMessage()); - } catch (InternalErrorServiceEx e) { - if (LOGGER.isInfoEnabled()) - LOGGER.info(e.getMessage()); - throw new InternalErrorWebEx(e.getMessage()); - } catch (NotFoundServiceEx e) { - if (LOGGER.isInfoEnabled()) - LOGGER.info(e.getMessage()); - throw new NotFoundWebEx(e.getMessage()); - } - } else { - throw new ForbiddenErrorWebEx( + ShortAttribute owner = resourceService.getAttribute(id, "owner"); + if ((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { + try { + resourceService.updateSecurityRules(id, Convert + .convertSecurityRuleList(securityRules.getList(), id)); + } catch (BadRequestServiceEx e) { + if (LOGGER.isInfoEnabled()) + LOGGER.info(e.getMessage()); + throw new BadRequestWebEx(e.getMessage()); + } catch (InternalErrorServiceEx e) { + if (LOGGER.isInfoEnabled()) + LOGGER.info(e.getMessage()); + throw new InternalErrorWebEx(e.getMessage()); + } catch (NotFoundServiceEx e) { + if (LOGGER.isInfoEnabled()) + LOGGER.info(e.getMessage()); + throw new NotFoundWebEx(e.getMessage()); + } + } else { + throw new ForbiddenErrorWebEx( "This user cannot update this resource permissions!"); - } + } } else { throw new ForbiddenErrorWebEx( "This user cannot write this resource so neither its permissions!"); - } - } + } + } - @Override - public SecurityRuleList getSecurityRules(SecurityContext sc, long id) { - // + @Override + public SecurityRuleList getSecurityRules(SecurityContext sc, long id) { + // // Authorization check. // boolean canRead = false; @@ -592,27 +580,27 @@ public SecurityRuleList getSecurityRules(SecurityContext sc, long id) { canRead = resourceAccessRead(authUser, id); if (canRead) { - ShortAttribute owner = resourceService.getAttribute(id, "owner"); - if((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { - try { - return new SecurityRuleList(resourceService.getSecurityRules(id)); - } catch (BadRequestServiceEx e) { - if (LOGGER.isInfoEnabled()) - LOGGER.info(e.getMessage()); - throw new BadRequestWebEx(e.getMessage()); - } catch (InternalErrorServiceEx e) { - if (LOGGER.isInfoEnabled()) - LOGGER.info(e.getMessage()); - throw new InternalErrorWebEx(e.getMessage()); - } - } else { - throw new ForbiddenErrorWebEx( + ShortAttribute owner = resourceService.getAttribute(id, "owner"); + if ((authUser.getRole() == Role.ADMIN) || (owner == null) || (owner.getValue().equals(authUser.getName()))) { + try { + return new SecurityRuleList(resourceService.getSecurityRules(id)); + } catch (BadRequestServiceEx e) { + if (LOGGER.isInfoEnabled()) + LOGGER.info(e.getMessage()); + throw new BadRequestWebEx(e.getMessage()); + } catch (InternalErrorServiceEx e) { + if (LOGGER.isInfoEnabled()) + LOGGER.info(e.getMessage()); + throw new InternalErrorWebEx(e.getMessage()); + } + } else { + throw new ForbiddenErrorWebEx( "This user cannot read this resource permissions!"); - } + } } else { throw new ForbiddenErrorWebEx( "This user cannot read this resource so neither its permissions!"); } - } - + } + } diff --git a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/utils/Convert.java b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/utils/Convert.java index 9fdf1e0b..19a44987 100644 --- a/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/utils/Convert.java +++ b/src/modules/rest/impl/src/main/java/it/geosolutions/geostore/services/rest/utils/Convert.java @@ -57,6 +57,8 @@ public static Resource convertResource(RESTResource resource) { r.setMetadata(resource.getMetadata()); r.setName(resource.getName()); r.setCategory(category); + r.setCreator(resource.getCreator()); + r.setEditor(resource.getEditor()); r.setAdvertised(resource.isAdvertised()); // Parsing Attributes list