diff --git a/src/pom.xml b/src/pom.xml index 0c30489d..ce9c71ab 100644 --- a/src/pom.xml +++ b/src/pom.xml @@ -6,146 +6,158 @@ */ --> - 4.0.0 + 4.0.0 - org.geoserver.geofence - geofence - 3.7-SNAPSHOT - pom - - - - 8 - 8 - 3.7-SNAPSHOT - 3.6.9.Final - 1.1.0 - 1.1.3.2 - 42.2.18 - 1.3.3 - 1.19.0 - + org.geoserver.geofence + geofence + 3.8-SNAPSHOT + pom - Master GeoFence POM - GeoFence is a java web application that handles authorization rules, integrating with GeoServer using the interface described in GSIP 57. + Master GeoFence POM + GeoFence is a java web application that handles authorization rules, integrating with GeoServer using the interface described in GSIP 57. - https://github.com/geoserver/geofence - 2011 + https://github.com/geoserver/geofence + 2011 - - GeoSolutions - http://www.geo-solutions.it - + + GeoSolutions + https://www.geosolutionsgroup.com + - - - etj - Emanuele Tajariol - GeoSolutions - - architect - developer - - +2 - - - afabiani - Alessio Fabiani - GeoSolutions - - developer - - +2 - - + + + etj + Emanuele Tajariol + GeoSolutions + + architect + developer + + +1 + + + afabiani + Alessio Fabiani + GeoSolutions + + developer + + +1 + + - - github - https://github.com/geoserver/geofence/issues - + + github + https://github.com/geoserver/geofence/issues + - - scm:git:git@github.com:geoserver/geofence.git - main - https://github.com/geoserver/geofence - + + scm:git:git@github.com:geoserver/geofence.git + main + https://github.com/geoserver/geofence + - - jenkins - https://build.geoserver.org - + + jenkins + https://build.geoserver.org + - - - osgeo-releases - OSGeo Nexus Release Repository - https://repo.osgeo.org/repository/release/ - - false - - - true - - + + + osgeo-releases + OSGeo Nexus Release Repository + https://repo.osgeo.org/repository/release/ + + false + + + true + + - - osgeo-snapshots - OSGeo Nexus Snapshot Repository - https://repo.osgeo.org/repository/snapshot/ - - true - - - false - - + + osgeo-snapshots + OSGeo Nexus Snapshot Repository + https://repo.osgeo.org/repository/snapshot/ + + true + + + false + + - - geosolutions - GeoSolutions Repository - https://maven.geo-solutions.it/ - - true - - - true - - - + + geosolutions + GeoSolutions Repository + https://maven.geo-solutions.it/ + + true + + + true + + + - - - nexus - OSGeo Release Repository - https://repo.osgeo.org/repository/Geoserver-releases/ - false - - - nexus - false - OSGeo Snapshot Repository - https://repo.osgeo.org/repository/geoserver-snapshots/ - - + + + nexus + OSGeo Release Repository + https://repo.osgeo.org/repository/Geoserver-releases/ + false + + + nexus + false + OSGeo Snapshot Repository + https://repo.osgeo.org/repository/geoserver-snapshots/ + + - - services - - - - - - - - - org.apache.maven.wagon - wagon-webdav - 1.0-beta-2 - - - - + + services + + + + + libyear + + + + + io.github.mfoo + libyear-maven-plugin + 1.1.0 + + + libyear-analysis + + analyze + + + + + + + + + + + + + + + + + + + org.apache.maven.wagon + wagon-webdav + 1.0-beta-2 + + + + diff --git a/src/samples/csv2geofence/pom.xml b/src/samples/csv2geofence/pom.xml index ef9796d1..29431cc4 100644 --- a/src/samples/csv2geofence/pom.xml +++ b/src/samples/csv2geofence/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence.sample csv2geofence - 3.7-SNAPSHOT + 3.8-SNAPSHOT jar csv2geofence diff --git a/src/services/core/model-external/pom.xml b/src/services/core/model-external/pom.xml index 48a68a7e..41a60c16 100644 --- a/src/services/core/model-external/pom.xml +++ b/src/services/core/model-external/pom.xml @@ -4,8 +4,6 @@ * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ - - --> @@ -14,7 +12,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -26,17 +24,38 @@ - org.locationtech.jts - jts-core + javax.annotation + javax.annotation-api - - + + + javax.xml.bind + jaxb-api + + + org.glassfish.jaxb + jaxb-runtime + + + + + org.locationtech.jts + jts-core @@ -51,7 +70,7 @@ org.apache.maven.plugins maven-dependency-plugin - 2.10 + 3.1.1 @@ -84,7 +103,7 @@ maven-antrun-plugin - 1.7 + 3.1.0 @@ -107,14 +126,28 @@ - + + + + + + + + + + + + + + + - + @@ -133,7 +166,7 @@ org.codehaus.mojo build-helper-maven-plugin - 1.9 + 3.5.0 @@ -155,7 +188,7 @@ true org.apache.maven.plugins maven-source-plugin - 2.3 + 3.1.0 true diff --git a/src/services/core/model/pom.xml b/src/services/core/model/pom.xml index d2bbd053..d54ea1bc 100644 --- a/src/services/core/model/pom.xml +++ b/src/services/core/model/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -24,7 +24,35 @@ Model with JPA annotation used internally - + + + javax.annotation + javax.annotation-api + + + + jakarta.xml.bind + jakarta.xml.bind-api + 4.0.2 + + + org.glassfish.jaxb + jaxb-runtime + + + + org.hibernate hibernate-core @@ -43,14 +71,6 @@ - - jakarta.xml.bind - jakarta.xml.bind-api - - - org.glassfish.jaxb - jaxb-runtime - org.locationtech.jts jts-core @@ -87,7 +107,7 @@ org.apache.maven.plugins maven-source-plugin - 3.0.0 + 3.1.0 attach-sources @@ -97,6 +117,9 @@ + diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/AdminRule.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/AdminRule.java index a06571a8..49793278 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/AdminRule.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/AdminRule.java @@ -16,8 +16,11 @@ import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.FetchType; +import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @@ -26,8 +29,6 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; -import org.hibernate.annotations.ForeignKey; -import org.hibernate.annotations.Index; /** * An AdminRule expresses if a given combination of request access is allowed or not. @@ -39,14 +40,23 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "AdminRule") -@Table(name = "gf_adminrule", uniqueConstraints = { - @UniqueConstraint(columnNames = {"username", "rolename", "instance_id", "workspace"})}) +@Table(name = "gf_adminrule", + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = {"username", "rolename", "instance_id", "workspace"}, // @InternalModel + name = "gf_adminrule_username_rolename_instance_id_workspace_key" // @InternalModel + )}, // @InternalModel + indexes = { // @InternalModel + @Index(name="idx_adminrule_priority", columnList = "priority"), + @Index(name = "idx_adminrule_workspace", columnList = "workspace") + } // @InternalModel +) // @InternalModel @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "Rule") @XmlRootElement(name = "AdminRule") @XmlType(propOrder={"id","priority","username","rolename","instance","addressRange","workspace","access"}) public class AdminRule implements Identifiable, Serializable, Prioritizable, IPRangeProvider { - private static final long serialVersionUID = -5127129225384707999L; + private static final long serialVersionUID = -3807129225384707999L; /** The id. */ @Id @@ -56,7 +66,6 @@ public class AdminRule implements Identifiable, Serializable, Prioritizable, IPR /** Lower numbers have higher priority */ @Column(nullable = false) - @Index(name = "idx_adminrule_priority") private long priority; @Column(name = "username", nullable = true) @@ -66,7 +75,7 @@ public class AdminRule implements Identifiable, Serializable, Prioritizable, IPR private String rolename; @ManyToOne(optional = true, fetch = FetchType.EAGER) - @ForeignKey(name = "fk_adminrule_instance") + @JoinColumn(foreignKey = @ForeignKey(name="fk_adminrule_instance")) private GSInstance instance; @Embedded @@ -77,7 +86,6 @@ public class AdminRule implements Identifiable, Serializable, Prioritizable, IPR private IPAddressRange addressRange; @Column - @Index(name = "idx_adminrule_workspace") private String workspace; @Enumerated(EnumType.STRING) diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GFUser.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GFUser.java index 4bbb669c..f404184c 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GFUser.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GFUser.java @@ -15,6 +15,7 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import javax.persistence.UniqueConstraint; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @@ -27,7 +28,14 @@ * */ @Entity(name = "GFUser") -@Table(name = "gf_gfuser") +@Table(name = "gf_gfuser", + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = {"extid"}, name = "gf_gfuser_extid_key"), // @InternalModel + @UniqueConstraint( + columnNames = {"name"}, name = "gf_gfuser_name_key") // @InternalModel + } // @InternalModel +) // @InternalModel @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "GFUser") @XmlRootElement(name = "GFUser") @XmlType(propOrder={"id","extId","name","enabled","fullName","password","emailAddress","dateCreation"}) @@ -35,11 +43,11 @@ public class GFUser implements Identifiable, Serializable { /** - * - */ - private static final long serialVersionUID = -5161617651332259455L; + * + */ + private static final long serialVersionUID = -3801617651332259455L; - /** The id. */ + /** The id. */ @Id @GeneratedValue @Column @@ -48,11 +56,11 @@ public class GFUser implements Identifiable, Serializable { /** External Id. An ID used in an external systems. * This field should simplify Geofence integration in complex systems. */ - @Column(nullable=true, updatable=false, unique=true) + @Column(nullable=true, updatable=false) private String extId; /** The name. */ - @Column(nullable=false, unique=true) + @Column(nullable=false) private String name; /** The user name. */ diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSInstance.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSInstance.java index e0beab4d..9ffbfcb1 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSInstance.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSInstance.java @@ -35,7 +35,7 @@ public class GSInstance implements Identifiable, Serializable { /** The Constant serialVersionUID. */ - private static final long serialVersionUID = -2584592064221812813L; + private static final long serialVersionUID = 3804592064221812813L; /** The id. */ @Id diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSUser.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSUser.java index 79802ee4..6214eaa0 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSUser.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/GSUser.java @@ -16,14 +16,17 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; +import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.Index; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import javax.persistence.UniqueConstraint; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @@ -34,8 +37,6 @@ import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.annotations.ForeignKey; -import org.hibernate.annotations.Index; /** * A User that can access GeoServer resources. @@ -45,13 +46,21 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "GSUser") -@Table(name = "gf_gsuser") +@Table(name = "gf_gsuser", + indexes = { @Index(name = "idx_gsuser_name", columnList = "name")}, + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = "extid", name = "gf_gsuser_extid_key"), // @InternalModel + @UniqueConstraint( + columnNames = "name", name = "gf_gsuser_name_key") // @InternalModel + } // @InternalModel +) // @InternalModel @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "GSUser") @XmlRootElement(name = "GSUser") @XmlType(propOrder={"id","extId","name","fullName","password","emailAddress","dateCreation","groups"}) public class GSUser implements Identifiable, Serializable { - private static final long serialVersionUID = 7718458156939088033L; + private static final long serialVersionUID = 3808458156939088033L; /** The id. */ @Id @@ -63,12 +72,11 @@ public class GSUser implements Identifiable, Serializable { * External Id. An ID used in an external systems. * This field should simplify Geofence integration in complex systems. */ - @Column(nullable=true, updatable=false, unique=true) + @Column(nullable=true, updatable=false) private String extId; /** The name. */ - @Index(name = "idx_gsuser_name") - @Column(nullable=false, unique=true) + @Column(nullable=false) private String name; /** The user name. */ @@ -98,9 +106,14 @@ public class GSUser implements Identifiable, Serializable { /** Groups to which the user is associated */ @ManyToMany(fetch= FetchType.LAZY) - @JoinTable( name = "gf_user_usergroups", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns=@JoinColumn(name = "group_id") ) - @Column(name = "u_id") - @ForeignKey(name="fk_uug_user", inverseName="fk_uug_group") + @JoinTable( + name = "gf_user_usergroups", // @InternalModel + joinColumns = @JoinColumn(name = "user_id"), + inverseJoinColumns = @JoinColumn(name = "group_id"), + foreignKey = @ForeignKey(name="fk_uug_user"), + inverseForeignKey = @ForeignKey(name="fk_uug_group") + ) // @JoinTable + @Column(name = "u_id") @Fetch(FetchMode.SUBSELECT) // without this, hibernate will duplicate results(!) private Set userGroups = new HashSet(); diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/IPAddressRange.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/IPAddressRange.java index 28f1d248..8c425975 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/IPAddressRange.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/IPAddressRange.java @@ -20,6 +20,8 @@ @Embeddable public class IPAddressRange implements Serializable { + private static final long serialVersionUID = 3808458156939088033L; + /** * The lower 64 bits. * For IPv4, only the lower 32 are used. @@ -44,7 +46,7 @@ protected IPAddressRange() { public IPAddressRange(String cidrNotation) { SubnetV4Utils su = new SubnetV4Utils(cidrNotation); - low = new Long(su.getInfo().getAddressAsInteger()); + low = Long.valueOf(su.getInfo().getAddressAsInteger()); size = su.getInfo().getMaskSize(); } diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Identifiable.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Identifiable.java index f62c5d55..c3d4476f 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Identifiable.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Identifiable.java @@ -14,6 +14,4 @@ public interface Identifiable { Long getId(); void setId(Long id); -// String getName(); -// void setName(String name); } diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerAttribute.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerAttribute.java index 533871e1..a0274a9b 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerAttribute.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerAttribute.java @@ -7,8 +7,6 @@ import org.geoserver.geofence.core.model.enums.AccessType; import java.io.Serializable; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.EnumType; @@ -27,7 +25,7 @@ @XmlRootElement(name = "LayerAttribute") public class LayerAttribute implements Serializable, Cloneable { - private static final long serialVersionUID = -4739817113509675752L; + private static final long serialVersionUID = 3809817113509675752L; @Column(nullable=false) private String name; diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerDetails.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerDetails.java index b04683d9..b921b4b7 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerDetails.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/LayerDetails.java @@ -14,10 +14,12 @@ import java.util.HashSet; import java.util.Set; import javax.persistence.Column; +import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.FetchType; +import javax.persistence.ForeignKey; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; @@ -32,8 +34,6 @@ import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; -import org.hibernate.annotations.ForeignKey; -import org.hibernate.annotations.Type; /** * Details may be set only for ules with non-wildcarded profile, instance, workspace,layer. @@ -46,12 +46,19 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "LayerDetails") -@Table(name = "gf_layer_details") +@Table(name = "gf_layer_details", + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = "rule_id", name = "gf_layer_details_rule_id_key") // @InternalModel + } // @InternalModel +) // @InternalModel + + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "LayerDetails") @XmlRootElement(name = "LayerDetails") -public class LayerDetails implements Serializable { +public class LayerDetails implements Identifiable, Serializable { - private static final long serialVersionUID = -4150963895550551513L; + private static final long serialVersionUID = 3800963895550551513L; /** The id. */ @Id @@ -72,13 +79,12 @@ public class LayerDetails implements Serializable { @Column(length=4000) private String cqlFilterWrite; - @Type(type = "org.hibernatespatial.GeometryUserType") - @Column(name = "area") - private MultiPolygon area; + @Column(name = "area") + private MultiPolygon area; - @Enumerated(EnumType.STRING) + @Enumerated(EnumType.STRING) @Column(name="spatial_filter_type",nullable = true) - private SpatialFilterType spatialFilterType; + private SpatialFilterType spatialFilterType; @Enumerated(EnumType.STRING) @Column(name = "catalog_mode", nullable = true) @@ -86,27 +92,34 @@ public class LayerDetails implements Serializable { @OneToOne(optional=false) // @Check(constraints="rule.access='LIMIT'") // ??? check this - @ForeignKey(name="fk_details_rule") + @JoinColumn(foreignKey = @ForeignKey(name="fk_details_rule")) private Rule rule; /** Styles allowed for this layer */ - @org.hibernate.annotations.CollectionOfElements(fetch=FetchType.EAGER) - @JoinTable( name = "gf_layer_styles", joinColumns = @JoinColumn(name = "details_id")) - @ForeignKey(name="fk_styles_layer") + @ElementCollection(fetch=FetchType.EAGER) + @JoinTable( + name = "gf_layer_styles", // @InternalModel + joinColumns = @JoinColumn(name = "details_id"), + foreignKey = @ForeignKey(name="fk_styles_layer") + ) // @InternalModel @Column(name="styleName") - private Set allowedStyles = new HashSet(); + private Set allowedStyles = new HashSet<>(); /** Feature Attributes associated to the Layer *

We'll use the pair (details_id, name) as PK for the associated table. * To do so, we have to perform some trick on the {@link LayerAttribute#access} field. */ - @org.hibernate.annotations.CollectionOfElements(fetch=FetchType.EAGER) - @JoinTable( name = "gf_layer_attributes", joinColumns = @JoinColumn(name = "details_id"), uniqueConstraints = @UniqueConstraint(columnNames={"details_id", "name"})) + @ElementCollection(fetch=FetchType.EAGER) + @JoinTable( + name = "gf_layer_attributes", // @InternalModel + joinColumns = @JoinColumn(name = "details_id"), + uniqueConstraints = @UniqueConstraint(columnNames={"details_id", "name"}, name="gf_layer_attributes_didname_unq"), + foreignKey = @ForeignKey(name="fk_attribute_layer") + ) // @InternalModel // override is used to set the pk as {"details_id", "name"} // @AttributeOverride( name="access", column=@Column(name="access", nullable=false) ) - @ForeignKey(name="fk_attribute_layer") @Fetch(FetchMode.SELECT) // without this, hibernate will duplicate results(!) - private Set attributes = new HashSet(); + private Set attributes = new HashSet<>(); @XmlJavaTypeAdapter(MultiPolygonAdapter.class) public MultiPolygon getArea() { diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Rule.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Rule.java index d988bd08..4cdee966 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Rule.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/Rule.java @@ -17,8 +17,11 @@ import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.FetchType; +import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.Index; +import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToOne; import javax.persistence.Table; @@ -28,8 +31,6 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; -import org.hibernate.annotations.ForeignKey; -import org.hibernate.annotations.Index; /** * A Rule expresses if a given combination of request access is allowed or not. @@ -61,14 +62,26 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "Rule") -@Table(name = "gf_rule", uniqueConstraints = { - @UniqueConstraint(columnNames = {"username", "rolename", "instance_id", "service", "request", "workspace", "layer"})}) +@Table(name = "gf_rule", + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = {"username", "rolename", "instance_id", "service", "request", "workspace", "layer"}, // @InternalModel + name = "gf_rule_username_rolename_instance_id_service_request_works_key" // @InternalModel + )}, // @InternalModel + indexes = { // @InternalModel + @Index(name = "idx_rule_priority", columnList = "priority"), + @Index(name = "idx_rule_service", columnList = "service"), + @Index(name = "idx_rule_request", columnList = "request"), + @Index(name = "idx_rule_workspace", columnList = "workspace"), + @Index(name = "idx_rule_layer", columnList = "layer") + } // @InternalModel +) // @InternalModel @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "Rule") @XmlRootElement(name = "Rule") @XmlType(propOrder={"id","priority","username","rolename","instance","addressRange","service","request","workspace","layer","access","layerDetails","ruleLimits"}) public class Rule implements Identifiable, Serializable, Prioritizable, IPRangeProvider { - private static final long serialVersionUID = -5127129225384707164L; + private static final long serialVersionUID = -3803809225384707164L; /** The id. */ @Id @@ -78,7 +91,6 @@ public class Rule implements Identifiable, Serializable, Prioritizable, IPRangeP /** Lower numbers have higher priority */ @Column(nullable = false) - @Index(name = "idx_rule_priority") private long priority; @Column(name = "username", nullable = true) @@ -88,11 +100,10 @@ public class Rule implements Identifiable, Serializable, Prioritizable, IPRangeP private String rolename; @ManyToOne(optional = true, fetch = FetchType.EAGER) - @ForeignKey(name = "fk_rule_instance") + @JoinColumn(foreignKey = @ForeignKey(name="fk_rule_instance")) private GSInstance instance; @Column - @Index(name = "idx_rule_service") private String service; @Embedded @@ -103,18 +114,15 @@ public class Rule implements Identifiable, Serializable, Prioritizable, IPRangeP private IPAddressRange addressRange; @Column - @Index(name = "idx_rule_request") private String request; @Column private String subfield; @Column - @Index(name = "idx_rule_workspace") private String workspace; @Column - @Index(name = "idx_rule_layer") private String layer; @Enumerated(EnumType.STRING) @@ -122,11 +130,11 @@ public class Rule implements Identifiable, Serializable, Prioritizable, IPRangeP private GrantType access; @OneToOne(optional = true, cascade = CascadeType.REMOVE, mappedBy = "rule") // main ref is in LayerDetails - @ForeignKey(name = "fk_rule_details") + @JoinColumn(foreignKey = @ForeignKey(name="fk_rule_details")) private LayerDetails layerDetails; @OneToOne(optional = true, cascade = CascadeType.REMOVE, mappedBy = "rule") // main ref is in ruleLimits - @ForeignKey(name = "fk_rule_limits") + @JoinColumn(foreignKey = @ForeignKey(name="fk_rule_limits")) private RuleLimits ruleLimits; public Rule() { @@ -147,15 +155,6 @@ public Rule(long priority, String username, String rolename, GSInstance instance this.access = access; } - /** - * @deprecated need new subfield argument - */ - @Deprecated - public Rule(long priority, String username, String rolename, GSInstance instance, IPAddressRange addressRange, - String service, String request, String workspace, String layer, GrantType access) { - this(priority, username, rolename, instance, addressRange, service, request, null, workspace, layer, access); - } - public Long getId() { return id; } @@ -262,6 +261,7 @@ public RuleLimits getRuleLimits() { * @deprecated This setter is only used by hibernate, should not be called by the user. * @param ruleLimits */ + @Deprecated public void setRuleLimits(RuleLimits ruleLimits) { this.ruleLimits = ruleLimits; } @@ -273,6 +273,7 @@ public LayerDetails getLayerDetails() { /** * @deprecated This setter is only used by hibernate, should not be called by the user. */ + @Deprecated public void setLayerDetails(LayerDetails layerDetails) { this.layerDetails = layerDetails; } diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/RuleLimits.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/RuleLimits.java index 53cbe550..d4d039c6 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/RuleLimits.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/RuleLimits.java @@ -11,7 +11,9 @@ import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; +import javax.persistence.ForeignKey; import javax.persistence.Id; +import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.UniqueConstraint; @@ -26,8 +28,6 @@ import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Check; -import org.hibernate.annotations.ForeignKey; -import org.hibernate.annotations.Type; import org.locationtech.jts.geom.MultiPolygon; @@ -38,12 +38,16 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "RuleLimits") -@Table(name = "gf_rule_limits", uniqueConstraints = @UniqueConstraint(columnNames = "rule_id")) +@Table(name = "gf_rule_limits", + uniqueConstraints = @UniqueConstraint( + columnNames = "rule_id", // @InternalModel + name = "gf_rule_limits_rule_id_key") // @InternalModel +) // @InternalModel @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "RuleLimits") @XmlRootElement(name = "RuleLimits") -public class RuleLimits implements Serializable { +public class RuleLimits implements Identifiable, Serializable { - private static final long serialVersionUID = 2829839552804345725L; + private static final long serialVersionUID = 3809839552804345725L; /** The id. */ @Id @@ -53,10 +57,10 @@ public class RuleLimits implements Serializable { @OneToOne(optional = false) @Check(constraints = "rule.access='LIMIT'") // ??? check this - @ForeignKey(name = "fk_limits_rule") + @JoinColumn(foreignKey = @ForeignKey(name = "fk_limits_rule")) private Rule rule; - @Type(type = "org.hibernatespatial.GeometryUserType") + //@Type(type = "org.hibernate.spatial.JTSGeometryType") @Column(name = "area") private MultiPolygon allowedArea; diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/UserGroup.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/UserGroup.java index 08e42c20..3a6956dd 100644 --- a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/UserGroup.java +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/UserGroup.java @@ -16,6 +16,7 @@ import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; +import javax.persistence.UniqueConstraint; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @@ -30,7 +31,15 @@ * @author ETj (etj at geo-solutions.it) */ @Entity(name = "UserGroup") -@Table(name = "gf_usergroup") +@Table(name = "gf_usergroup", + uniqueConstraints = { // @InternalModel + @UniqueConstraint( + columnNames = {"extid"}, name = "gf_usergroup_extid_key"), // @InternalModel + @UniqueConstraint( + columnNames = {"name"}, name = "gf_usergroup_name_key") // @InternalModel + } // @InternalModel +) // @InternalModel + @Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "usergroup") @XmlRootElement(name = "UserGroup") @XmlType(propOrder={"id","extId","name","dateCreation"/*,"customProps"*/}) @@ -49,11 +58,11 @@ public class UserGroup implements Identifiable, Serializable { * An ID used in an external systems. * This field should simplify Geofence integration in complex systems. */ - @Column(nullable=true, updatable=false, unique=true) + @Column(nullable=true, updatable=false) private String extId; /** The name. */ - @Column(nullable=false, updatable=true, unique=true) + @Column(nullable=false, updatable=true) private String name; /** The date creation. */ diff --git a/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/util/EWKTParser.java b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/util/EWKTParser.java new file mode 100644 index 00000000..1e9427d0 --- /dev/null +++ b/src/services/core/model/src/main/java/org/geoserver/geofence/core/model/util/EWKTParser.java @@ -0,0 +1,35 @@ +/* (c) 2024 Open Source Geospatial Foundation - all rights reserved + * This code is licensed under the GPL 2.0 license, available at the root + * application directory. + */ +package org.geoserver.geofence.core.model.util; + +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.io.ParseException; +import org.locationtech.jts.io.WKTReader; + +/** + * + * @author etj + */ +public class EWKTParser { + + static public Geometry parse(String wkt) throws ParseException { + if (wkt == null) { + return null; + } + + WKTReader reader = new WKTReader(); + Geometry result; + if (wkt.startsWith("SRID=")) { + String[] areaAr = wkt.split(";"); + String srid = areaAr[0].split("=")[1]; + result = reader.read(areaAr[1]); + result.setSRID(Integer.valueOf(srid)); + } else { + result = reader.read(wkt); + result.setSRID(4326); + } + return result; + } +} diff --git a/src/services/core/persistence-pg-test/pom.xml b/src/services/core/persistence-pg-test/pom.xml index b2282e5d..97d5fb34 100644 --- a/src/services/core/persistence-pg-test/pom.xml +++ b/src/services/core/persistence-pg-test/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/core/persistence/pom.xml b/src/services/core/persistence/pom.xml index 3593050e..41c4fdc3 100644 --- a/src/services/core/persistence/pom.xml +++ b/src/services/core/persistence/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -69,7 +69,7 @@ - dom4j + org.dom4j dom4j @@ -98,36 +98,36 @@ spring-jdbc - - - + + + - - + + + JPA1 dependency will excluded by exclusions in depenendency management com.googlecode.genericdao search-jpa-hibernate - + --> - + commons-dbcp commons-dbcp - + - org.hibernatespatial + org.hibernate hibernate-spatial @@ -152,23 +152,39 @@ - - com.h2database - h2 - + + com.h2database + h2 + + + + + + + org.opengeo + geodb + 0.9 + - - - + + + - - org.hibernate - hibernate-ehcache - + + org.hibernate + hibernate-jcache + + + + org.ehcache + ehcache + 3.8.1 + + org.hibernate - hibernate-entitymanager + hibernate-core asm @@ -185,14 +201,14 @@ - + cglib cglib-nodep - - + @@ -205,49 +221,78 @@ aspectjweaver - + junit junit test + + org.slf4j + slf4j-simple + 1.7.30 + test + + + + + com.google.guava + guava + 33.4.0-jre + + - + org.codehaus.mojo - hibernate3-maven-plugin - 3.0 - - - - generate-sources - - run - - - - + exec-maven-plugin + 3.5.0 + + exportSchema + - - - - - - - - - - - - - - - + org.geoserver.geofence.SchemaExporter - + + + org.geoserver.geofence + geofence-model-internal + ${geofence-version} + + + org.geoserver.geofence + geofence-persistence + ${geofence-version} + + + com.google.guava + guava + 33.4.0-jre + + + + org.apache.logging.log4j + log4j-core + 2.24.3 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.24.3 + + + org.slf4j + slf4j-simple + 1.7.30 + + + + + + org.apache.maven.plugins @@ -265,6 +310,7 @@ org.apache.maven.plugins maven-source-plugin + 3.3.1 attach-sources @@ -276,6 +322,24 @@ + + org.apache.maven.plugins + maven-surefire-plugin + + + false + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.13.0 + + + -Xlint + + + diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/SchemaExporter.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/SchemaExporter.java new file mode 100644 index 00000000..64bd2bd8 --- /dev/null +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/SchemaExporter.java @@ -0,0 +1,95 @@ +/* + */ +package org.geoserver.geofence; + +import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.geoserver.geofence.core.model.AdminRule; +import org.geoserver.geofence.core.model.GFUser; +import org.geoserver.geofence.core.model.GSInstance; +import org.geoserver.geofence.core.model.GSUser; +import org.geoserver.geofence.core.model.IPAddressRange; +import org.geoserver.geofence.core.model.IPRangeProvider; +import org.geoserver.geofence.core.model.Identifiable; +import org.geoserver.geofence.core.model.LayerAttribute; +import org.geoserver.geofence.core.model.LayerDetails; +import org.geoserver.geofence.core.model.Rule; +import org.geoserver.geofence.core.model.RuleLimits; +import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.core.model.adapter.MultiPolygonAdapter; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.service.ServiceRegistry; +import org.hibernate.boot.MetadataSources; +import org.hibernate.tool.hbm2ddl.SchemaExport; +import org.hibernate.tool.hbm2ddl.SchemaExport.Action; +import org.hibernate.tool.schema.TargetType; + +/** + * + * @author etj + */ +public class SchemaExporter { + + private static final Logger LOGGER = LogManager.getLogger(SchemaExporter.class); + + static final String DIALECT_POSTGIS = "org.hibernate.spatial.dialect.postgis.PostgisDialect"; + static final String DIALECT_H2 = "org.hibernate.dialect.H2Dialect"; + + public static void main(String[] args) { + SchemaExporter exporter = new SchemaExporter(); + + exporter.createSqlFile(DIALECT_POSTGIS, "./schema.postgres.sql", true); + exporter.createSqlFile(DIALECT_H2, "./schema.h2.sql", false); + } + + + public void createSqlFile(String dialect, String outputFile, boolean stdout) { + + Map settings = new HashMap<>(); + + settings.put("hibernate.dialect", dialect); +// settings.put("hibernate.show_sql", "true"); + settings.put("hibernate.format_sql", "true"); + + ServiceRegistry serviceRegistry + = new StandardServiceRegistryBuilder().applySettings(settings).build(); + + MetadataSources sources = new MetadataSources(serviceRegistry); + + Class modelClasses[] = { + AdminRule.class, + GFUser.class, + GSInstance.class, + GSUser.class, + IPAddressRange.class, + IPRangeProvider.class, + Identifiable.class, + LayerAttribute.class, + LayerDetails.class, + Rule.class, + RuleLimits.class, + UserGroup.class, + MultiPolygonAdapter.class + }; + + for(Class c: modelClasses) { + sources.addAnnotatedClass(c); + } + + EnumSet enumSet = EnumSet.of(TargetType.SCRIPT); + if(stdout) { + enumSet.add(TargetType.STDOUT); + } + + SchemaExport schemaExport = new SchemaExport(); + schemaExport.setOutputFile(outputFile) + .setOverrideOutputFileContent() + .setDelimiter(";") + .setFormat(true) + .execute(enumSet, Action.CREATE, sources.buildMetadata()); + } + +} diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/AdminRuleDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/AdminRuleDAO.java index c1234a0c..3db1d224 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/AdminRuleDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/AdminRuleDAO.java @@ -14,7 +14,8 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public interface AdminRuleDAO extends PrioritizableDAO { +public interface AdminRuleDAO // + extends PrioritizableDAO, SearchableDAO { long persist(AdminRule entity, InsertPosition position); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GFUserDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GFUserDAO.java index e40ee617..0ae1587c 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GFUserDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GFUserDAO.java @@ -13,5 +13,6 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public interface GFUserDAO extends RestrictedGenericDAO { +public interface GFUserDAO // + extends RestrictedGenericDAO, SearchableDAO { } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSInstanceDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSInstanceDAO.java index 6c5e4af0..25f5f9e8 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSInstanceDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSInstanceDAO.java @@ -13,5 +13,6 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public interface GSInstanceDAO extends RestrictedGenericDAO { +public interface GSInstanceDAO // + extends RestrictedGenericDAO, SearchableDAO { } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSUserDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSUserDAO.java index c2fee6f2..4ab65801 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSUserDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/GSUserDAO.java @@ -5,6 +5,7 @@ package org.geoserver.geofence.core.dao; +import java.util.List; import org.geoserver.geofence.core.model.GSUser; /** @@ -17,4 +18,7 @@ public interface GSUserDAO extends RestrictedGenericDAO, RegistrableDAO /** Fetch a GSUser with all of its related groups */ GSUser getFull(String name); + + List search(String nameLike, Integer page, Integer entries, boolean fetchGroups) throws IllegalArgumentException; + long countByNameLike(String nameLike); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RestrictedGenericDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RestrictedGenericDAO.java index ed6a7ced..cecbade1 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RestrictedGenericDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RestrictedGenericDAO.java @@ -7,8 +7,6 @@ import java.util.List; -import com.googlecode.genericdao.search.ISearch; - /** * Public interface to define a restricted set of operation wrt to ones * defined in GenericDAO. @@ -19,13 +17,10 @@ */ public interface RestrictedGenericDAO /* extends GenericDAO */{ - public List findAll(); public ENTITY find(Long id); public void persist(ENTITY... entities); public ENTITY merge(ENTITY entity); public boolean remove(ENTITY entity); public boolean removeById(Long id); - public List search(ISearch search); - public int count(ISearch search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RuleDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RuleDAO.java index 9cf52548..da48226b 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RuleDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/RuleDAO.java @@ -14,7 +14,8 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public interface RuleDAO extends PrioritizableDAO { +public interface RuleDAO // + extends PrioritizableDAO, SearchableDAO { long persist(Rule entity, InsertPosition position); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/SearchableDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/SearchableDAO.java new file mode 100644 index 00000000..8adf52c8 --- /dev/null +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/SearchableDAO.java @@ -0,0 +1,20 @@ +/* (c) 2020 Open Source Geospatial Foundation - all rights reserved + * This code is licensed under the GPL 2.0 license, available at the root + * application directory. + */ +package org.geoserver.geofence.core.dao; + +import java.util.List; +import org.geoserver.geofence.core.dao.search.Search; + +/** + * @author Emanuele Tajariol (etj at geo-solutions.it) + */ +public interface SearchableDAO { + + public Search createSearch(); + public Search createCountSearch(); + + public List search(Search search); + public long count(Search search); +} diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/UserGroupDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/UserGroupDAO.java index 8f8149eb..528a7236 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/UserGroupDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/UserGroupDAO.java @@ -5,6 +5,7 @@ package org.geoserver.geofence.core.dao; +import java.util.List; import org.geoserver.geofence.core.model.UserGroup; /** @@ -13,5 +14,11 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public interface UserGroupDAO extends RestrictedGenericDAO, RegistrableDAO { +public interface UserGroupDAO // + extends RestrictedGenericDAO, RegistrableDAO { + + UserGroup get(String name); + + List search(String nameLike, Integer page, Integer entries) throws IllegalArgumentException; + long countByNameLike(String nameLike); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/AdminRuleDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/AdminRuleDAOImpl.java index 38165ba8..7cc81fd5 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/AdminRuleDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/AdminRuleDAOImpl.java @@ -7,14 +7,12 @@ import java.util.List; -import com.googlecode.genericdao.search.ISearch; -import com.googlecode.genericdao.search.Search; - -import static org.geoserver.geofence.core.dao.util.SearchUtil.*; +import static org.geoserver.geofence.core.dao.search.SearchUtil.*; import org.geoserver.geofence.core.model.enums.InsertPosition; import org.geoserver.geofence.core.dao.AdminRuleDAO; import org.geoserver.geofence.core.dao.DuplicateKeyException; +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.core.model.AdminRule; import org.apache.logging.log4j.LogManager; @@ -28,10 +26,17 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ @Transactional(value = "geofenceTransactionManager") -public class AdminRuleDAOImpl extends PrioritizableDAOImpl implements AdminRuleDAO { +public class AdminRuleDAOImpl + extends PrioritizableDAOImpl + implements AdminRuleDAO { +// public class RuleDAOImpl extends PrioritizableDAOImpl implements RuleDAO { private static final Logger LOGGER = LogManager.getLogger(AdminRuleDAOImpl.class); + public AdminRuleDAOImpl() { + super(AdminRule.class); + } + @Override public void persist(AdminRule... entities) { @@ -77,7 +82,7 @@ public void persistInternal(AdminRule entity) { } protected Search getDupSearch(AdminRule rule) { - Search search = new Search(AdminRule.class); + Search search = createSearch(); addSearchField(search, "username", rule.getUsername()); addSearchField(search, "rolename", rule.getRolename()); addSearchField(search, "instance", rule.getInstance()); @@ -94,7 +99,7 @@ public List findAll() { } @Override - public List search(ISearch search) { + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/BaseDAO.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/BaseDAO.java index 928346d0..a1b586f7 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/BaseDAO.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/BaseDAO.java @@ -1,70 +1,144 @@ -/* (c) 2014 Open Source Geospatial Foundation - all rights reserved +/* (c) 2014 - 2020 Open Source Geospatial Foundation - all rights reserved * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ - package org.geoserver.geofence.core.dao.impl; -import com.googlecode.genericdao.dao.jpa.GenericDAOImpl; -import com.googlecode.genericdao.search.jpa.JPASearchProcessor; +import org.geoserver.geofence.core.dao.search.Search; import java.io.Serializable; +import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; - -//import com.trg.dao.jpa.GenericDAOImpl; -//import com.trg.search.jpa.JPASearchProcessor; - +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import org.geoserver.geofence.core.model.Identifiable; +import org.hibernate.Session; import org.springframework.stereotype.Repository; - /** * * The base DAO furnish a set of methods usually used * - * @author Tobia Di Pisa (tobia.dipisa@geo-solutions.it) */ @Repository(value = "geofence") -public class BaseDAO extends GenericDAOImpl +public class BaseDAO // extends GenericDAOImpl { + protected final Class ENTITY; + + protected BaseDAO(Class entity) { + this.ENTITY = entity; + } @PersistenceContext(unitName = "geofenceEntityManagerFactory") - private EntityManager entityManager; - - /** - * EntityManager setting - * - * @param entityManager - * the entity manager to set - */ - @Override - public void setEntityManager(EntityManager entityManager) - { - this.entityManager = entityManager; - super.setEntityManager(this.entityManager); - } - - /** - * JPASearchProcessor setting - * - * @param searchProcessor - * the search processor to set - */ - @Override - public void setSearchProcessor(JPASearchProcessor searchProcessor) - { - super.setSearchProcessor(searchProcessor); - } - - /* - * (non-Javadoc) - * - * @see com.trg.dao.jpa.JPABaseDAO#em() - */ - @Override - public EntityManager em() - { - return this.entityManager; + private EntityManager em; + + + public void setEntityManager(EntityManager em) { + this.em = em; + } + + public EntityManager em() { + return this.em; + } + + public Session session() { + return em.unwrap(Session.class); + } + + public class DSearch extends Search { + + public DSearch(EntityManager em, Class resultType ) { + super(em, resultType); + } + + public DSearch(EntityManager em, Class resultType, Class baseClass) { + super(em, resultType, baseClass); + } + } + + public Search createSearch(Class resultType) { + return new DSearch(em, resultType); + } + + public Search createSearch() { + return createSearch(ENTITY); + } + + public Search createCountSearch() { + return new DSearch(em, Long.class, ENTITY); + } + + public List findAll() { + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery q = cb.createQuery(ENTITY); + Root c = q.from(ENTITY); + q.select(c); + + return em.createQuery(q).getResultList(); } + + public E find(ID id) { + return em().find(ENTITY, id); + } + + public void persist(E... entities) { + for (E entity : entities) { + em.persist(entity); + } + } + + public E merge(E entity) { + return em.merge(entity); + } + + public void merge(E... entities) { + for (E entity : entities) { + em.merge(entity); + } + } + + public boolean remove(E entity) { + return removeById(entity.getId()); + } + + public boolean removeById(Long id) { + E e = em.find(ENTITY, id); + if (e == null) { + return false; + } + em.remove(e); + return true; + } + + public List _search(Search search) { + return search(search); + } + + public List search(Search search) { + return search.getQuery().getResultList(); + } + + protected Object searchUnique(Search search) { + List found = search(search); + switch (found.size()) { + case 0: + return null; + case 1: + return found.get(0); + default: + throw new IllegalStateException("Result is not unique"); + } + } + + public long count(Search search) { + if(search == null) { + search = createSearch(); + } + + return (Long)search.getCountQuery().getSingleResult(); + } + } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GFUserDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GFUserDAOImpl.java index 93b979b9..ed857f78 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GFUserDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GFUserDAOImpl.java @@ -8,13 +8,13 @@ import org.geoserver.geofence.core.dao.GFUserDAO; import org.geoserver.geofence.core.model.GFUser; +import org.geoserver.geofence.core.dao.search.Search; import java.util.Date; import java.util.List; import org.springframework.transaction.annotation.Transactional; -import com.googlecode.genericdao.search.ISearch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,11 +25,17 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ @Transactional(value = "geofenceTransactionManager") -public class GFUserDAOImpl extends BaseDAO implements GFUserDAO +public class GFUserDAOImpl // + extends BaseDAO // + implements GFUserDAO { private static final Logger LOGGER = LogManager.getLogger(GFUserDAOImpl.class); + public GFUserDAOImpl() { + super(GFUser.class); + } + @Override public void persist(GFUser... entities) { @@ -48,7 +54,7 @@ public List findAll() } @Override - public List search(ISearch search) + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSInstanceDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSInstanceDAOImpl.java index 148b3f49..8885509b 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSInstanceDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSInstanceDAOImpl.java @@ -7,13 +7,14 @@ import java.util.List; -import com.googlecode.genericdao.search.ISearch; import org.geoserver.geofence.core.dao.GSInstanceDAO; +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.core.model.GSInstance; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; + import org.springframework.transaction.annotation.Transactional; @@ -28,6 +29,10 @@ public class GSInstanceDAOImpl extends BaseDAO implements GSIn private static final Logger LOGGER = LogManager.getLogger(GSInstanceDAOImpl.class); + public GSInstanceDAOImpl() { + super(GSInstance.class); + } + @Override public void persist(GSInstance... entities) { @@ -41,7 +46,7 @@ public List findAll() } @Override - public List search(ISearch search) + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSUserDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSUserDAOImpl.java index 28400667..159af503 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSUserDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/GSUserDAOImpl.java @@ -7,17 +7,12 @@ import org.geoserver.geofence.core.dao.GSUserDAO; import org.geoserver.geofence.core.model.GSUser; -import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.core.dao.search.Search; import java.util.Date; import java.util.List; -import java.util.Set; - -import org.hibernate.Hibernate; import org.springframework.transaction.annotation.Transactional; -import com.googlecode.genericdao.search.ISearch; -import com.googlecode.genericdao.search.Search; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -30,9 +25,12 @@ @Transactional(value = "geofenceTransactionManager") public class GSUserDAOImpl extends BaseDAO implements GSUserDAO { - private static final Logger LOGGER = LogManager.getLogger(GSUserDAOImpl.class); + public GSUserDAOImpl() { + super(GSUser.class); + } + @Override public void persist(GSUser... entities) { @@ -51,14 +49,14 @@ public List findAll() } @Override - public List search(ISearch search) + public List search(Search search) { return super.search(search); } @Override public GSUser getFull(String name) { - Search search = new Search(GSUser.class); + Search search = createSearch(); search.addFilterEqual("name", name); return searchFull(search); } @@ -68,38 +66,17 @@ public GSUser getFull(String name) { */ protected GSUser searchFull(Search search) { search.addFetch("userGroups"); + // When fetching users with multiple groups, the gsusers list id multiplied for the number of groups found search.setDistinct(true); List users = super.search(search); - - // When fetching users with multiple groups, the gsusers list id multiplied for the number of groups found. - // Next there is a workaround to this problem; maybe this: - // search.setDistinct(true); - // Dunno if some annotations in the GSUser definition are wrong, some deeper checks have to be performed. - + switch(users.size()) { case 0: return null; case 1: return users.get(0); default: -// if(users.size() == users.get(0).getGroups().size()) { // normal hibernate behaviour -// if(LOGGER.isDebugEnabled()) { // perform some more consistency tests only when debugging -// for (GSUser user : users) { -// if(user.getId() != users.get(0).getId() || -// user.getGroups().size() != users.get(0).getGroups().size()) { -// LOGGER.error("Inconsistent userlist " + user); -// } -// } -// } -// -// return users.get(0); -// } else { -// LOGGER.error("Too many users in unique search " + search); -// for (GSUser user : users) { -// LOGGER.error(" " + user + " grp:"+user.getGroups().size()); -// } - throw new IllegalStateException("Found more than one user (search:"+search+")"); -// } + throw new IllegalStateException("Found more than one user (search:"+search+")"); } } @@ -122,4 +99,44 @@ public boolean removeById(Long id) return super.removeById(id); } + @Override + public List search(String nameLike, Integer page, Integer entries, boolean fetchGroups) throws IllegalArgumentException { + + if( (page != null && entries == null) || (page ==null && entries != null)) { + throw new IllegalArgumentException("Page and entries params should be declared together."); + } + + Search search = createSearch(); + + if(page != null) { + search.setMaxResults(entries); + search.setPage(page); + } + + if(fetchGroups) { + search.addFetch("userGroups"); + search.setDistinct(true); + } + + search.addSortAsc("name"); + + if (nameLike != null) { + search.addFilterILike("name", nameLike); + } + + return search(search); + } + + + @Override + public long countByNameLike(String nameLike) { + Search searchCriteria = createCountSearch(); + + if (nameLike != null) { + searchCriteria.addFilterILike("name", nameLike); + } + + return count(searchCriteria); + } + } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/LayerDetailsDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/LayerDetailsDAOImpl.java index 10c0df1f..607e7a22 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/LayerDetailsDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/LayerDetailsDAOImpl.java @@ -8,13 +8,13 @@ import java.util.List; import java.util.Set; -import com.googlecode.genericdao.search.ISearch; import org.geoserver.geofence.core.dao.LayerDetailsDAO; import org.geoserver.geofence.core.model.LayerAttribute; import org.geoserver.geofence.core.model.LayerDetails; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.geoserver.geofence.core.dao.search.Search; import org.hibernate.Hibernate; import org.springframework.transaction.annotation.Transactional; @@ -25,10 +25,16 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ @Transactional(value = "geofenceTransactionManager") -public class LayerDetailsDAOImpl extends BaseDAO implements LayerDetailsDAO { +public class LayerDetailsDAOImpl + extends BaseDAO + implements LayerDetailsDAO { private static final Logger LOGGER = LogManager.getLogger(LayerDetailsDAOImpl.class); + public LayerDetailsDAOImpl() { + super(LayerDetails.class); + } + @Override public void persist(LayerDetails... entities) { for (LayerDetails details : entities) { @@ -56,7 +62,7 @@ public List findAll() { } @Override - public List search(ISearch search) { + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/PrioritizableDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/PrioritizableDAOImpl.java index 05ced7d2..4d3dd355 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/PrioritizableDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/PrioritizableDAOImpl.java @@ -5,20 +5,19 @@ package org.geoserver.geofence.core.dao.impl; -import java.util.List; -import javax.persistence.Query; - -import org.geoserver.geofence.core.dao.PrioritizableDAO; import org.geoserver.geofence.core.model.enums.InsertPosition; import org.geoserver.geofence.core.model.Prioritizable; - -import com.googlecode.genericdao.search.Field; -import com.googlecode.genericdao.search.ISearch; -import com.googlecode.genericdao.search.Search; +import org.geoserver.geofence.core.dao.PrioritizableDAO; +import org.geoserver.geofence.core.dao.search.Search; +import org.geoserver.geofence.core.dao.search.Search.Field; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.List; +import javax.persistence.Query; +import org.geoserver.geofence.core.model.Identifiable; + import org.springframework.transaction.annotation.Transactional; /** @@ -27,11 +26,15 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ @Transactional(value = "geofenceTransactionManager") -public abstract class PrioritizableDAOImpl +public abstract class PrioritizableDAOImpl extends BaseDAO implements PrioritizableDAO { private static final Logger LOGGER = LogManager.getLogger(PrioritizableDAOImpl.class); + + protected PrioritizableDAOImpl(Class e) { + super(e); + } protected long persist(Class clazz, T entity, InsertPosition position) { switch(position) { @@ -39,13 +42,13 @@ protected long persist(Class clazz, T entity, InsertPosition position) { // priority is already set break; case FROM_START: - Search search = new Search(clazz); + Search search = createSearch(clazz); search.setFirstResult((int)entity.getPriority()); search.setMaxResults(1); search.addSortAsc("priority"); List list = super._search(search); if(list.isEmpty()) { // no rule found at given position: let's find out why - int count = count(new Search(clazz)); + long count = count(createSearch(clazz)); if(LOGGER.isDebugEnabled()) LOGGER.debug("No rule found at position " + entity.getPriority() + " -- rules count:"+count); @@ -54,7 +57,7 @@ protected long persist(Class clazz, T entity, InsertPosition position) { LOGGER.debug("Inserting first rule"); entity.setPriority(1); // this is the only rule so far, let's put in an arbitrary value } else { // some rules in, the requested postion is at bottom - Search s1 = new Search(clazz); + Search s1 = createSearch(clazz); s1.addField("priority", Field.OP_MAX); long maxPri = (Long)searchUnique(s1); entity.setPriority(maxPri+1); @@ -77,7 +80,7 @@ protected long persist(Class clazz, T entity, InsertPosition position) { case FROM_END: // 0 based: if set to 0, this rule will go in last position long posFromEnd = entity.getPriority(); - int count = count(new Search(clazz)); + long count = count(createSearch(clazz)); if(count == 0) { if(LOGGER.isDebugEnabled()) LOGGER.debug("Inserting first rule"); @@ -96,7 +99,7 @@ protected long persist(Class clazz, T entity, InsertPosition position) { } - Search searchEnd = new Search(clazz); + Search searchEnd = createSearch(clazz); searchEnd.setFirstResult((int)posFromStart); searchEnd.setMaxResults(1); searchEnd.addSortAsc("priority"); @@ -144,7 +147,7 @@ protected int shift(Classclazz, long priorityStart, long offset) { throw new IllegalArgumentException("Positive offset required"); } - Search search = new Search(clazz); + Search search = createSearch(clazz); search.addFilterGreaterOrEqual("priority", priorityStart); search.addFilterLessThan("priority", priorityStart + offset); if ( super.count(search) == 0 ) { @@ -176,7 +179,7 @@ public void swap(long id1, long id2) { } @Override - public List search(ISearch search) { + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleDAOImpl.java index e07adf3c..853353a0 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleDAOImpl.java @@ -7,12 +7,8 @@ import java.util.List; - -import com.googlecode.genericdao.search.ISearch; -import com.googlecode.genericdao.search.Search; - import org.geoserver.geofence.core.dao.RuleDAO; -import static org.geoserver.geofence.core.dao.util.SearchUtil.*; +import static org.geoserver.geofence.core.dao.search.SearchUtil.*; import org.geoserver.geofence.core.model.Rule; import org.geoserver.geofence.core.model.enums.GrantType; @@ -20,6 +16,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.geoserver.geofence.core.dao.DuplicateKeyException; +import org.geoserver.geofence.core.dao.search.Search; import org.springframework.transaction.annotation.Transactional; @@ -33,6 +30,10 @@ public class RuleDAOImpl extends PrioritizableDAOImpl implements RuleDAO { private static final Logger LOGGER = LogManager.getLogger(RuleDAOImpl.class); + public RuleDAOImpl() { + super(Rule.class); + } + @Override public void persist(Rule... entities) throws DuplicateKeyException { @@ -82,7 +83,7 @@ public void persistInternal(Rule entity) { protected Search getDupSearch(Rule rule) { - Search search = new Search(Rule.class); + Search search = createSearch(); addSearchField(search, "username", rule.getUsername()); addSearchField(search, "rolename", rule.getRolename()); addSearchField(search, "instance", rule.getInstance()); @@ -103,7 +104,7 @@ public List findAll() { } @Override - public List search(ISearch search) { + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleLimitsDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleLimitsDAOImpl.java index 02790df8..b225b6a7 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleLimitsDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/RuleLimitsDAOImpl.java @@ -5,16 +5,15 @@ package org.geoserver.geofence.core.dao.impl; -import java.util.List; - -import com.googlecode.genericdao.search.ISearch; - import org.geoserver.geofence.core.dao.RuleLimitsDAO; +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.core.model.RuleLimits; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.List; + import org.springframework.transaction.annotation.Transactional; @@ -24,11 +23,16 @@ * @author Emanuele Tajariol (etj at geo-solutions.it) */ @Transactional(value = "geofenceTransactionManager") -public class RuleLimitsDAOImpl extends BaseDAO implements RuleLimitsDAO +public class RuleLimitsDAOImpl // + extends BaseDAO // + implements RuleLimitsDAO { - private static final Logger LOGGER = LogManager.getLogger(RuleLimitsDAOImpl.class); + public RuleLimitsDAOImpl() { + super(RuleLimits.class); + } + @Override public void persist(RuleLimits... entities) { @@ -42,7 +46,7 @@ public List findAll() } @Override - public List search(ISearch search) + public List search(Search search) { return super.search(search); } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/UserGroupDAOImpl.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/UserGroupDAOImpl.java index 9805aa26..92856891 100644 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/UserGroupDAOImpl.java +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/impl/UserGroupDAOImpl.java @@ -2,23 +2,21 @@ * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ - package org.geoserver.geofence.core.dao.impl; import java.util.Date; import java.util.List; -import com.googlecode.genericdao.search.ISearch; - import org.geoserver.geofence.core.dao.UserGroupDAO; import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.core.dao.search.Search; +import org.geoserver.geofence.core.model.GSUser; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.transaction.annotation.Transactional; - /** * Public implementation of the UserGroupDAO interface * @@ -26,17 +24,19 @@ */ @Transactional(value = "geofenceTransactionManager") public class UserGroupDAOImpl extends BaseDAO - // extends GenericDAOImpl - implements UserGroupDAO -{ + // extends GenericDAOImpl + implements UserGroupDAO { + private static final Logger LOGGER = LogManager.getLogger(UserGroupDAOImpl.class); + public UserGroupDAOImpl() { + super(UserGroup.class); + } + @Override - public void persist(UserGroup... entities) - { + public void persist(UserGroup... entities) { Date now = new Date(); - for (UserGroup e : entities) - { + for (UserGroup e : entities) { e.setDateCreation(now); } @@ -44,35 +44,66 @@ public void persist(UserGroup... entities) } @Override - public List findAll() - { + public List findAll() { return super.findAll(); } @Override - public List search(ISearch search) - { - return super.search(search); - } - - @Override - public UserGroup merge(UserGroup entity) - { + public UserGroup merge(UserGroup entity) { return super.merge(entity); } @Override - public boolean remove(UserGroup entity) - { + public boolean remove(UserGroup entity) { return super.remove(entity); } @Override - public boolean removeById(Long id) - { + public boolean removeById(Long id) { return super.removeById(id); } - // ========================================================================== + @Override + public UserGroup get(String name) { + Search search = createSearch(); + search.addFilterEqual("name", name); + return (UserGroup)searchUnique(search); + } + + @Override + public List search(String nameLike, Integer page, Integer entries) throws IllegalArgumentException { + + if( (page != null && entries == null) || (page ==null && entries != null)) { + throw new IllegalArgumentException("Page and entries params should be declared together."); + } + + Search search = createSearch(); + + if(page != null) { + search.setMaxResults(entries); + search.setPage(page); + } + + search.addSortAsc("name"); + + if (nameLike != null) { + search.addFilterILike("name", nameLike); + } + + return search(search); + } + + + @Override + public long countByNameLike(String nameLike) { + Search searchCriteria = createCountSearch(); + + if (nameLike != null) { + searchCriteria.addFilterILike("name", nameLike); + } + + return count(searchCriteria); + } + } diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/Search.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/Search.java new file mode 100644 index 00000000..8ac4b3ab --- /dev/null +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/Search.java @@ -0,0 +1,244 @@ +/* + */ +package org.geoserver.geofence.core.dao.search; + +import java.util.ArrayList; +import java.util.List; +import javax.persistence.EntityManager; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Fetch; +import javax.persistence.criteria.Join; +import javax.persistence.criteria.JoinType; +import javax.persistence.criteria.Order; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +/** + * + * @author Emanuele Tajariol + */ +public class Search { + + private final Class outType; + private final Class rootClass; + private final EntityManager em; + private final CriteriaBuilder cb; + private CriteriaQuery q; + private Root root; + private List whereClauses = new ArrayList<>() ; + private List orderBy = new ArrayList<>() ; + + private Integer firstResult = null; + private Integer maxResults = null; + private Integer page = null; + + public static class JoinInfo{ + Join join; + String field; + + public JoinInfo(Join join, String field) { + this.join = join; + this.field = field; + } + + public String getField() { + return field; + } + + } + + protected Search(EntityManager em, Class resultType, Class rootClass) { + this.em = em; + this.outType = resultType; + this.rootClass = rootClass; + + cb = em.getCriteriaBuilder(); + q = cb.createQuery(resultType); + + root = q.from(rootClass); + } + + protected Search(EntityManager em, Class resultType) { + this(em, resultType, resultType); + } + + public void addField(String field, Field op) { + + if(op == Field.OP_MAX) { + q.select(cb.max(root.get(field))); + } else { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + } + + public static enum Field { + OP_MAX + } + + + + public void setFirstResult(int i) { + firstResult = i; + } + + public void setMaxResults(int i) { + maxResults = i; + } + + public void setPage(int page) { + this.page = page; + } + + public void addSortAsc(String field) { + orderBy.add(cb.asc(root.get(field))); +// c.addOrder(Order.asc(field)); + } + + public void setDistinct(boolean b) { + q.distinct(b); + } + + public JoinInfo addJoin(String field) { + return new JoinInfo(root.join(field, JoinType.LEFT), field); + } + + public Fetch addFetch(String field) { + return root.fetch(field, JoinType.LEFT); + } + + public Fetch addFetch(String field, Class type) { + return root.fetch(field, JoinType.LEFT); +// c.setFetchMode(field, FetchMode.EAGER); + } + + public void addFilterNull(String field) { + whereClauses.add(cb.isNull(root.get(field))); + } + + public void addFilterNull(JoinInfo j, String field) { + whereClauses.add(cb.isNull(j.join.get(field))); + } + + public void addFilterEqual(JoinInfo j, String field, Object o) { + whereClauses.add(cb.equal(j.join.get(field), o)); +// c.add(Restrictions.eq(field, o)); + } + + public void addFilterEqual(String field, Object o) { + whereClauses.add(cb.equal(root.get(field), o)); +// c.add(Restrictions.eq(field, o)); + } + + public void addFilterGreaterOrEqual(String field, Long value) { + whereClauses.add(cb.ge(root.get(field), value)); + +// c.add(Restrictions.ge(field, value)); + } + + public void addFilterLessThan(String field, Long value) { + whereClauses.add(cb.lt(root.get(field), value)); +// c.add(Restrictions.lt(field, value)); + } + + public void addFilterILike(String name, String like) { + whereClauses.add( + cb.like( + cb.lower(root.get(name)), + cb.lower(cb.literal("%" + like + "%") + ))); +// c.add(Restrictions.ilike(name, like)); + } + + public Predicate isNull(String field) { + return cb.isNull(root.get(field)); + } + + public Predicate isEqual(String field, Object val) { + return cb.equal(root.get(field), val); + } + + public Predicate isEqual(JoinInfo j, String field, Object val) { + return cb.equal(j.join.get(field), val); + } + + public Predicate isGE(String field, Number val) { + return cb.ge(root.get(field), val); + } + + + public void addFilterOr(Predicate f1, Predicate f2) { + whereClauses.add(cb.or(f1, f2)); +// c.add(Restrictions.or(f1.getCriterion(),f2.getCriterion())); + } + +// public void addFilter(Predicate f) { +// c.add(f.getCriterion()); +// } + + + + private void applyWhere(CriteriaQuery q) { + + switch(whereClauses.size()) { + case 0: + break; + case 1: + q.where(whereClauses.get(0)); + break; + default: + q.where(whereClauses.toArray(new Predicate[whereClauses.size()])); + + } + } + + public TypedQuery getQuery() { + applyWhere(q); + + if(! orderBy.isEmpty()) { + q.orderBy(orderBy); + } + + if(q.getSelection() == null) { + q.select(root); + } + + TypedQuery query = em.createQuery(q); + + applyPagination(query); + + return query; + } + + private void applyPagination(TypedQuery query) throws IllegalStateException { + if(firstResult != null) { + query.setFirstResult(firstResult); + } + if(maxResults != null) { + query.setMaxResults(maxResults); + } + if(page != null) { + if(maxResults == null) { + throw new IllegalStateException("Page set without maxresults"); + } + query.setFirstResult(page * maxResults); + } + } + +// public TypedQuery _getCountQuery() { +// CriteriaQuery cq = cb.createQuery(Long.class); +// cq.select(cb.count(cq.from(outType))); +//// applyWhere(cq); +// return em.createQuery(cq); +// } + + public TypedQuery getCountQuery() { + applyWhere(q); +// CriteriaQuery cq = cb.createQuery(Long.class); + q.select(cb.count(root)); + return em.createQuery(q); + } + + +} \ No newline at end of file diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/SearchUtil.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/SearchUtil.java new file mode 100644 index 00000000..f62c436a --- /dev/null +++ b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/search/SearchUtil.java @@ -0,0 +1,63 @@ +/* (c) 2014 Open Source Geospatial Foundation - all rights reserved + * This code is licensed under the GPL 2.0 license, available at the root + * application directory. + */ + +package org.geoserver.geofence.core.dao.search; + +import org.geoserver.geofence.core.dao.search.Search.JoinInfo; +import org.geoserver.geofence.core.model.IPAddressRange; + +/** + * + * @author ETj (etj at geo-solutions.it) + */ +public class SearchUtil { + + /** + * Adds a search field for finding the precise addressrange instance. + * Does NOT search for an address in a range. + */ + public static void addAddressRangeSearch(Search search, IPAddressRange addressRange) { + +// search.addFetch("addressRange", IPAddressRange.class); + JoinInfo ar = search.addJoin("addressRange"); + + if(addressRange != null ) { + // it's embedded + addSearchField(search, ar, "low", addressRange.getLow()); + addSearchField(search, ar, "high", addressRange.getHigh()); + addSearchField(search, ar, "size", addressRange.getSize()); + } else { + addSearchField(search, ar, "low", null); + addSearchField(search, ar, "high", null); + addSearchField(search, ar, "size", null); + } +// if(addressRange != null ) { +// // it's embedded +// addSearchField(search, "addressRange.low", addressRange.getLow()); +// addSearchField(search, "addressRange.high", addressRange.getHigh()); +// addSearchField(search, "addressRange.size", addressRange.getSize()); +// } else { +// addSearchField(search, "addressRange.low", null); +// addSearchField(search, "addressRange.high", null); +// addSearchField(search, "addressRange.size", null); +// } + } + + public static void addSearchField(Search search, String field, Object o) { + if ( o == null ) { + search.addFilterNull(field); + } else { + search.addFilterEqual(field, o); + } + } + + public static void addSearchField(Search search, JoinInfo j, String field, Object o) { + if ( o == null ) { + search.addFilterNull(j, field); + } else { + search.addFilterEqual(j, field, o); + } + } +} diff --git a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/util/SearchUtil.java b/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/util/SearchUtil.java deleted file mode 100644 index 4476b8d5..00000000 --- a/src/services/core/persistence/src/main/java/org/geoserver/geofence/core/dao/util/SearchUtil.java +++ /dev/null @@ -1,41 +0,0 @@ -/* (c) 2014 Open Source Geospatial Foundation - all rights reserved - * This code is licensed under the GPL 2.0 license, available at the root - * application directory. - */ - -package org.geoserver.geofence.core.dao.util; - -import com.googlecode.genericdao.search.Search; -import org.geoserver.geofence.core.model.IPAddressRange; - -/** - * - * @author ETj (etj at geo-solutions.it) - */ -public class SearchUtil { - - /** - * Adds a search field for finding the precise addressrange instance. - * Does NOT search for an address in a range. - */ - public static void addAddressRangeSearch(Search search, IPAddressRange addressRange) { - if(addressRange != null ) { - // it's embedded - addSearchField(search, "addressRange.low", addressRange.getLow()); - addSearchField(search, "addressRange.high", addressRange.getHigh()); - addSearchField(search, "addressRange.size", addressRange.getSize()); - } else { - addSearchField(search, "addressRange.low", null); - addSearchField(search, "addressRange.high", null); - addSearchField(search, "addressRange.size", null); - } - } - - public static void addSearchField(Search search, String field, Object o) { - if ( o == null ) { - search.addFilterNull(field); - } else { - search.addFilterEqual(field, o); - } - } -} diff --git a/src/services/core/persistence/src/main/resources/applicationContext-geofenceDatasource.xml b/src/services/core/persistence/src/main/resources/applicationContext-geofenceDatasource.xml index 992f26ec..5bc8744f 100644 --- a/src/services/core/persistence/src/main/resources/applicationContext-geofenceDatasource.xml +++ b/src/services/core/persistence/src/main/resources/applicationContext-geofenceDatasource.xml @@ -81,26 +81,28 @@ - + + + - + - + - + @@ -109,6 +111,24 @@ + + + + + diff --git a/src/services/core/persistence/src/main/resources/applicationContext.xml b/src/services/core/persistence/src/main/resources/applicationContext.xml index 5571472b..d521eaec 100644 --- a/src/services/core/persistence/src/main/resources/applicationContext.xml +++ b/src/services/core/persistence/src/main/resources/applicationContext.xml @@ -31,49 +31,51 @@ (i.e. EntityManagerFactory). We're wiring these to the Generic DAOs using an autowire annotation in BaseDAO. --> - + - - + --> + + - + - + - + - + - + - + - + - + diff --git a/src/services/core/persistence/src/main/resources/geofence-ehcache.xml b/src/services/core/persistence/src/main/resources/geofence-ehcache.xml index 89da4aff..6a2ad13f 100644 --- a/src/services/core/persistence/src/main/resources/geofence-ehcache.xml +++ b/src/services/core/persistence/src/main/resources/geofence-ehcache.xml @@ -17,7 +17,15 @@ diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> - + + diff --git a/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/BaseDAOTest.java b/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/BaseDAOTest.java index 83ea4a7c..363a2dfa 100644 --- a/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/BaseDAOTest.java +++ b/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/BaseDAOTest.java @@ -95,19 +95,19 @@ protected void removeAllUsers() { List list = userDAO.findAll(); for (GSUser item : list) { LOGGER.info("Removing " + item); - boolean ret = userDAO.remove(item); - assertTrue("User not removed", ret); + userDAO.remove(item); + assertNull("User not removed", userDAO.find(item.getId())); } - assertEquals("Users have not been properly deleted", 0, userDAO.count(null)); + assertEquals("Users have not been properly deleted", 0, userDAO.countByNameLike(null)); } protected void removeAllGRUsers() { List list = gfUserDAO.findAll(); for (GFUser item : list) { LOGGER.info("Removing " + item); - boolean ret = gfUserDAO.remove(item); - assertTrue("User not removed", ret); + gfUserDAO.remove(item); + assertNull("User not removed", gfUserDAO.find(item.getId())); } assertEquals("GRUsers have not been properly deleted", 0, gfUserDAO.count(null)); @@ -117,8 +117,8 @@ protected void removeAllRules() { List list = ruleDAO.findAll(); for (Rule item : list) { LOGGER.info("Removing " + item); - boolean ret = ruleDAO.remove(item); - assertTrue("Rule not removed", ret); + ruleDAO.remove(item); + assertNull("Rule not removed", ruleDAO.find(item.getId())); } assertEquals("Rules have not been properly deleted", 0, ruleDAO.count(null)); @@ -128,11 +128,11 @@ protected void removeAllUserGroups() { List list = userGroupDAO.findAll(); for (UserGroup item : list) { LOGGER.info("Removing " + item); - boolean ret = userGroupDAO.remove(item); - assertTrue("UserGroup not removed", ret); + userGroupDAO.remove(item); + assertNull("UserGroup not removed", userGroupDAO.find(item.getId())); } - assertEquals("UserGroups have not been properly deleted", 0, userGroupDAO.count(null)); + assertEquals("UserGroups have not been properly deleted", 0, userGroupDAO.countByNameLike(null)); } protected GSUser createUser(String base, UserGroup userGroup) { diff --git a/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/RuleDAOTest.java b/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/RuleDAOTest.java index a52a4213..0baf0c55 100644 --- a/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/RuleDAOTest.java +++ b/src/services/core/persistence/src/test/java/org/geoserver/geofence/core/dao/RuleDAOTest.java @@ -5,16 +5,16 @@ package org.geoserver.geofence.core.dao; -import com.googlecode.genericdao.search.Search; import org.geoserver.geofence.core.model.Rule; +import org.geoserver.geofence.core.dao.search.Search; +import static org.geoserver.geofence.core.dao.BaseDAOTest.ruleDAO; import org.geoserver.geofence.core.model.LayerAttribute; import org.geoserver.geofence.core.model.LayerDetails; import org.geoserver.geofence.core.model.IPAddressRange; import org.geoserver.geofence.core.model.GSUser; import org.geoserver.geofence.core.model.enums.SpatialFilterType; -import org.locationtech.jts.geom.GeometryFactory; import org.locationtech.jts.geom.MultiPolygon; -import org.geoserver.geofence.core.dao.util.SearchUtil; +import org.geoserver.geofence.core.dao.search.SearchUtil; import org.geoserver.geofence.core.model.enums.AccessType; import org.geoserver.geofence.core.model.enums.GrantType; import org.geoserver.geofence.core.model.enums.InsertPosition; @@ -280,7 +280,7 @@ public void testRuleDetails() { final Long id; { - Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); // ruleAdminService.insert(r1); ruleDAO.persist(r1); id = r1.getId(); @@ -444,8 +444,8 @@ public void setDetails(Long ruleId, LayerDetails details) { public void testDupRuleTest() throws Exception { { - Rule rule1 = new Rule(10, null, null, null, null, "s", null, null, null, GrantType.ALLOW); - Rule rule2 = new Rule(10, null, null, null, null, "s", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(10, null, null, null, null, "s", null, null, null, null, GrantType.ALLOW); + Rule rule2 = new Rule(10, null, null, null, null, "s", null, null, null, null, GrantType.ALLOW); ruleDAO.persist(rule1); @@ -462,8 +462,8 @@ public void testDupRuleTest() throws Exception { @Test public void testDupRule2Test() throws Exception { { - Rule rule1 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, GrantType.ALLOW); - Rule rule2 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, null, GrantType.ALLOW); + Rule rule2 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, null, GrantType.ALLOW); ruleDAO.persist(rule1); @@ -479,8 +479,8 @@ public void testDupRule2Test() throws Exception { @Test public void testDupRule3Test() throws Exception { { - Rule rule1 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.5/32"), "s", null, null, null, GrantType.ALLOW); - Rule rule2 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.5/32"), "s", null, null, null, null, GrantType.ALLOW); + Rule rule2 = new Rule(10, null, null, null, new IPAddressRange("1.2.3.4/32"), "s", null, null, null, null, GrantType.ALLOW); ruleDAO.persist(rule1); @@ -496,12 +496,12 @@ public void testDupRule3Test() throws Exception { @Test public void testShift() { - assertEquals(0, ruleDAO.count(new Search(Rule.class))); + assertEquals(0, ruleDAO.count(ruleDAO.createSearch())); - Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, null, null, null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, null, null, null, "s3", "r3", "w3", "l3", GrantType.ALLOW); - Rule r4 = new Rule(40, null, null, null, null, "s4", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, null, null, null, "s2", "r2", null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, null, null, null, "s3", "r3", null, "w3", "l3", GrantType.ALLOW); + Rule r4 = new Rule(40, null, null, null, null, "s4", "r3", null, "w3", "l3", GrantType.ALLOW); ruleDAO.persist(r1); ruleDAO.persist(r2); @@ -511,7 +511,7 @@ public void testShift() { int n = ruleDAO.shift(20, 5); assertEquals(3, n); - Search s = new Search(Rule.class); + Search s = ruleDAO.createSearch(); s.addFilterEqual("service", "s3"); List loaded = ruleDAO.search(s); assertEquals(1, loaded.size()); @@ -524,11 +524,11 @@ public void testShift() { @Test public void testSwap() { - assertEquals(0, ruleDAO.count(new Search(Rule.class))); + assertEquals(0, ruleDAO.count(ruleDAO.createSearch())); - Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, null, null, null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, null, null, null, "s3", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, null, null, null, "s2", "r2", null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, null, null, null, "s3", "r3", null, "w3", "l3", GrantType.ALLOW); ruleDAO.persist(r1); ruleDAO.persist(r2); @@ -546,8 +546,8 @@ public void testPersistRulePosition() throws Exception { long id1; { - assertEquals(0, ruleDAO.count(new Search(Rule.class))); - Rule rule1 = new Rule(1000, null, null, null, null, "s", null, null, null, GrantType.ALLOW); + assertEquals(0, ruleDAO.count(ruleDAO.createCountSearch())); + Rule rule1 = new Rule(1000, null, null, null, null, "s", null, null, null, null, GrantType.ALLOW); ruleDAO.persist(rule1, InsertPosition.FROM_START); id1 = rule1.getId(); } @@ -559,30 +559,30 @@ public void testPersistRulePosition() throws Exception { } - ruleDAO.persist(new Rule(10, null, null, null, null, "s10", null, null, null, GrantType.ALLOW)); - ruleDAO.persist(new Rule(20, null, null, null, null, "s20", null, null, null, GrantType.ALLOW)); + ruleDAO.persist(new Rule(10, null, null, null, null, "s10", null, null, null, null, GrantType.ALLOW)); + ruleDAO.persist(new Rule(20, null, null, null, null, "s20", null, null, null, null, GrantType.ALLOW)); { - assertEquals(3, ruleDAO.count(new Search(Rule.class))); - Rule rule1 = new Rule(1000, null, null, null, null, "sZ", null, null, null, GrantType.ALLOW); + assertEquals(3, ruleDAO.count(ruleDAO.createCountSearch())); + Rule rule1 = new Rule(1000, null, null, null, null, "sZ", null, null, null, null, GrantType.ALLOW); long pri = ruleDAO.persist(rule1, InsertPosition.FROM_START); assertEquals(21, pri); } { - Rule rule1 = new Rule(1, null, null, null, null, "second", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(1, null, null, null, null, "second", null, null, null, null, GrantType.ALLOW); long pri = ruleDAO.persist(rule1, InsertPosition.FROM_START); assertEquals(10, pri); } { - Rule rule1 = new Rule(0, null, null, null, null, "last", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(0, null, null, null, null, "last", null, null, null, null, GrantType.ALLOW); long pri = ruleDAO.persist(rule1, InsertPosition.FROM_END); assertEquals(23, pri); } { - Rule rule1 = new Rule(1, null, null, null, null, "last2", null, null, null, GrantType.ALLOW); + Rule rule1 = new Rule(1, null, null, null, null, "last2", null, null, null, null, GrantType.ALLOW); long pri = ruleDAO.persist(rule1, InsertPosition.FROM_END); assertEquals(23, pri); } @@ -623,7 +623,7 @@ public void testIPRangeTest() throws Exception { //test search { - Search s = new Search(Rule.class); + Search s = ruleDAO.createSearch(); SearchUtil.addAddressRangeSearch(s, new IPAddressRange("10.11.0.0/16")); diff --git a/src/services/core/pom.xml b/src/services/core/pom.xml index 36c6c968..b577511b 100644 --- a/src/services/core/pom.xml +++ b/src/services/core/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/core/services-api/pom.xml b/src/services/core/services-api/pom.xml index 68ec1ef7..162fbb28 100644 --- a/src/services/core/services-api/pom.xml +++ b/src/services/core/services-api/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/UserAdminService.java b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/UserAdminService.java index d30ca753..2e032867 100644 --- a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/UserAdminService.java +++ b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/UserAdminService.java @@ -40,7 +40,6 @@ public interface UserAdminService extends GetProviderService */ @Override GSUser get(long id) throws NotFoundServiceEx; - GSUser get(String name) throws NotFoundServiceEx; GSUser getFull(String name) throws NotFoundServiceEx; long getCount(String nameLike); diff --git a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/AccessInfo.java b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/AccessInfo.java index 07e3d7c4..cb56d410 100644 --- a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/AccessInfo.java +++ b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/AccessInfo.java @@ -158,7 +158,7 @@ public String toString() { sb.append(" cqlW:").append(cqlFilterWrite); } if (areaWkt != null) { - sb.append(" areaWkt:defined"); + sb.append(" areaWkt:").append(areaWkt.substring(0, 8)).append("..."); } if (catalogMode != null) { sb.append(" cmode:").append(catalogMode); diff --git a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/RuleFilter.java b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/RuleFilter.java index 19febb45..11089dd2 100644 --- a/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/RuleFilter.java +++ b/src/services/core/services-api/src/main/java/org/geoserver/geofence/services/dto/RuleFilter.java @@ -9,8 +9,6 @@ import java.io.Serializable; import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; import java.util.stream.Collectors; @@ -121,32 +119,6 @@ public RuleFilter(SpecialFilterType type, boolean includeDefault) { layer.setIncludeDefault(includeDefault); } - /** - * Creates a RuleFilter by heuristically converting special string values into Fitler behaviour:

  • a null value will - * match only with null
  • a '*' value will match everything (no filter condition on that given field)
  • any - * other string will match literally
- * - * @deprecated Please use plain setters if you want to handle by hand special values or filter conditions. - */ - public RuleFilter(String userName, String groupName, String instanceName, - String sourceAddress, - String service, String request, String subfield, - String workspace, String layer) { - this(SpecialFilterType.DEFAULT); - - - this.user.setHeuristically(userName); - this.role.setHeuristically(groupName); - this.instance.setHeuristically(instanceName); - this.sourceAddress.setHeuristically(sourceAddress); - - this.service.setHeuristically(service); - this.request.setHeuristically(request); - this.subfield.setHeuristically(subfield); - this.workspace.setHeuristically(workspace); - this.layer.setHeuristically(layer); - } - public RuleFilter(RuleFilter source) { try { diff --git a/src/services/core/services-impl/pom.xml b/src/services/core/services-impl/pom.xml index b45dd89b..6024bf18 100644 --- a/src/services/core/services-impl/pom.xml +++ b/src/services/core/services-impl/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -79,9 +79,9 @@ - + - dom4j + org.dom4j dom4j @@ -116,12 +116,12 @@ org.locationtech.jts jts-core
- + @@ -133,10 +133,11 @@
--> - org.hibernatespatial - hibernate-spatial-h2-geodb + org.hibernate + hibernate-spatial test + org.opengeo geodb @@ -144,29 +145,7 @@ test - - - com.googlecode.genericdao - dao - - - org.slf4j - slf4j-api - - - - - javax.persistence - persistence-api - - - - - com.googlecode.genericdao - search-jpa-hibernate - - - - + + false + + + + diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AdminRuleAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AdminRuleAdminServiceImpl.java index 9f75551b..056e5dc5 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AdminRuleAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AdminRuleAdminServiceImpl.java @@ -8,16 +8,16 @@ import java.util.ArrayList; import java.util.List; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; -import org.geoserver.geofence.core.dao.AdminRuleDAO; import org.geoserver.geofence.core.model.AdminRule; import org.geoserver.geofence.core.model.enums.InsertPosition; +import org.geoserver.geofence.core.dao.AdminRuleDAO; +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.services.dto.RuleFilter; import org.geoserver.geofence.services.dto.ShortAdminRule; import org.geoserver.geofence.services.exception.BadRequestServiceEx; import org.geoserver.geofence.services.exception.NotFoundServiceEx; + import static org.geoserver.geofence.services.util.FilterUtils.addCriteria; import static org.geoserver.geofence.services.util.FilterUtils.addFixedCriteria; import static org.geoserver.geofence.services.util.FilterUtils.addFixedStringCriteria; @@ -109,8 +109,8 @@ public boolean delete(long id) throws NotFoundServiceEx { @Override public void deleteRulesByUser(String username) throws NotFoundServiceEx { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addFilter(Filter.equal("username", username)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("username", username); List list = ruleDAO.search(searchCriteria); if(LOGGER.isInfoEnabled()) @@ -124,8 +124,8 @@ public void deleteRulesByUser(String username) throws NotFoundServiceEx { @Override public void deleteRulesByRole(String rolename) throws NotFoundServiceEx { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addFilter(Filter.equal("rolename", rolename)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("rolename", rolename); List list = ruleDAO.search(searchCriteria); for (AdminRule rule : list) { @@ -137,8 +137,8 @@ public void deleteRulesByRole(String rolename) throws NotFoundServiceEx { @Override public void deleteRulesByInstance(long instanceId) throws NotFoundServiceEx { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addFilter(Filter.equal("instance.id", instanceId)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("instance.id", instanceId); List list = ruleDAO.search(searchCriteria); for (AdminRule rule : list) { @@ -179,18 +179,18 @@ public ShortAdminRule getRule(RuleFilter filter) throws BadRequestServiceEx { @Override public List getRulesByPriority(long priority, Integer page, Integer entries) { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addFilter(Filter.greaterOrEqual("priority", priority)); - searchCriteria.addSortAsc("priority"); - addPagingConstraints(searchCriteria, page, entries); - List found = ruleDAO.search(searchCriteria); + Search search = ruleDAO.createSearch(); + search.addFilterGreaterOrEqual("priority", priority); + search.addSortAsc("priority"); + addPagingConstraints(search, page, entries); + List found = ruleDAO.search(search); return convertToShortList(found); } @Override public ShortAdminRule getRuleByPriority(long priority) throws BadRequestServiceEx { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addFilter(Filter.equal("priority", priority)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("priority", priority); List found = ruleDAO.search(searchCriteria); if(found.isEmpty()) return null; @@ -210,7 +210,8 @@ public List getListFull(RuleFilter filter, Integer page, Integer entr } protected Search buildSearch(Integer page, Integer entries, RuleFilter filter) throws BadRequestServiceEx { - Search searchCriteria = buildRuleSearch(filter); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria = buildRuleSearch(searchCriteria, filter); addPagingConstraints(searchCriteria, page, entries); searchCriteria.addSortAsc("priority"); return searchCriteria; @@ -225,8 +226,8 @@ public long getCountAll() { public long count(RuleFilter filter) { // if(LOGGER.isDebugEnabled()) // LOGGER.debug("Counting rules: " + filter); - - Search searchCriteria = buildRuleSearch(filter); + Search searchCriteria = ruleDAO.createCountSearch(); + searchCriteria = buildRuleSearch(searchCriteria, filter); // if(LOGGER.isDebugEnabled()) // LOGGER.debug("Counting rules: " + searchCriteria); return ruleDAO.count(searchCriteria); @@ -235,36 +236,34 @@ public long count(RuleFilter filter) { // ========================================================================= // Search stuff - private Search buildRuleSearch(RuleFilter filter) { - Search searchCriteria = new Search(AdminRule.class); + private Search buildRuleSearch(Search search, RuleFilter filter) { + if(filter != null) { - addStringCriteria(searchCriteria, "username", filter.getUser()); - addStringCriteria(searchCriteria, "rolename", filter.getRole()); - addCriteria(searchCriteria, "instance", filter.getInstance()); + addStringCriteria(search, "username", filter.getUser()); + addStringCriteria(search, "rolename", filter.getRole()); + addCriteria(search, search.addJoin("instance"), filter.getInstance()); - addStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); + addStringCriteria(search, "workspace", filter.getWorkspace()); } - return searchCriteria; + return search; } - - //========================================================================= private Search buildFixedRuleSearch(RuleFilter filter) { - Search searchCriteria = new Search(AdminRule.class); + Search search = ruleDAO.createSearch(); if(filter != null) { - addFixedStringCriteria(searchCriteria, "username", filter.getUser()); - addFixedStringCriteria(searchCriteria, "rolename", filter.getRole()); - addFixedCriteria(searchCriteria, "instance", filter.getInstance()); + addFixedStringCriteria(search, "username", filter.getUser()); + addFixedStringCriteria(search, "rolename", filter.getRole()); + addFixedCriteria(search, search.addJoin("instance"), filter.getInstance()); - addFixedStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); + addFixedStringCriteria(search, "workspace", filter.getWorkspace()); } - return searchCriteria; + return search; } // ========================================================================== diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AuthorizationServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AuthorizationServiceImpl.java index 7013ec99..8c684424 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AuthorizationServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/AuthorizationServiceImpl.java @@ -5,17 +5,14 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Search; +import org.geoserver.geofence.core.model.GSUser; +import org.geoserver.geofence.core.dao.GSUserDAO; +import org.geoserver.geofence.services.dto.AuthUser; -import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.core.dao.GSUserDAO; -import org.geoserver.geofence.core.model.GSUser; -import org.geoserver.geofence.services.dto.AuthUser; - /** * * @author ETj (etj at geo-solutions.it) @@ -28,7 +25,7 @@ public class AuthorizationServiceImpl implements AuthorizationService { @Override public AuthUser authorize(String username, String password) { - GSUser user = getUserByName(username); + GSUser user = userDAO.getFull(username); if(user == null) { LOGGER.debug("User not found " + username); return null; @@ -41,16 +38,6 @@ public AuthUser authorize(String username, String password) { return new AuthUser(username, user.isAdmin() ? AuthUser.Role.ADMIN : AuthUser.Role.USER); } - private GSUser getUserByName(String userName) { - Search search = new Search(GSUser.class); - search.addFilterEqual("name", userName); - List users = userDAO.search(search); - if(users.size() > 1) - throw new IllegalStateException("Found more than one user with name '"+userName+"'"); - - return users.isEmpty() ? null : users.get(0); - } - public void setGsUserDAO(GSUserDAO userDAO) { this.userDAO = userDAO; } diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/GFUserAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/GFUserAdminServiceImpl.java index b3252d63..4f217461 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/GFUserAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/GFUserAdminServiceImpl.java @@ -5,8 +5,12 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Search; +import org.geoserver.geofence.core.dao.GFUserDAO; +import org.geoserver.geofence.core.dao.search.Search; +import org.geoserver.geofence.core.model.GFUser; import org.geoserver.geofence.services.dto.ShortUser; +import org.geoserver.geofence.services.exception.BadRequestServiceEx; +import org.geoserver.geofence.services.exception.NotFoundServiceEx; import java.util.ArrayList; import java.util.List; @@ -14,11 +18,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.core.dao.GFUserDAO; -import org.geoserver.geofence.core.model.GFUser; -import org.geoserver.geofence.services.exception.BadRequestServiceEx; -import org.geoserver.geofence.services.exception.NotFoundServiceEx; - /** * * @author ETj (etj at geo-solutions.it) @@ -60,7 +59,7 @@ public GFUser get(long id) throws NotFoundServiceEx { @Override public GFUser get(String name) { - Search search = new Search(GFUser.class); + Search search = gfUserDAO.createSearch(); search.addFilterEqual("name", name); List users = gfUserDAO.search(search); @@ -87,7 +86,7 @@ public List getFullList(String nameLike, Integer page, Integer entries) throw new BadRequestServiceEx("Page and entries params should be declared together."); } - Search searchCriteria = new Search(GFUser.class); + Search searchCriteria = gfUserDAO.createSearch(); if(page != null) { searchCriteria.setMaxResults(entries); @@ -111,7 +110,7 @@ public List getList(String nameLike, Integer page, Integer entries) { @Override public long getCount(String nameLike) { - Search searchCriteria = new Search(GFUser.class); + Search searchCriteria = gfUserDAO.createCountSearch(); if (nameLike != null) { searchCriteria.addFilterILike("name", nameLike); diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/InstanceAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/InstanceAdminServiceImpl.java index f795e401..e6eb9b98 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/InstanceAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/InstanceAdminServiceImpl.java @@ -5,18 +5,18 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Search; import org.geoserver.geofence.core.model.GSInstance; +import org.geoserver.geofence.core.dao.GSInstanceDAO; +import org.geoserver.geofence.core.dao.search.Search; +import org.geoserver.geofence.services.dto.ShortInstance; +import org.geoserver.geofence.services.exception.BadRequestServiceEx; +import org.geoserver.geofence.services.exception.NotFoundServiceEx; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.core.dao.GSInstanceDAO; -import org.geoserver.geofence.services.dto.ShortInstance; -import org.geoserver.geofence.services.exception.BadRequestServiceEx; -import org.geoserver.geofence.services.exception.NotFoundServiceEx; import java.util.ArrayList; /** @@ -60,7 +60,7 @@ public GSInstance get(long id) throws NotFoundServiceEx { @Override public GSInstance get(String name) { - Search search = new Search(GSInstance.class); + Search search = instanceDAO.createSearch(); search.addFilterEqual("name", name); List groups = instanceDAO.search(search); @@ -97,7 +97,7 @@ public List getFullList(String nameLike, Integer page, Integer entri throw new BadRequestServiceEx("Page and entries params should be declared together."); } - Search searchCriteria = new Search(GSInstance.class); + Search searchCriteria = instanceDAO.createSearch(); if(page != null) { searchCriteria.setMaxResults(entries); @@ -128,7 +128,7 @@ private List convertToShortList(List list) { @Override public long getCount(String nameLike) { - Search searchCriteria = new Search(GSInstance.class); + Search searchCriteria = instanceDAO.createCountSearch(); if (nameLike != null) { searchCriteria.addFilterILike("name", nameLike); diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleAdminServiceImpl.java index ae2f08fa..355e5c32 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleAdminServiceImpl.java @@ -5,8 +5,6 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; import org.geoserver.geofence.core.dao.LayerDetailsDAO; import org.geoserver.geofence.core.dao.RuleDAO; import org.geoserver.geofence.core.dao.RuleLimitsDAO; @@ -16,9 +14,13 @@ import org.geoserver.geofence.core.model.RuleLimits; import org.geoserver.geofence.core.model.enums.GrantType; import org.geoserver.geofence.core.model.enums.InsertPosition; +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.services.dto.RuleFilter; import org.geoserver.geofence.services.dto.ShortRule; +import org.geoserver.geofence.services.exception.BadRequestServiceEx; +import org.geoserver.geofence.services.exception.NotFoundServiceEx; + import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -27,9 +29,6 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.services.exception.BadRequestServiceEx; -import org.geoserver.geofence.services.exception.NotFoundServiceEx; - import static org.geoserver.geofence.services.util.FilterUtils.addCriteria; import static org.geoserver.geofence.services.util.FilterUtils.addFixedCriteria; import static org.geoserver.geofence.services.util.FilterUtils.addFixedStringCriteria; @@ -144,8 +143,8 @@ public boolean delete(long id) throws NotFoundServiceEx { @Override public void deleteRulesByUser(String username) throws NotFoundServiceEx { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addFilter(Filter.equal("username", username)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("username", username); List list = ruleDAO.search(searchCriteria); if(LOGGER.isInfoEnabled()) @@ -159,8 +158,8 @@ public void deleteRulesByUser(String username) throws NotFoundServiceEx { @Override public void deleteRulesByRole(String rolename) throws NotFoundServiceEx { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addFilter(Filter.equal("rolename", rolename)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("rolename", rolename); List list = ruleDAO.search(searchCriteria); for (Rule rule : list) { @@ -172,8 +171,9 @@ public void deleteRulesByRole(String rolename) throws NotFoundServiceEx { @Override public void deleteRulesByInstance(long instanceId) throws NotFoundServiceEx { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addFilter(Filter.equal("instance.id", instanceId)); + Search searchCriteria = ruleDAO.createSearch(); + Search.JoinInfo instance = searchCriteria.addJoin("instance"); + searchCriteria.addFilterEqual(instance, "id", instanceId); List list = ruleDAO.search(searchCriteria); for (Rule rule : list) { @@ -256,8 +256,8 @@ public ShortRule getRule(RuleFilter filter) throws BadRequestServiceEx { @Override public List getRulesByPriority(long priority, Integer page, Integer entries) { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addFilter(Filter.greaterOrEqual("priority", priority)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterGreaterOrEqual("priority", priority); searchCriteria.addSortAsc("priority"); addPagingConstraints(searchCriteria, page, entries); List found = ruleDAO.search(searchCriteria); @@ -266,8 +266,8 @@ public List getRulesByPriority(long priority, Integer page, Integer e @Override public ShortRule getRuleByPriority(long priority) throws BadRequestServiceEx { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addFilter(Filter.equal("priority", priority)); + Search searchCriteria = ruleDAO.createSearch(); + searchCriteria.addFilterEqual("priority", priority); List found = ruleDAO.search(searchCriteria); if(found.isEmpty()) return null; @@ -314,39 +314,39 @@ public long count(RuleFilter filter) { // Search stuff private Search buildRuleSearch(RuleFilter filter) { - Search searchCriteria = new Search(Rule.class); + Search search = ruleDAO.createSearch(); if(filter != null) { - addStringCriteria(searchCriteria, "username", filter.getUser()); - addStringCriteria(searchCriteria, "rolename", filter.getRole()); - addCriteria(searchCriteria, "instance", filter.getInstance()); - - addStringCriteria(searchCriteria, "service", filter.getService()); // see class' javadoc - addStringCriteria(searchCriteria, "request", filter.getRequest()); // see class' javadoc - addStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); - addStringCriteria(searchCriteria, "layer", filter.getLayer()); + addStringCriteria(search, "username", filter.getUser()); + addStringCriteria(search, "rolename", filter.getRole()); + addCriteria(search, search.addJoin("instance"), filter.getInstance()); + + addStringCriteria(search, "service", filter.getService()); // see class' javadoc + addStringCriteria(search, "request", filter.getRequest()); // see class' javadoc + addStringCriteria(search, "workspace", filter.getWorkspace()); + addStringCriteria(search, "layer", filter.getLayer()); } - return searchCriteria; + return search; } //========================================================================= private Search buildFixedRuleSearch(RuleFilter filter) { - Search searchCriteria = new Search(Rule.class); + Search search = ruleDAO.createSearch(); if(filter != null) { - addFixedStringCriteria(searchCriteria, "username", filter.getUser()); - addFixedStringCriteria(searchCriteria, "rolename", filter.getRole()); - addFixedCriteria(searchCriteria, "instance", filter.getInstance()); - - addFixedStringCriteria(searchCriteria, "service", filter.getService()); // see class' javadoc - addFixedStringCriteria(searchCriteria, "request", filter.getRequest()); // see class' javadoc - addFixedStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); - addFixedStringCriteria(searchCriteria, "layer", filter.getLayer()); + addFixedStringCriteria(search, "username", filter.getUser()); + addFixedStringCriteria(search, "rolename", filter.getRole()); + addFixedCriteria(search, search.addJoin("instance"), filter.getInstance()); + + addFixedStringCriteria(search, "service", filter.getService()); // see class' javadoc + addFixedStringCriteria(search, "request", filter.getRequest()); // see class' javadoc + addFixedStringCriteria(search, "workspace", filter.getWorkspace()); + addFixedStringCriteria(search, "layer", filter.getLayer()); } - return searchCriteria; + return search; } diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleReaderServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleReaderServiceImpl.java index 686e0f7d..f853ead5 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleReaderServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/RuleReaderServiceImpl.java @@ -5,19 +5,29 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; -import org.geoserver.geofence.core.model.enums.*; -import org.geotools.geometry.jts.JTS; -import org.geotools.referencing.CRS; import org.locationtech.jts.geom.Geometry; import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; + +import org.geotools.geometry.jts.JTS; +import org.geotools.referencing.CRS; +import org.geotools.api.referencing.FactoryException; +import org.geotools.api.referencing.crs.CoordinateReferenceSystem; +import org.geotools.api.referencing.operation.MathTransform; +import org.geotools.api.referencing.operation.TransformException; + import org.geoserver.geofence.core.dao.AdminRuleDAO; import org.geoserver.geofence.core.dao.LayerDetailsDAO; import org.geoserver.geofence.core.dao.RuleDAO; +import org.geoserver.geofence.core.dao.search.Search; +import org.geoserver.geofence.core.dao.search.Search.JoinInfo; +import static org.geoserver.geofence.services.util.FilterUtils.filterByAddress; import org.geoserver.geofence.core.model.*; +import org.geoserver.geofence.core.model.enums.AccessType; +import org.geoserver.geofence.core.model.enums.AdminGrantType; +import org.geoserver.geofence.core.model.enums.CatalogMode; +import org.geoserver.geofence.core.model.enums.GrantType; import org.geoserver.geofence.services.dto.AccessInfo; import org.geoserver.geofence.services.dto.AuthUser; import org.geoserver.geofence.services.dto.RuleFilter; @@ -27,16 +37,13 @@ import org.geoserver.geofence.services.dto.ShortRule; import org.geoserver.geofence.services.exception.BadRequestServiceEx; import org.geoserver.geofence.services.util.AccessInfoInternal; + import org.geoserver.geofence.spi.UserResolver; -import org.geotools.api.referencing.FactoryException; -import org.geotools.api.referencing.crs.CoordinateReferenceSystem; -import org.geotools.api.referencing.operation.MathTransform; -import org.geotools.api.referencing.operation.TransformException; import java.util.*; import java.util.Map.Entry; +import org.geoserver.geofence.core.model.enums.SpatialFilterType; -import static org.geoserver.geofence.services.util.FilterUtils.filterByAddress; /** * @@ -410,6 +417,9 @@ private AccessInfoInternal buildAllowAccessInfo(Rule rule, List limi accessInfo.setCatalogMode(cmode); if (area != null) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Attaching an area to Accessinfo: " + area.getClass().getName() + " " + area.toString()); + } // if we have a clip area we apply clip type // since is more restrictive, otherwise we keep // the intersect @@ -640,42 +650,42 @@ protected Set validateUserRoles(RuleFilter filter) throws BadRequestServ } protected List getRuleAux(RuleFilter filter, TextFilter roleFilter) { - Search searchCriteria = new Search(Rule.class); - searchCriteria.addSortAsc("priority"); - addStringCriteria(searchCriteria, "username", filter.getUser()); - addStringCriteria(searchCriteria, "rolename", roleFilter); - addCriteria(searchCriteria, "instance", filter.getInstance()); - addStringCriteria(searchCriteria, "service", filter.getService()); // see class' javadoc - addStringCriteria(searchCriteria, "request", filter.getRequest()); // see class' javadoc - addStringCriteria(searchCriteria, "subfield", filter.getSubfield()); - addStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); - addStringCriteria(searchCriteria, "layer", filter.getLayer()); - - List found = ruleDAO.search(searchCriteria); + Search search = ruleDAO.createSearch(); + search.addSortAsc("priority"); + addStringCriteria(search, "username", filter.getUser()); + addStringCriteria(search, "rolename", roleFilter); + addCriteria(search, search.addJoin("instance"), filter.getInstance()); + addStringCriteria(search, "service", filter.getService()); // see class' javadoc + addStringCriteria(search, "request", filter.getRequest()); // see class' javadoc + addStringCriteria(search, "subfield", filter.getSubfield()); + addStringCriteria(search, "workspace", filter.getWorkspace()); + addStringCriteria(search, "layer", filter.getLayer()); + + List found = ruleDAO.search(search); found = filterByAddress(filter, found); return found; } - private void addCriteria(Search searchCriteria, String fieldName, IdNameFilter filter) { + private void addCriteria(Search searchCriteria, JoinInfo join, IdNameFilter filter) { switch (filter.getType()) { case ANY: break; // no filtering case DEFAULT: - searchCriteria.addFilterNull(fieldName); + searchCriteria.addFilterNull(join.getField()); break; case IDVALUE: searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName + ".id", filter.getId())); + searchCriteria.isNull(join.getField()), + searchCriteria.isEqual(join, "id", filter.getId())); break; case NAMEVALUE: searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName + ".name", filter.getName())); + searchCriteria.isNull(join.getField()), + searchCriteria.isEqual(join, "name", filter.getName())); break; default: @@ -694,8 +704,8 @@ private void addStringCriteria(Search searchCriteria, String fieldName, TextFilt case NAMEVALUE: searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName, filter.getText())); + searchCriteria.isNull(fieldName), + searchCriteria.isEqual(fieldName, filter.getText())); break; case IDVALUE: @@ -779,17 +789,17 @@ private boolean getAdminAuth(RuleFilter filter) { } protected AdminRule getAdminAuthAux(RuleFilter filter, TextFilter roleFilter) { - Search searchCriteria = new Search(AdminRule.class); - searchCriteria.addSortAsc("priority"); - addStringCriteria(searchCriteria, "username", filter.getUser()); - addStringCriteria(searchCriteria, "rolename", roleFilter); - addCriteria(searchCriteria, "instance", filter.getInstance()); - addStringCriteria(searchCriteria, "workspace", filter.getWorkspace()); + Search search = adminRuleDAO.createSearch(); + search.addSortAsc("priority"); + addStringCriteria(search, "username", filter.getUser()); + addStringCriteria(search, "rolename", roleFilter); + addCriteria(search, search.addJoin("instance"), filter.getInstance()); + addStringCriteria(search, "workspace", filter.getWorkspace()); // we only need the first match, no need to aggregate (no LIMIT rules here) - searchCriteria.setMaxResults(1); + search.setMaxResults(1); - List found = adminRuleDAO.search(searchCriteria); + List found = adminRuleDAO.search(search); found = filterByAddress(filter, found); switch(found.size()) { diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserAdminServiceImpl.java index 0e32421a..3d042f8f 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserAdminServiceImpl.java @@ -5,20 +5,21 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Search; -import org.geoserver.geofence.core.dao.GSUserDAO; import org.geoserver.geofence.core.model.GSUser; + +import org.geoserver.geofence.core.dao.GSUserDAO; + import org.geoserver.geofence.services.dto.ShortUser; +import org.geoserver.geofence.services.exception.BadRequestServiceEx; +import org.geoserver.geofence.services.exception.NotFoundServiceEx; + import java.util.ArrayList; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.services.exception.BadRequestServiceEx; -import org.geoserver.geofence.services.exception.NotFoundServiceEx; - /** * * @author ETj (etj at geo-solutions.it) @@ -58,21 +59,6 @@ public GSUser get(long id) throws NotFoundServiceEx { return user; } - @Override - public GSUser get(String name) { - Search search = new Search(GSUser.class); - search.addFilterEqual("name", name); - List users = userDAO.search(search); - - if(users.isEmpty()) - throw new NotFoundServiceEx("User not found '"+ name + "'"); - else if(users.size() > 1) - throw new IllegalStateException("Found more than one user with name '"+name+"'"); - else - return users.get(0); - } - - @Override public GSUser getFull(String name) throws NotFoundServiceEx { @@ -100,25 +86,7 @@ public List getFullList(String nameLike, Integer page, Integer entries, throw new BadRequestServiceEx("Page and entries params should be declared together."); } - Search searchCriteria = new Search(GSUser.class); - - if(page != null) { - searchCriteria.setMaxResults(entries); - searchCriteria.setPage(page); - } - - if(fetchGroups) { - searchCriteria.addFetch("userGroups"); - } - - searchCriteria.addSortAsc("name"); - - if (nameLike != null) { - searchCriteria.addFilterILike("name", nameLike); - } - - List found = userDAO.search(searchCriteria); - return found; + return userDAO.search(nameLike, page, entries, fetchGroups); } @Override @@ -128,13 +96,7 @@ public List getList(String nameLike, Integer page, Integer entries) { @Override public long getCount(String nameLike) { - Search searchCriteria = new Search(GSUser.class); - - if (nameLike != null) { - searchCriteria.addFilterILike("name", nameLike); - } - - return userDAO.count(searchCriteria); + return userDAO.countByNameLike(nameLike); } // ========================================================================== diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserGroupAdminServiceImpl.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserGroupAdminServiceImpl.java index 55c0a3f0..e04b29e9 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserGroupAdminServiceImpl.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/UserGroupAdminServiceImpl.java @@ -5,19 +5,17 @@ package org.geoserver.geofence.services; -import com.googlecode.genericdao.search.Search; -import org.geoserver.geofence.core.dao.UserGroupDAO; import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.core.dao.UserGroupDAO; +import org.geoserver.geofence.services.dto.ShortGroup; +import org.geoserver.geofence.services.exception.NotFoundServiceEx; import java.util.List; +import java.util.ArrayList; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.geoserver.geofence.services.dto.ShortGroup; -import org.geoserver.geofence.services.exception.BadRequestServiceEx; -import org.geoserver.geofence.services.exception.NotFoundServiceEx; -import java.util.ArrayList; /** * @@ -74,18 +72,14 @@ public UserGroup get(long id) throws NotFoundServiceEx { } @Override - public UserGroup get(String name) { - Search search = new Search(UserGroup.class); - search.addFilterEqual("name", name); - List groups = userGroupDAO.search(search); + public UserGroup get(String name) { + UserGroup group = userGroupDAO.get(name); - if ( groups.isEmpty() ) { + if ( group == null ) { throw new NotFoundServiceEx("UserGroup not found '" + name + "'"); - } else if ( groups.size() > 1 ) { - throw new IllegalStateException("Found more than one UserGroup with name '" + name + "'"); - } else { - return groups.get(0); - } + } + + return group; } @Override @@ -102,31 +96,13 @@ public boolean delete(long id) throws NotFoundServiceEx { @Override public List getList(String nameLike, Integer page, Integer entries) { - Search searchCriteria = buildCriteria(page, entries, nameLike); - List found = userGroupDAO.search(searchCriteria); + List found = userGroupDAO.search(nameLike, page, entries); return convertToShortList(found); } @Override public long getCount(String nameLike) { - Search searchCriteria = buildCriteria(null, null, nameLike); - return userGroupDAO.count(searchCriteria); - } - - protected Search buildCriteria(Integer page, Integer entries, String nameLike) throws BadRequestServiceEx { - if ( (page != null && entries == null) || (page == null && entries != null) ) { - throw new BadRequestServiceEx("Page and entries params should be declared together."); - } - Search searchCriteria = new Search(UserGroup.class); - if ( page != null ) { - searchCriteria.setMaxResults(entries); - searchCriteria.setPage(page); - } - searchCriteria.addSortAsc("name"); - if ( nameLike != null ) { - searchCriteria.addFilterILike("name", nameLike); - } - return searchCriteria; + return userGroupDAO.countByNameLike(nameLike); } // ========================================================================== diff --git a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/util/FilterUtils.java b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/util/FilterUtils.java index 1925817c..e156e578 100644 --- a/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/util/FilterUtils.java +++ b/src/services/core/services-impl/src/main/java/org/geoserver/geofence/services/util/FilterUtils.java @@ -5,13 +5,13 @@ package org.geoserver.geofence.services.util; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; + +import org.geoserver.geofence.core.dao.search.Search; import org.geoserver.geofence.core.model.IPRangeProvider; import org.geoserver.geofence.services.dto.RuleFilter; import org.geoserver.geofence.services.exception.BadRequestServiceEx; @@ -97,34 +97,32 @@ List filterByAddress(RuleFilter filter, List rules) * * We're dealing with IDs here, so we'll suppose that the related object id field is called "id". */ - public static void addCriteria(Search searchCriteria, String fieldName, RuleFilter.IdNameFilter filter) { + public static void addCriteria(Search search, Search.JoinInfo join, RuleFilter.IdNameFilter filter) { switch (filter.getType()) { case ANY: break; // no filtering case DEFAULT: - searchCriteria.addFilterNull(fieldName); + search.addFilterNull(join.getField()); break; case IDVALUE: if(filter.isIncludeDefault()) { - searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName + ".id", filter.getId())); + search.addFilterOr( + search.isNull(join.getField()), + search.isEqual(join, "id", filter.getId())); } else { - searchCriteria.addFilter( - Filter.equal(fieldName + ".id", filter.getId())); + search.addFilterEqual(join , "id", filter.getId()); } break; case NAMEVALUE: if(filter.isIncludeDefault()) { - searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName + ".name", filter.getName())); + search.addFilterOr( + search.isNull(join.getField()), + search.isEqual(join, "name", filter.getName())); } else { - searchCriteria.addFilter( - Filter.equal(fieldName + ".name", filter.getName())); + search.addFilterEqual(join, "name", filter.getName()); } break; @@ -133,7 +131,7 @@ public static void addCriteria(Search searchCriteria, String fieldName, RuleFilt } } - public static void addPagingConstraints(Search searchCriteria, Integer page, Integer entries) { + public static void addPagingConstraints(Search search, Integer page, Integer entries) { if( (page != null && entries == null) || (page ==null && entries != null)) { throw new BadRequestServiceEx("Page and entries params should be declared together."); } @@ -143,28 +141,27 @@ public static void addPagingConstraints(Search searchCriteria, Integer page, Int } if(entries != null) { - searchCriteria.setMaxResults(entries); - searchCriteria.setPage(page); + search.setMaxResults(entries); + search.setPage(page); } } - public static void addStringCriteria(Search searchCriteria, String fieldName, RuleFilter.TextFilter filter) { + public static void addStringCriteria(Search search, String fieldName, RuleFilter.TextFilter filter) { switch (filter.getType()) { case ANY: break; // no filtering case DEFAULT: - searchCriteria.addFilterNull(fieldName); + search.addFilterNull(fieldName); break; case NAMEVALUE: if(filter.isIncludeDefault()) { - searchCriteria.addFilterOr( - Filter.isNull(fieldName), - Filter.equal(fieldName, filter.getText())); + search.addFilterOr( + search.isNull(fieldName), + search.isEqual(fieldName, filter.getText())); } else { - searchCriteria.addFilter( - Filter.equal(fieldName, filter.getText())); + search.addFilterEqual(fieldName, filter.getText()); } break; @@ -175,39 +172,34 @@ public static void addStringCriteria(Search searchCriteria, String fieldName, Ru } - - - /** * Add criteria for searching. * * We're dealing with IDs here, so we'll suppose that the related object id field is called "id". */ - public static void addFixedCriteria(Search searchCriteria, String fieldName, RuleFilter.IdNameFilter filter) { + public static void addFixedCriteria(Search search, Search.JoinInfo join, RuleFilter.IdNameFilter filter) { switch (filter.getType()) { case ANY: - throw new BadRequestServiceEx(fieldName + " should be a fixed search and can't be ANY"); + throw new BadRequestServiceEx(join.getField() + " should be a fixed search and can't be ANY"); case DEFAULT: - searchCriteria.addFilterNull(fieldName); + search.addFilterNull(join.getField()); break; case IDVALUE: if(filter.isIncludeDefault()) { - throw new BadRequestServiceEx(fieldName + " should be a fixed search"); + throw new BadRequestServiceEx(join.getField() + " should be a fixed search"); } else { - searchCriteria.addFilter( - Filter.equal(fieldName + ".id", filter.getId())); + search.addFilterEqual(join, "id", filter.getId()); } break; case NAMEVALUE: if(filter.isIncludeDefault()) { - throw new BadRequestServiceEx(fieldName + " should be a fixed search"); + throw new BadRequestServiceEx(join.getField() + " should be a fixed search"); } else { - searchCriteria.addFilter( - Filter.equal(fieldName + ".name", filter.getName())); + search.addFilterEqual(join, "name", filter.getName()); } break; @@ -217,23 +209,20 @@ public static void addFixedCriteria(Search searchCriteria, String fieldName, Rul } - - - public static void addFixedStringCriteria(Search searchCriteria, String fieldName, RuleFilter.TextFilter filter) { + public static void addFixedStringCriteria(Search search, String fieldName, RuleFilter.TextFilter filter) { switch (filter.getType()) { case ANY: throw new BadRequestServiceEx(fieldName + " should be a fixed search and can't be ANY"); case DEFAULT: - searchCriteria.addFilterNull(fieldName); + search.addFilterNull(fieldName); break; case NAMEVALUE: if(filter.isIncludeDefault()) { throw new BadRequestServiceEx(fieldName + " should be a fixed search"); } else { - searchCriteria.addFilter( - Filter.equal(fieldName, filter.getText())); + search.addFilterEqual(fieldName, filter.getText()); } break; diff --git a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleAdminServiceImplTest.java b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleAdminServiceImplTest.java index 82c98182..e14622cc 100644 --- a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleAdminServiceImplTest.java +++ b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleAdminServiceImplTest.java @@ -61,7 +61,7 @@ public void testUpdateRule() throws Exception { UserGroup p1 = createRole("p1"); UserGroup p2 = createRole("p2"); - Rule rule = new Rule(10, null, "p1",null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule rule = new Rule(10, null, "p1",null,null, "s1", "r1",null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(rule); { @@ -94,9 +94,9 @@ public void testGetAllRules() { UserGroup p1 = createRole("p1"); - Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, "p1", null,null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1",null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, "p1", null,null, "s2", "r2",null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3",null, "w3", "l3", GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); @@ -112,10 +112,10 @@ public void testGetRules() { UserGroup p1 = createRole("p1"); UserGroup p2 = createRole("p2"); - Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, "p2", null,null, "s1", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); - Rule r4 = new Rule(40, null, "p1", null,null, null, null, null, null, GrantType.ALLOW); + Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1",null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, "p2", null,null, "s1", "r2",null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3",null, "w3", "l3", GrantType.ALLOW); + Rule r4 = new Rule(40, null, "p1", null,null, null, null,null, null, null, GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); @@ -144,10 +144,10 @@ public void testGetFixedRule() { UserGroup p1 = createRole("p1"); UserGroup p2 = createRole("p2"); - Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, "p2", null,null, "s1", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); - Rule r4 = new Rule(40, null, "p1", null,null, null, null, null, null, GrantType.ALLOW); + Rule r1 = new Rule(10, null, "p1", null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, "p2", null,null, "s1", "r2", null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, "p1", null,null, "s3", "r3", null, "w3", "l3", GrantType.ALLOW); + Rule r4 = new Rule(40, null, "p1", null,null, null, null, null, null, null, GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); @@ -177,7 +177,7 @@ public void testRuleLimits() throws NotFoundServiceEx { final Long id; { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.LIMIT); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.LIMIT); ruleAdminService.insert(r1); id = r1.getId(); } @@ -241,7 +241,7 @@ public void testRuleLimitsErrors() throws NotFoundServiceEx { } final Long id; - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); @@ -259,7 +259,7 @@ public void testRuleDetails() throws NotFoundServiceEx { final Long id; { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); } @@ -345,7 +345,7 @@ public void testAllowedStyles() throws NotFoundServiceEx { final Long id; { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); } @@ -416,7 +416,7 @@ public void testAllowedStyles() throws NotFoundServiceEx { // set new allowed styles directly { - ruleAdminService.setAllowedStyles(id, new HashSet(Arrays.asList("style_C_1","style_C_2","style_C_3"))); + ruleAdminService.setAllowedStyles(id, new HashSet<>(Arrays.asList("style_C_1","style_C_2","style_C_3"))); } // check @@ -434,7 +434,7 @@ public void testAttribs() throws NotFoundServiceEx { final Long id; { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); } @@ -512,7 +512,7 @@ public void testRuleDetailsErrors() throws NotFoundServiceEx { } try { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.DENY); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.DENY); ruleAdminService.insert(r1); Long id1 = r1.getId(); @@ -524,7 +524,7 @@ public void testRuleDetailsErrors() throws NotFoundServiceEx { } try { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", null, GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", null, GrantType.ALLOW); ruleAdminService.insert(r1); Long id1 = r1.getId(); @@ -542,7 +542,7 @@ public void testRuleDetailsProps() throws NotFoundServiceEx { final Long lid1; { - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); @@ -593,10 +593,10 @@ public void testRuleDetailsProps() throws NotFoundServiceEx { public void testShift() { assertEquals(0, ruleAdminService.getCountAll()); - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, null, null,null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, null, null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); - Rule r4 = new Rule(40, null, null, null,null, "s4", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, null, null,null, "s2", "r2", null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, null, null,null, "s3", "r3", null, "w3", "l3", GrantType.ALLOW); + Rule r4 = new Rule(40, null, null, null,null, "s4", "r3", null, "w3", "l3", GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); @@ -617,9 +617,9 @@ public void testShift() { public void testSwap() { assertEquals(0, ruleAdminService.getCountAll()); - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, null, null,null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, null, null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, null, null,null, "s2", "r2", null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, null, null,null, "s3", "r3", null, "w3", "l3", GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); @@ -643,11 +643,11 @@ public void testSwap() { public void testGetByPriority() { assertEquals(0, ruleAdminService.getAll().size()); - Rule r1 = new Rule(10, null, null, null,null, "s1", "r1", "w1", "l1", GrantType.ALLOW); - Rule r2 = new Rule(20, null, null, null,null, "s2", "r2", "w2", "l2", GrantType.ALLOW); - Rule r3 = new Rule(30, null, null, null,null, "s3", "r3", "w3", "l3", GrantType.ALLOW); - Rule r4 = new Rule(40, null, null, null,null, "s4", "r3", "w3", "l3", GrantType.ALLOW); - Rule r5 = new Rule(50, null, null, null,null, "s5", "r3", "w3", "l3", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null,null, "s1", "r1",null, "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(20, null, null, null,null, "s2", "r2",null, "w2", "l2", GrantType.ALLOW); + Rule r3 = new Rule(30, null, null, null,null, "s3", "r3",null, "w3", "l3", GrantType.ALLOW); + Rule r4 = new Rule(40, null, null, null,null, "s4", "r3",null, "w3", "l3", GrantType.ALLOW); + Rule r5 = new Rule(50, null, null, null,null, "s5", "r3",null, "w3", "l3", GrantType.ALLOW); ruleAdminService.insert(r1); ruleAdminService.insert(r2); diff --git a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderCatalogModeTest.java b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderCatalogModeTest.java index 34deb55d..e7e49e69 100644 --- a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderCatalogModeTest.java +++ b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderCatalogModeTest.java @@ -48,8 +48,8 @@ public void testCatalogModeBothNull() throws NotFoundServiceEx { GSUser u1 = createUser("u1", g1, g2, g3, g4); - insertRule(new Rule(20, "u1", null, null,null, null, null, null, "l1", GrantType.LIMIT), null); - insertRule(new Rule(21, null, "g1", null,null, null, null, null, "l1", GrantType.ALLOW), null); + insertRule(new Rule(20, "u1", null, null,null, null, null, null, null, "l1", GrantType.LIMIT), null); + insertRule(new Rule(21, null, "g1", null,null, null, null, null, null, "l1", GrantType.ALLOW), null); LOGGER.info("SETUP ENDED, STARTING TESTS========================================"); @@ -76,8 +76,8 @@ public void testCatalogModeOneNull() throws NotFoundServiceEx { GSUser u1 = createUser("u1", g1, g2, g3, g4); - insertRule(new Rule(20, "u1", null, null,null, null, null, null, "l1", GrantType.LIMIT), null); - insertRule(new Rule(21, null, "g1", null,null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.MIXED); + insertRule(new Rule(20, "u1", null, null,null, null, null, null, null, "l1", GrantType.LIMIT), null); + insertRule(new Rule(21, null, "g1", null,null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.MIXED); LOGGER.info("SETUP ENDED, STARTING TESTS========================================"); @@ -104,8 +104,8 @@ public void testCatalogModeNoNull() throws NotFoundServiceEx { GSUser u1 = createUser("u1", g1, g2, g3, g4); - insertRule(new Rule(20, "u1", null, null, null, null, null, null, "l1", GrantType.LIMIT), CatalogMode.HIDE); - insertRule(new Rule(21, null, "g1", null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.MIXED); + insertRule(new Rule(20, "u1", null, null, null, null, null, null, null, "l1", GrantType.LIMIT), CatalogMode.HIDE); + insertRule(new Rule(21, null, "g1", null, null, null, null, null, null , "l1", GrantType.ALLOW), CatalogMode.MIXED); LOGGER.info("SETUP ENDED, STARTING TESTS========================================"); @@ -131,8 +131,8 @@ public void testCatalogModeTwoGroups() throws NotFoundServiceEx { GSUser u1 = createUser("u1", g1, g2, g3, g4); - insertRule(new Rule(20, null, "g2", null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.HIDE); - insertRule(new Rule(21, null, "g1", null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.MIXED); + insertRule(new Rule(20, null, "g2", null, null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.HIDE); + insertRule(new Rule(21, null, "g1", null, null, null, null, null, null, "l1", GrantType.ALLOW), CatalogMode.MIXED); LOGGER.info("SETUP ENDED, STARTING TESTS========================================"); diff --git a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImplTest.java b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImplTest.java index 61d1c648..727bf1fa 100644 --- a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImplTest.java +++ b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImplTest.java @@ -175,9 +175,6 @@ public void testGetInfo() { baseFilter.setWorkspace("W0"); baseFilter.setLayer("l0"); - - AccessInfo accessInfo; - { RuleFilter ruleFilter = new RuleFilter(baseFilter); ruleFilter.setUser(SpecialFilterType.ANY); @@ -251,7 +248,7 @@ public void testResolveLazy() { public void testNoDefault() { assertEquals(0, ruleAdminService.count(new RuleFilter(SpecialFilterType.ANY))); - ruleAdminService.insert(new Rule(0, null, null, null,null, "WCS", null, null, null, GrantType.ALLOW)); + ruleAdminService.insert(new Rule(0, null, null, null,null, "WCS", null,null, null, null, GrantType.ALLOW)); assertEquals(1, getMatchingRules("u0","*","i0",null, "WCS", null,"W0","l0").size()); assertEquals(GrantType.ALLOW, getAccessInfo("u0","*","i0",null, "WCS", null,"W0","l0").getGrant()); @@ -406,7 +403,7 @@ public void testAttrib() throws NotFoundServiceEx { int pri = 0; { - Rule r1 = new Rule(pri++, null, "g1", null,null, null, null, null, "l1", GrantType.ALLOW); + Rule r1 = new Rule(pri++, null, "g1", null,null, null, null,null, null, "l1", GrantType.ALLOW); ruleAdminService.insert(r1); LayerDetails d1 = new LayerDetails(); @@ -419,7 +416,7 @@ public void testAttrib() throws NotFoundServiceEx { ruleAdminService.setDetails(r1.getId(), d1); } { - Rule r1 = new Rule(pri++, null, "g2", null,null, null, null, null, "l1", GrantType.ALLOW); + Rule r1 = new Rule(pri++, null, "g2", null,null, null, null, null, null, "l1", GrantType.ALLOW); ruleAdminService.insert(r1); LayerDetails d1 = new LayerDetails(); @@ -440,7 +437,7 @@ public void testAttrib() throws NotFoundServiceEx { ruleAdminService.setDetails(r1.getId(), d1); } { - Rule r1 = new Rule(pri++, null, "g4", null,null, null, null, null, null, "l1", GrantType.DENY); + Rule r1 = new Rule(pri++, null, "g4", null,null, null, null, null, null, "l1", GrantType.DENY); ruleAdminService.insert(r1); } } @@ -812,9 +809,34 @@ public void testMultiRoles() { assertRules(createFilter(u3, "p1,p2,NO"), new Integer[]{50,51,52,60,70,999}); } + /** + * Creates a RuleFilter by heuristically converting special string values into Fitler behaviour:
  • a null value will + * match only with null
  • a '*' value will match everything (no filter condition on that given field)
  • any + * other string will match literally
+ * + */ + private RuleFilter createHeuristicFilter(String userName, String groupName, String instanceName, + String sourceAddress, + String service, String request, String subfield, + String workspace, String layer) { + + RuleFilter filter = new RuleFilter(SpecialFilterType.DEFAULT); + filter.getUser().setHeuristically(userName); + filter.getRole().setHeuristically(groupName); + filter.getInstance().setHeuristically(instanceName); + filter.getSourceAddress().setHeuristically(sourceAddress); + + filter.getService().setHeuristically(service); + filter.getRequest().setHeuristically(request); + filter.getSubfield().setHeuristically(subfield); + filter.getWorkspace().setHeuristically(workspace); + filter.getLayer().setHeuristically(layer); + + return filter; + } private RuleFilter createFilter(String userName, String groupName) { - return new RuleFilter(userName, groupName, "*", "*", "*", "*", "*", "*", "*"); + return createHeuristicFilter(userName, groupName, "*", "*", "*", "*", "*", "*", "*"); } private void assertRules(RuleFilter filter, Integer[] expectedPriorities) { @@ -837,7 +859,7 @@ private List getMatchingRules( String workspace, String layer) { return ruleReaderService.getMatchingRules( - new RuleFilter(userName, profileName, instanceName, sourceAddress, + createHeuristicFilter(userName, profileName, instanceName, sourceAddress, service, request, null, workspace, layer)); } @@ -846,7 +868,7 @@ private AccessInfo getAccessInfo(String userName, String roleName, String instan String service, String request, String workspace, String layer) { return ruleReaderService.getAccessInfo( - new RuleFilter(userName, roleName, instanceName, sourceAddress, + createHeuristicFilter(userName, roleName, instanceName, sourceAddress, service, request, null, workspace, layer)); } diff --git a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImpl_GeomTest.java b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImpl_GeomTest.java index 580f0a33..5e49e525 100644 --- a/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImpl_GeomTest.java +++ b/src/services/core/services-impl/src/test/java/org/geoserver/geofence/services/RuleReaderServiceImpl_GeomTest.java @@ -12,6 +12,7 @@ import org.geoserver.geofence.core.model.enums.CatalogMode; import org.geoserver.geofence.core.model.enums.GrantType; import org.geoserver.geofence.core.model.enums.SpatialFilterType; +import org.geoserver.geofence.core.model.util.EWKTParser; import static org.geoserver.geofence.services.ServiceTestBase.ruleAdminService; import static org.geoserver.geofence.services.ServiceTestBase.ruleReaderService; import org.geoserver.geofence.services.dto.AccessInfo; @@ -36,13 +37,13 @@ public void testRuleLimitsAllowedAreaSRIDIsPreserved() throws NotFoundServiceEx, Long id2 = null; try { { - Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.LIMIT); + Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.LIMIT); ruleAdminService.insert(r1); id = r1.getId(); } { - Rule r2 = new Rule(11, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r2 = new Rule(11, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); id2 = ruleAdminService.insert(r2); } @@ -88,13 +89,13 @@ public void testRuleLimitsAllowedAreaReprojectionWithDifferentSrid() throws NotF Long id3 = null; try { { - Rule r1 = new Rule(999, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(999, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); id = r1.getId(); } { - Rule r2 = new Rule(11, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.LIMIT); + Rule r2 = new Rule(11, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.LIMIT); id2 = ruleAdminService.insert(r2); } @@ -109,7 +110,7 @@ public void testRuleLimitsAllowedAreaReprojectionWithDifferentSrid() throws NotF } { - Rule r3 = new Rule(12, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.LIMIT); + Rule r3 = new Rule(12, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.LIMIT); id3 = ruleAdminService.insert(r3); } @@ -158,8 +159,8 @@ public void testRuleSpatialFilterTypeClipSameGroup() throws ParseException { UserGroup g2 = createRole("group12"); GSUser user = createUser("auth11", g1, g2); - ruleAdminService.insert(new Rule(9999, null, null, null, null, "s11", "r11", "w11", "l11", GrantType.ALLOW)); - long id = ruleAdminService.insert(new Rule(10, user.getName(), "group11", null, null, "s11", "r11", "w11", "l11", GrantType.LIMIT)); + ruleAdminService.insert(new Rule(9999, null, null, null, null, "s11", "r11", null, "w11", "l11", GrantType.ALLOW)); + long id = ruleAdminService.insert(new Rule(10, user.getName(), "group11", null, null, "s11", "r11", null, "w11", "l11", GrantType.LIMIT)); RuleLimits limits = new RuleLimits(); limits.setSpatialFilterType(SpatialFilterType.CLIP); limits.setCatalogMode(CatalogMode.HIDE); @@ -168,7 +169,7 @@ public void testRuleSpatialFilterTypeClipSameGroup() throws ParseException { limits.setAllowedArea(area); ruleAdminService.setLimits(id, limits); - long id2 = ruleAdminService.insert(new Rule(11, user.getName(), "group12", null, null, "s11", "r11", "w11", "l11", GrantType.LIMIT)); + long id2 = ruleAdminService.insert(new Rule(11, user.getName(), "group12", null, null, "s11", "r11", null, "w11", "l11", GrantType.LIMIT)); RuleLimits limits2 = new RuleLimits(); limits2.setSpatialFilterType(SpatialFilterType.INTERSECT); limits2.setCatalogMode(CatalogMode.HIDE); @@ -191,7 +192,7 @@ public void testRuleSpatialFilterTypeClipSameGroup() throws ParseException { assertNull(accessInfo.getAreaWkt()); assertNotNull(accessInfo.getClipAreaWkt()); - Geometry resultArea = (new WKTReader().read(accessInfo.getClipAreaWkt())); + Geometry resultArea = EWKTParser.parse(accessInfo.getClipAreaWkt()); resultArea.normalize(); assertTrue(testArea.equalsExact(resultArea, 10.0E-15)); } @@ -206,8 +207,8 @@ public void testRuleSpatialFilterTypeIntersectsSameGroup() throws ParseException UserGroup g2 = createRole("group14"); GSUser user = createUser("auth12", g1, g2); - ruleAdminService.insert(new Rule(9999, null, null, null, null, "s11", "r11", "w11", "l11", GrantType.ALLOW)); - long id = ruleAdminService.insert(new Rule(13, user.getName(), "group13", null, null, "s11", "r11", "w11", "l11", GrantType.LIMIT)); + ruleAdminService.insert(new Rule(9999, null, null, null, null, "s11", "r11", null, "w11", "l11", GrantType.ALLOW)); + long id = ruleAdminService.insert(new Rule(13, user.getName(), "group13", null, null, "s11", "r11", null, "w11", "l11", GrantType.LIMIT)); RuleLimits limits = new RuleLimits(); limits.setSpatialFilterType(SpatialFilterType.INTERSECT); limits.setCatalogMode(CatalogMode.HIDE); @@ -216,7 +217,7 @@ public void testRuleSpatialFilterTypeIntersectsSameGroup() throws ParseException limits.setAllowedArea(area); ruleAdminService.setLimits(id, limits); - long id2 = ruleAdminService.insert(new Rule(14, user.getName(), "group14", null, null, "s11", "r11", "w11", "l11", GrantType.LIMIT)); + long id2 = ruleAdminService.insert(new Rule(14, user.getName(), "group14", null, null, "s11", "r11", null, "w11", "l11", GrantType.LIMIT)); RuleLimits limits2 = new RuleLimits(); limits2.setSpatialFilterType(SpatialFilterType.INTERSECT); limits2.setCatalogMode(CatalogMode.HIDE); @@ -239,7 +240,7 @@ public void testRuleSpatialFilterTypeIntersectsSameGroup() throws ParseException assertNull(accessInfo.getClipAreaWkt()); assertNotNull(accessInfo.getAreaWkt()); - Geometry resultArea = (new WKTReader().read(accessInfo.getAreaWkt())); + Geometry resultArea = EWKTParser.parse((accessInfo.getAreaWkt())); resultArea.normalize(); assertTrue(testArea.equalsExact(resultArea, 10.0E-15)); } @@ -255,9 +256,9 @@ public void testRuleSpatialFilterTypeEnlargeAccess() throws ParseException { UserGroup g2 = createRole("group23"); GSUser user = createUser("auth22", g1, g2); - ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", "w22", "l22", GrantType.ALLOW)); + ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", null, "w22", "l22", GrantType.ALLOW)); - long id = ruleAdminService.insert(new Rule(15, null, "group22", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id = ruleAdminService.insert(new Rule(15, null, "group22", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits = new RuleLimits(); limits.setSpatialFilterType(SpatialFilterType.INTERSECT); limits.setCatalogMode(CatalogMode.HIDE); @@ -266,7 +267,7 @@ public void testRuleSpatialFilterTypeEnlargeAccess() throws ParseException { limits.setAllowedArea(area); ruleAdminService.setLimits(id, limits); - long id2 = ruleAdminService.insert(new Rule(16, null, "group23", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id2 = ruleAdminService.insert(new Rule(16, null, "group23", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits2 = new RuleLimits(); limits2.setSpatialFilterType(SpatialFilterType.CLIP); limits2.setCatalogMode(CatalogMode.HIDE); @@ -288,13 +289,12 @@ public void testRuleSpatialFilterTypeEnlargeAccess() throws ParseException { assertNotNull(accessInfo.getAreaWkt()); assertNotNull(accessInfo.getClipAreaWkt()); - // the intersects should be equal to the originally defined - // allowed area - Geometry intersects = new WKTReader().read(accessInfo.getAreaWkt()); + // the intersects should be equal to the originally defined allowed area + Geometry intersects = EWKTParser.parse(accessInfo.getAreaWkt()); intersects.normalize(); assertTrue(intersects.equalsExact(area, 10.0E-15)); - Geometry clip = new WKTReader().read(accessInfo.getClipAreaWkt()); + Geometry clip = EWKTParser.parse(accessInfo.getClipAreaWkt()); clip.normalize(); area2.normalize(); assertTrue(clip.equalsExact(area2, 10.0E-15)); @@ -315,9 +315,9 @@ public void testRuleSpatialFilterTypeFourRules() throws ParseException { WKTReader reader = new WKTReader(); - ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", "w22", "l22", GrantType.ALLOW)); + ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", null, "w22", "l22", GrantType.ALLOW)); - long id = ruleAdminService.insert(new Rule(17, null, "group31", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id = ruleAdminService.insert(new Rule(17, null, "group31", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits = new RuleLimits(); limits.setSpatialFilterType(SpatialFilterType.INTERSECT); limits.setCatalogMode(CatalogMode.HIDE); @@ -326,7 +326,7 @@ public void testRuleSpatialFilterTypeFourRules() throws ParseException { limits.setAllowedArea(area); ruleAdminService.setLimits(id, limits); - long id2 = ruleAdminService.insert(new Rule(18, null, "group31", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id2 = ruleAdminService.insert(new Rule(18, null, "group31", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits2 = new RuleLimits(); limits2.setSpatialFilterType(SpatialFilterType.CLIP); limits2.setCatalogMode(CatalogMode.HIDE); @@ -335,7 +335,7 @@ public void testRuleSpatialFilterTypeFourRules() throws ParseException { limits2.setAllowedArea(area2); ruleAdminService.setLimits(id2, limits2); - long id3 = ruleAdminService.insert(new Rule(19, null, "group32", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id3 = ruleAdminService.insert(new Rule(19, null, "group32", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits3 = new RuleLimits(); limits3.setSpatialFilterType(SpatialFilterType.CLIP); limits3.setCatalogMode(CatalogMode.HIDE); @@ -344,7 +344,7 @@ public void testRuleSpatialFilterTypeFourRules() throws ParseException { limits3.setAllowedArea(area3); ruleAdminService.setLimits(id3, limits3); - long id4 = ruleAdminService.insert(new Rule(20, null, "group32", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id4 = ruleAdminService.insert(new Rule(20, null, "group32", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits4 = new RuleLimits(); limits4.setSpatialFilterType(SpatialFilterType.CLIP); limits4.setCatalogMode(CatalogMode.HIDE); @@ -368,7 +368,7 @@ public void testRuleSpatialFilterTypeFourRules() throws ParseException { // allowed area Geometry expectedResult = area.intersection(area2).union(area3.intersection(area4)); expectedResult.normalize(); - Geometry clip = reader.read(accessInfo.getClipAreaWkt()); + Geometry clip = EWKTParser.parse(accessInfo.getClipAreaWkt()); clip.normalize(); assertTrue(clip.equalsExact(expectedResult, 10.0E-15)); } @@ -385,9 +385,9 @@ public void testRuleSpatialFilterTypeFourRules2() throws ParseException { WKTReader reader = new WKTReader(); - ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", "w22", "l22", GrantType.ALLOW)); + ruleAdminService.insert(new Rule(999, null, null, null, null, "s22", "r22", null, "w22", "l22", GrantType.ALLOW)); - long id = ruleAdminService.insert(new Rule(21, null, "group41", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id = ruleAdminService.insert(new Rule(21, null, "group41", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits = new RuleLimits(); limits.setSpatialFilterType(SpatialFilterType.CLIP); limits.setCatalogMode(CatalogMode.HIDE); @@ -396,7 +396,7 @@ public void testRuleSpatialFilterTypeFourRules2() throws ParseException { limits.setAllowedArea(area); ruleAdminService.setLimits(id, limits); - long id2 = ruleAdminService.insert(new Rule(22, null, "group41", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id2 = ruleAdminService.insert(new Rule(22, null, "group41", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits2 = new RuleLimits(); limits2.setSpatialFilterType(SpatialFilterType.CLIP); limits2.setCatalogMode(CatalogMode.HIDE); @@ -405,7 +405,7 @@ public void testRuleSpatialFilterTypeFourRules2() throws ParseException { limits2.setAllowedArea(area2); ruleAdminService.setLimits(id2, limits2); - long id3 = ruleAdminService.insert(new Rule(23, null, "group42", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id3 = ruleAdminService.insert(new Rule(23, null, "group42", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits3 = new RuleLimits(); limits3.setSpatialFilterType(SpatialFilterType.INTERSECT); limits3.setCatalogMode(CatalogMode.HIDE); @@ -414,7 +414,7 @@ public void testRuleSpatialFilterTypeFourRules2() throws ParseException { limits3.setAllowedArea(area3); ruleAdminService.setLimits(id3, limits3); - long id4 = ruleAdminService.insert(new Rule(24, null, "group42", null, null, "s22", "r22", "w22", "l22", GrantType.LIMIT)); + long id4 = ruleAdminService.insert(new Rule(24, null, "group42", null, null, "s22", "r22", null, "w22", "l22", GrantType.LIMIT)); RuleLimits limits4 = new RuleLimits(); limits4.setSpatialFilterType(SpatialFilterType.INTERSECT); limits4.setCatalogMode(CatalogMode.HIDE); @@ -439,13 +439,13 @@ public void testRuleSpatialFilterTypeFourRules2() throws ParseException { // allowed area Geometry expectedIntersects = area3.intersection(area4); expectedIntersects.normalize(); - Geometry intersects = reader.read(accessInfo.getAreaWkt()); + Geometry intersects = EWKTParser.parse(accessInfo.getAreaWkt()); intersects.normalize(); System.out.println(intersects.toString()); System.out.println(expectedIntersects.toString()); assertTrue(expectedIntersects.equalsExact(intersects, 10.0E-15)); - Geometry clip = reader.read(accessInfo.getClipAreaWkt()); + Geometry clip = EWKTParser.parse(accessInfo.getClipAreaWkt()); clip.normalize(); Geometry expectedClip = area2.intersection(area); expectedClip.normalize(); diff --git a/src/services/core/webtest/pom.xml b/src/services/core/webtest/pom.xml index fe187a47..b2fa57f7 100644 --- a/src/services/core/webtest/pom.xml +++ b/src/services/core/webtest/pom.xml @@ -6,33 +6,33 @@ */ --> - 4.0.0 + 4.0.0 org.geoserver.geofence geofence-core - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence - geofence-webtest - war + geofence-webtest + war - GeoFence - Core - Web Test Module + GeoFence - Core - Web Test Module - - - Emanuele Tajariol - ETj - GeoSolutions - - + + + Emanuele Tajariol + ETj + GeoSolutions + + - - UTF-8 - + + UTF-8 + - + @@ -57,6 +57,14 @@ geofence-services-impl
+ + + + + org.hibernate + hibernate-spatial + + @@ -70,23 +78,28 @@ - + - dom4j + org.dom4j dom4j - - + + org.apache.logging.log4j log4j-core + + @@ -94,13 +107,20 @@ org.codehaus.jettison jettison - + + + + javax.servlet + javax.servlet-api provided - - + - @@ -132,7 +153,7 @@ org.apache.cxf cxf-rt-frontend-jaxrs - + org.apache.cxf @@ -153,6 +174,8 @@ + + org.springframework spring-beans @@ -165,34 +188,31 @@ org.springframework spring-core - - org.springframework - spring-aop - - - org.springframework - spring-tx - - - - org.springframework - spring-orm - - - org.springframework - spring-jdbc - - - org.springframework - spring-web - - - org.springframework - spring-webmvc - + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + org.springframework + spring-orm + + + org.springframework + spring-jdbc + + + org.springframework + spring-web + + + org.springframework + spring-webmvc + + @@ -203,14 +223,23 @@ test -
+ + + + + junit + junit + test + - + + + geofence-web-test @@ -242,30 +271,28 @@ org.apache.maven.plugins maven-war-plugin - 2.4 + 3.2.3 - - - org.mortbay.jetty - maven-jetty-plugin - 6.1.20 - - - - /geofence - - - - 9191 - 60000 - - - manual - - + + org.eclipse.jetty + jetty-maven-plugin + 9.4.25.v20191220 + + + + + /geofence + + + 9191 + 60000 + + + + diff --git a/src/services/core/webtest/src/main/java/org/geoserver/geofence/servicetest/MainTest.java b/src/services/core/webtest/src/main/java/org/geoserver/geofence/servicetest/MainTest.java index 72b0c1c4..0443d587 100644 --- a/src/services/core/webtest/src/main/java/org/geoserver/geofence/servicetest/MainTest.java +++ b/src/services/core/webtest/src/main/java/org/geoserver/geofence/servicetest/MainTest.java @@ -126,30 +126,32 @@ public void afterPropertiesSet() throws Exception { /* Cite user rules */ // allow user cite full control over the cite workspace - ruleAdminService.insert(new Rule(priority++, citeUsername, null, null, null, null, null, "cite", null, GrantType.ALLOW)); + ruleAdminService.insert(new Rule(priority++, citeUsername, null, null, null, null, null, null, "cite", null, GrantType.ALLOW)); // allow only getmap, getcapatbilities and reflector usage on workspace sf - ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "GetMap", "sf", null, GrantType.ALLOW))); - ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "GetCapabilities", "sf", null, GrantType.ALLOW))); - ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "reflect", "sf", null, GrantType.ALLOW))); + ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "GetMap", null, "sf", null, GrantType.ALLOW))); + ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "GetCapabilities", null, "sf", null, GrantType.ALLOW))); + ruleAdminService.insert((new Rule(priority++, citeUsername, null, null, null, "wms", "reflect", null, "sf", null, GrantType.ALLOW))); // allow only GetMap and GetFeature the topp workspace /* wms user rules */ - ruleAdminService.insert((new Rule(priority++, wmsUsername, null, null, null, "wms", null, null, null, GrantType.ALLOW))); + ruleAdminService.insert((new Rule(priority++, wmsUsername, null, null, null, "wms", null, null, null, null, GrantType.ALLOW))); /* all powerful but only in a restricted area */ - Rule areaRestriction = new Rule(priority++, areaUsername, null, null, null, null, null, null, null, GrantType.LIMIT); + Rule areaRestriction = new Rule(priority++, areaUsername, null, null, null, null, null, null, null, null, GrantType.LIMIT); RuleLimits limits = new RuleLimits(); - limits.setAllowedArea((MultiPolygon) new WKTReader().read(MULTIPOLYGONWKT)); + MultiPolygon allowedArea = (MultiPolygon) new WKTReader().read(MULTIPOLYGONWKT); + allowedArea.setSRID(4326); + limits.setAllowedArea(allowedArea); long ruleId = ruleAdminService.insert(areaRestriction); ruleAdminService.setLimits(ruleId, limits); - ruleAdminService.insert((new Rule(priority++, areaUsername, null, null, null, null, null, null, null, GrantType.ALLOW))); + ruleAdminService.insert((new Rule(priority++, areaUsername, null, null, null, null, null, null, null, null, GrantType.ALLOW))); /* some users for interactive testing with the default data directory */ // uStates can do whatever, but only on topp:states - ruleAdminService.insert(new Rule(priority++, statesUsername, null, null, null, null, null, "topp", "states", GrantType.ALLOW)); + ruleAdminService.insert(new Rule(priority++, statesUsername, null, null, null, null, null, null, "topp", "states", GrantType.ALLOW)); // deny everything else - ruleAdminService.insert(new Rule(priority++, null, null, null, null, null, null, null, null, GrantType.DENY)); + ruleAdminService.insert(new Rule(priority++, null, null, null, null, null, null, null, null, null, GrantType.DENY)); new Thread(new Runnable() { @Override diff --git a/src/services/core/webtest/src/main/resources/log4j2.xml b/src/services/core/webtest/src/main/resources/log4j2.xml index d462b287..b1704698 100644 --- a/src/services/core/webtest/src/main/resources/log4j2.xml +++ b/src/services/core/webtest/src/main/resources/log4j2.xml @@ -5,7 +5,9 @@ - + diff --git a/src/services/core/webtest/src/main/webapp/WEB-INF/web.xml b/src/services/core/webtest/src/main/webapp/WEB-INF/web.xml index 2ad4a673..a25ce74e 100755 --- a/src/services/core/webtest/src/main/webapp/WEB-INF/web.xml +++ b/src/services/core/webtest/src/main/webapp/WEB-INF/web.xml @@ -15,9 +15,7 @@ contextConfigLocation - - classpath*:applicationContext.xml - + classpath*:applicationContext.xml diff --git a/src/services/modules/generic-api/pom.xml b/src/services/modules/generic-api/pom.xml index f7f33a4a..346f94f8 100644 --- a/src/services/modules/generic-api/pom.xml +++ b/src/services/modules/generic-api/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-modules - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -33,9 +33,9 @@ - javassist - javassist - runtime + com.sun.xml.ws + jaxws-ri + pom org.springframework diff --git a/src/services/modules/ldap/pom.xml b/src/services/modules/ldap/pom.xml index cbada198..118f3262 100644 --- a/src/services/modules/ldap/pom.xml +++ b/src/services/modules/ldap/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-modules - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -20,6 +20,10 @@ jar GeoFence - Module - LDAP Support + + 2.0.0.AM25 + + @@ -48,22 +52,14 @@ org.springframework.ldap spring-ldap-core - 2.2.1.RELEASE com.google.guava guava - 20.0 + 28.1-jre - - - @@ -71,7 +67,6 @@ org.springframework.ldap spring-ldap-test - 2.2.1.RELEASE test @@ -88,31 +83,25 @@ org.apache.directory.server apacheds-core - 1.5.5 - test - - - org.apache.directory.server - apacheds-core-entry - 1.5.5 + ${apacheldap-version} test org.apache.directory.server apacheds-protocol-shared - 1.5.5 + ${apacheldap-version} test org.apache.directory.server apacheds-protocol-ldap - 1.5.5 + ${apacheldap-version} test org.apache.directory.server apacheds-server-jndi - 1.5.5 + ${apacheldap-version} test + + false + + + -
diff --git a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImpl.java b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImpl.java index 2e610303..c166e0ff 100644 --- a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImpl.java +++ b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImpl.java @@ -7,15 +7,15 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.HashSet; +import java.util.LinkedList; import java.util.List; import org.apache.commons.lang.StringUtils; import org.geoserver.geofence.core.dao.GSUserDAO; import org.geoserver.geofence.core.model.GSUser; import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.ldap.utils.LdapUtils; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; /** * GSUserDAO implementation, using an LDAP server as a primary source. @@ -23,7 +23,10 @@ * @author "Mauro Bartolomeoli - mauro.bartolomeoli@geo-solutions.it" * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public class GSUserDAOLdapImpl extends LDAPBaseDAO implements GSUserDAO { + +public class GSUserDAOLdapImpl // + extends LDAPBaseDAO // + implements GSUserDAO { private UserGroupDAOLdapImpl userGroupDAOLdapImpl; @@ -52,17 +55,15 @@ public GSUserDAOLdapImpl() { * @return */ private List getGroups(GSUser user) { - Filter filter = new Filter(); - String member; - List groups; - String filterStr = null; + final String filterStr; String dn = user.getExtId(); String userName = user.getName(); + if (memberFilter != null) { - filterStr = MessageFormat.format(memberFilter, new String[] { dn, userName }); + filterStr = MessageFormat.format(memberFilter, dn, userName); } else if (StringUtils.isNotBlank(dn)) { - filter = new Filter("member", dn); + filterStr = LdapUtils.createLDAPFilterEqual("member", dn, userGroupDAOLdapImpl.getAttributesMapper()); } else { LOGGER.info("User id is null, using username '" + userName + "'"); String nameAttr = getLDAPAttribute("username"); @@ -71,19 +72,17 @@ private List getGroups(GSUser user) { if (memberSearchFilterAttr != null) { // e.g String member = nameAttr + "=" + userName; String val = memberSearchFilterAttr.split("=")[0]; // e.g get uniqueMember part - member = memberSearchFilterAttr.split("=", 2)[1]; // e.g remove uniqueMember part + String member = memberSearchFilterAttr.split("=", 2)[1]; // e.g remove uniqueMember part member = member.replace("{0}", userName); filterStr = val + '=' + member; } else { String exp = nameAttr + "=" + userName; - filter = new Filter("member", exp); + filterStr = LdapUtils.createLDAPFilterEqual("member", exp, userGroupDAOLdapImpl.getAttributesMapper()); } } - if (filterStr == null) { - groups = userGroupDAOLdapImpl.search(filter); - } else { - groups = userGroupDAOLdapImpl.search(filterStr); - } + + List groups = userGroupDAOLdapImpl.search(filterStr); + if (enableHierarchicalGroups && nestedMemberFilter != null) { for (UserGroup group : groups) { groups = addParentGroups(groups, group, 0); @@ -96,8 +95,7 @@ private List addParentGroups(List groups, UserGroup group, if (level < maxLevelGroupsSearch) { List newGroups = new ArrayList(); newGroups.addAll(groups); - String filter = MessageFormat.format(nestedMemberFilter, - new String[] { group.getExtId(), group.getName() }); + String filter = MessageFormat.format(nestedMemberFilter, group.getExtId(), group.getName()); for(UserGroup parentGroup : (List)userGroupDAOLdapImpl.search(filter)) { if (!newGroups.contains(parentGroup)) { newGroups.add(parentGroup); @@ -131,9 +129,8 @@ private GSUser fillWithGroups(GSUser user) { public GSUser searchByName(String name) { - Search search = new Search(); - search.addFilter(new Filter("username", name)); - List users = search(search); + String filter = LdapUtils.createLDAPFilterEqual("username", name, getAttributesMapper()); + List users = search(filter); if (users.isEmpty()) return null; @@ -162,4 +159,51 @@ public void setNestedMemberFilter(String nestedMemberFilter) { public void setEnableHierarchicalGroups(boolean enableHierarchicalGroups) { this.enableHierarchicalGroups = enableHierarchicalGroups; } + + @Override + public List search(String nameLike, Integer page, Integer entries, boolean fetchGroups) throws IllegalArgumentException { + + if (StringUtils.isBlank(nameLike)) { + return paginate(findAll(), entries, page); + } + + // filtering needed -- we'll perform filtering by hand, and contectually + // pagination will be evalueated, in order to save memory and time + + int firstIndex = getFirstPaginationIndex(entries, page); + int lastIndex = getLastPaginationIndex(entries, page); + + List ret = new LinkedList<>(); + int index = 0; + for (GSUser user : findAll()) { + if(user.getName().contains(nameLike)) { + if(++index > firstIndex ) { + ret.add(user); + } + + if(index >= lastIndex) { + break; + } + } + } + + return ret; + } + + @Override + public long countByNameLike(String nameLike) { + + if (StringUtils.isBlank(nameLike)) { + return findAll().size(); + } + + int cnt = 0; + for (GSUser user : findAll()) { + if(user.getName().contains(nameLike)) { + ++cnt; + } + } + + return cnt; + } } diff --git a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/LDAPBaseDAO.java b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/LDAPBaseDAO.java index 8f085cb4..1ab149bd 100644 --- a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/LDAPBaseDAO.java +++ b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/LDAPBaseDAO.java @@ -23,15 +23,12 @@ import org.geoserver.geofence.core.dao.RestrictedGenericDAO; import org.geoserver.geofence.core.model.GSUser; import org.geoserver.geofence.core.model.UserGroup; -import org.geoserver.geofence.ldap.utils.LdapUtils; import org.springframework.ldap.core.AttributesMapper; import org.springframework.ldap.core.DirContextOperations; import org.springframework.ldap.core.LdapTemplate; import org.springframework.ldap.core.support.AbstractContextMapper; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.ISearch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -53,7 +50,7 @@ public abstract class LDAPBaseDAO, R> implements RestrictedGenericDAO, InitializingBean { - private static final class LDAPContextMapper extends AbstractContextMapper { + protected static final class LDAPContextMapper extends AbstractContextMapper { AttributesMapper mapper; public LDAPContextMapper(AttributesMapper mapper) { @@ -135,39 +132,32 @@ public R find(Long id) return null; } - @Override - public List search(ISearch search) - { - List objects = new ArrayList<>(); - if (search.getFilters().isEmpty()) { - // no filter - return paginate(findAll(), search); - } - for (Filter filter : search.getFilters()) { - if (filter != null) { - List filteredObjects = paginate(search(filter), search); - objects.addAll(filteredObjects); - } - } - return objects; - } - - protected List paginate(List list, ISearch search) { - if (search.getMaxResults() > 0 && search.getPage() >= 0) { - List result = new ArrayList(); - int start = search.getPage() * search.getMaxResults(); - for(int index = start ; index < start + search.getMaxResults() && index < list.size(); index++) { + protected List paginate(List list, Integer entries, Integer page) { + if (entries != null && page != null && entries > 0 && page >= 0) { + List result = new ArrayList<>(entries); + int start = page * entries; + for(int index = start ; index < start + entries && index < list.size(); index++) { result.add(list.get(index)); } return result; } return list; } - - @Override - public int count(ISearch search) - { - return search(search).size(); + + protected int getFirstPaginationIndex(Integer entries, Integer page) { + if (entries != null && page != null && entries > 0 && page >= 0) { + return page * entries; + } else { + return 0; + } + } + + protected int getLastPaginationIndex(Integer entries, Integer page) { + if (entries != null && page != null && entries > 0 && page >= 0) { + return page * entries + entries; + } else { + return Integer.MAX_VALUE; + } } @Override @@ -211,28 +201,13 @@ public R lookup(String dn) /** * Search using the given filter on the LDAP server. Uses default base, filter and mapper. * - * @param base - * @param filter - * @param mapper - * @return - */ - public List search(Filter filter) - { - return search(LdapUtils.createLDAPFilter(filter, attributesMapper)); - } - - /** - * Search using the given filter on the LDAP server. Uses default base, filter and mapper. - * - * @param base - * @param filter - * @param mapper + * @param ldapFilter * @return */ - public List search(String filter) + public List search(String ldapFilter) { if(LOGGER.isTraceEnabled()) - LOGGER.trace(getClass().getSimpleName() + ": searching base:'"+searchBase+"', filter: '"+filter+"'"); + LOGGER.trace(getClass().getSimpleName() + ": searching base:'"+searchBase+"', filter: '"+ldapFilter+"'"); if(LOGGER.isInfoEnabled()) { if(dumpCnt.incrementAndGet() % cachedumpmodulo == 0) { @@ -241,7 +216,7 @@ public List search(String filter) } try { - return ldapcache.get(filter); + return ldapcache.get(ldapFilter); //return search(ldapTemplate, searchBase, filter, attributesMapper); } catch (ExecutionException ex) { LOGGER.warn("Error while getting LDAP info: " + ex.getMessage(), ex); @@ -301,6 +276,10 @@ public void setAttributesMapper(AttributesMapper attributesMapper) this.attributesMapper = attributesMapper; } + protected AttributesMapper getAttributesMapper() { + return attributesMapper; + } + /** * Sets the LDAP communication object. * diff --git a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImpl.java b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImpl.java index 3fc9714e..df2cb2c9 100644 --- a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImpl.java +++ b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImpl.java @@ -4,8 +4,12 @@ */ package org.geoserver.geofence.ldap.dao.impl; +import java.util.LinkedList; +import java.util.List; +import org.apache.commons.lang.StringUtils; import org.geoserver.geofence.core.dao.UserGroupDAO; import org.geoserver.geofence.core.model.UserGroup; +import org.geoserver.geofence.ldap.utils.LdapUtils; /** * UserGroupDAO implementation, using an LDAP server as a primary source. @@ -13,7 +17,9 @@ * @author "Mauro Bartolomeoli - mauro.bartolomeoli@geo-solutions.it" * @author Emanuele Tajariol (etj at geo-solutions.it) */ -public class UserGroupDAOLdapImpl extends LDAPBaseDAO implements UserGroupDAO +public class UserGroupDAOLdapImpl // + extends LDAPBaseDAO // + implements UserGroupDAO { public UserGroupDAOLdapImpl() @@ -23,4 +29,66 @@ public UserGroupDAOLdapImpl() setSearchBase("ou=Groups"); setSearchFilter("objectClass=posixGroup"); } + + @Override + public UserGroup get(String name) { + + String filter = LdapUtils.createLDAPFilterEqual("groupname", name, getAttributesMapper()); + List groups = search(filter); + + if (groups.isEmpty()) + return null; + else if (groups.size() > 1) + throw new IllegalArgumentException( + "Given filter (" + name + ") returns too many groups (" + groups.size() + ")"); + else + return groups.get(0); + } + + @Override + public List search(String nameLike, Integer page, Integer entries) throws IllegalArgumentException { + + if (StringUtils.isBlank(nameLike)) { + return paginate(findAll(), entries, page); + } + + // filtering needed -- we'll perform filtering by hand, and contectually + // pagination will be evalueated, in order to save memory and time + + int firstIndex = getFirstPaginationIndex(entries, page); + int lastIndex = getLastPaginationIndex(entries, page); + + List ret = new LinkedList<>(); + int index = 0; + for (UserGroup user : findAll()) { + if(user.getName().contains(nameLike)) { + if(++index > firstIndex ) { + ret.add(user); + } + + if(index >= lastIndex) { + break; + } + } + } + + return ret; + } + + @Override + public long countByNameLike(String nameLike) { + + if (StringUtils.isBlank(nameLike)) { + return findAll().size(); + } + + int cnt = 0; + for (UserGroup user : findAll()) { + if(user.getName().contains(nameLike)) { + ++cnt; + } + } + + return cnt; + } } diff --git a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/utils/LdapUtils.java b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/utils/LdapUtils.java index 2c941106..9dd66da9 100644 --- a/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/utils/LdapUtils.java +++ b/src/services/modules/ldap/src/main/java/org/geoserver/geofence/ldap/utils/LdapUtils.java @@ -8,8 +8,6 @@ import org.springframework.ldap.core.AttributesMapper; -import com.googlecode.genericdao.search.Filter; - import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -20,28 +18,12 @@ public class LdapUtils { private static Logger LOGGER = LogManager.getLogger(LdapUtils.class); - - /** - * Creates and LDAP filter from the DAO search filter. Currently only "property = value" filters are supported. - * - * @param filter - * @return - */ - public static String createLDAPFilter(Filter filter, AttributesMapper mapper) + public static String createLDAPFilterEqual(String propertyName, String value, AttributesMapper mapper) { - // TODO add other filter types - if (filter.getOperator() == Filter.OP_EQUAL) { - String propertyName = filter.getProperty(); - if (mapper instanceof LdapAttributesMapper) { - propertyName = ((LdapAttributesMapper) mapper) - .getLdapAttribute(propertyName); - } - return propertyName + "=" + filter.getValue().toString(); - } else { - LOGGER.error("MISSING IMPLEMENTATION FOR " + filter); + if (mapper instanceof LdapAttributesMapper) { + propertyName = ((LdapAttributesMapper) mapper) + .getLdapAttribute(propertyName); } - return null; + return propertyName + "=" + value; } - - } diff --git a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/BaseDAOTest.java b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/BaseDAOTest.java index 402e0133..8931786b 100644 --- a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/BaseDAOTest.java +++ b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/BaseDAOTest.java @@ -1,17 +1,27 @@ -/* (c) 2014 - 2017 Open Source Geospatial Foundation - all rights reserved +/* (c) 2014 - 2020 Open Source Geospatial Foundation - all rights reserved * This code is licensed under the GPL 2.0 license, available at the root * application directory. */ - package org.geoserver.geofence.ldap.dao.impl; +import org.apache.directory.api.ldap.model.entry.DefaultEntry; +import org.apache.directory.api.ldap.model.ldif.LdifEntry; +import org.apache.directory.api.ldap.model.ldif.LdifReader; +import org.apache.directory.api.ldap.model.schema.SchemaManager; +import org.apache.directory.server.annotations.CreateLdapServer; +import org.apache.directory.server.annotations.CreateTransport; +import org.apache.directory.server.core.annotations.CreateDS; +import org.apache.directory.server.core.annotations.CreatePartition; +import org.apache.directory.server.core.api.DirectoryService; +import org.apache.directory.server.core.factory.DSAnnotationProcessor; +import org.apache.directory.server.core.integ.FrameworkRunner; +import org.apache.directory.server.factory.ServerAnnotationProcessor; +import org.apache.directory.server.ldap.LdapServer; + import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; -import org.springframework.ldap.core.LdapTemplate; -import org.springframework.ldap.core.support.LdapContextSource; import org.springframework.ldap.test.LdapTestUtils; -import org.springframework.ldap.support.LdapUtils; import org.geoserver.geofence.core.dao.GSUserDAO; import org.geoserver.geofence.core.dao.UserGroupDAO; @@ -26,90 +36,83 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; +import org.junit.runner.RunWith; /** * * @author ETj (etj at geo-solutions.it) */ -public abstract class BaseDAOTest -{ - protected final Logger LOGGER; +@RunWith(FrameworkRunner.class) +public abstract class BaseDAOTest { + + protected static Logger LOGGER; protected static GSUserDAO userDAO; protected static UserGroupDAO userGroupDAO; protected static ClassPathXmlApplicationContext ctx = null; - @Rule - public TestName name = new TestName(); + @Rule + public TestName name = new TestName(); - public BaseDAOTest() - { + public BaseDAOTest() { LOGGER = LogManager.getLogger(getClass()); synchronized (BaseDAOTest.class) { if (ctx == null) { String[] paths = { - "classpath*:applicationContext.xml", -// "applicationContext.xml", -// "applicationContext-geofence-ldap.xml" -// ,"applicationContext-test.xml" - }; + "classpath*:applicationContext.xml",}; ctx = new ClassPathXmlApplicationContext(paths); userDAO = (GSUserDAO) ctx.getBean("gsUserDAO_LDAP"); userGroupDAO = (UserGroupDAO) ctx.getBean("userGroupDAO_LDAP"); } - } } @BeforeClass - public static void setUpClass() throws Exception - { - // Start an LDAP server and import test data -// LdapTestUtils.startEmbeddedServer(10389, "", "test"); -// LdapTestUtils.startEmbeddedServer(10389, "dc=example,dc=com", "test"); - LdapTestUtils.startEmbeddedServer(10389, "dc=com", "test"); - loadData(); + @CreateLdapServer(name = "DSAlias", // + transports = {@CreateTransport(protocol = "LDAP", port = 10389)}, // + allowAnonymousAccess = true) + @CreateDS(name = "test", // + partitions = @CreatePartition(name = "example_com", suffix = "dc=example,dc=com"), // + allowAnonAccess = true) + public static void setUpClass() throws Exception { + + Logger logger = LogManager.getLogger(BaseDAOTest.class); + + DirectoryService directoryService = DSAnnotationProcessor.getDirectoryService(); + final SchemaManager schemaManager = directoryService.getSchemaManager(); + LdapServer ldapServer = ServerAnnotationProcessor.getLdapServer(directoryService); + + logger.info("Creating test entries..."); + + ClassPathResource ldif = new ClassPathResource("data.ldif"); + int entries = 0; + for (LdifEntry ldifEntry : new LdifReader(ldif.getInputStream())) { + + DefaultEntry entry = new DefaultEntry(schemaManager, ldifEntry.getEntry()); + directoryService.getAdminSession().add(entry); + ++entries; + + } + logger.info("Created " +entries + " entries "); } @AfterClass - public static void tearDownClass() throws Exception - { + public static void tearDownClass() throws Exception { LdapTestUtils.shutdownEmbeddedServer(); } @Before - public void setUp() throws Exception - { - LOGGER.info("################ Setting up -- " + getClass().getSimpleName() + ":: " + name.getMethodName() ); + public void setUp() throws Exception { +// LOGGER.info("################ Setting up -- " + getClass().getSimpleName() + ":: " + name.getMethodName()); // loadData(); - LOGGER.info("##### Ending setup for " + getClass().getSimpleName() + " ###----------------------"); - } - - protected static void loadData() throws Exception - { - // Bind to the directory - LdapContextSource contextSource = new LdapContextSource(); - contextSource.setUrl("ldap://127.0.0.1:10389"); - contextSource.setUserDn("uid=admin,ou=system"); - contextSource.setPassword("secret"); - contextSource.setPooled(false); - //contextSource.setDirObjectFactory(null); - contextSource.afterPropertiesSet(); - - // Create the Sprint LDAP template - LdapTemplate template = new LdapTemplate(contextSource); - - // Clear out any old data - and load the test data - LdapTestUtils.clearSubContexts(contextSource, LdapUtils.newLdapName("dc=example,dc=com")); - LdapTestUtils.loadLdif(contextSource, new ClassPathResource("data.ldif")); +// LOGGER.info("##### Ending setup for " + getClass().getSimpleName() + " ###----------------------"); } @Test - public void testCheckDAOs() - { + public void testCheckDAOs() { assertNotNull(userDAO); assertTrue(userDAO instanceof GSUserDAOLdapImpl); } diff --git a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImplTest.java b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImplTest.java index 1e9bc371..392c0b11 100644 --- a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImplTest.java +++ b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/GSUserDAOLdapImplTest.java @@ -11,8 +11,6 @@ import org.junit.Test; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; import java.util.HashSet; import java.util.Set; import org.geoserver.geofence.core.model.UserGroup; @@ -53,15 +51,13 @@ public void testGetFullByName() @Test public void testCount() { - assertTrue(userDAO.count(new Search()) > 0); + assertTrue(userDAO.countByNameLike(null) > 0); } @Test public void testSearch_admin() { - Search search = new Search(); - search.addFilter(new Filter("username", "admin")); - List users = userDAO.search(search); + List users = userDAO.search("admin", null, null, true); assertTrue(users.size() > 0); GSUser user = users.get(0); assertTrue(user.getName().length() > 0); @@ -70,18 +66,13 @@ public void testSearch_admin() @Test public void testSearchPagination() { - Search search = new Search(); - List users = userDAO.search(search); + List users = userDAO.search(null, null, null, true); assertEquals(4, users.size()); - search.setPage(0); - search.setMaxResults(3); - users = userDAO.search(search); + users = userDAO.search(null, 0, 3, true); assertEquals(3, users.size()); - search.setPage(1); - search.setMaxResults(3); - users = userDAO.search(search); + users = userDAO.search(null, 1, 3, true); assertEquals(1, users.size()); } @@ -89,12 +80,10 @@ public void testSearchPagination() @Test public void testSearch_groups() { - Search search = new Search(); - search.addFilter(new Filter("username", "destination1")); - List users = userDAO.search(search); - assertTrue(users.size() == 1); + List users = userDAO.search("destination1", null, null, true); + assertEquals(1, users.size()); GSUser user = users.get(0); - assertTrue(user.getName().equals("destination1")); + assertEquals("destination1", user.getName()); } @Test diff --git a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImplTest.java b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImplTest.java index cdfc792c..6a365aef 100644 --- a/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImplTest.java +++ b/src/services/modules/ldap/src/test/java/org/geoserver/geofence/ldap/dao/impl/UserGroupDAOLdapImplTest.java @@ -11,9 +11,6 @@ import org.junit.Test; -import com.googlecode.genericdao.search.Filter; -import com.googlecode.genericdao.search.Search; - import java.util.Arrays; import java.util.HashSet; import java.util.Set; @@ -53,41 +50,32 @@ public void testFind() @Test public void testSearch() { - Search search = new Search(); - search.addFilter(new Filter("groupname", "adminGroup")); + assertNotNull(userGroupDAO.get("adminGroup")); - List groups = userGroupDAO.search(search); - assertTrue(groups.size() == 1); - UserGroup group = groups.get(0); - assertEquals("adminGroup", group.getName()); + List groups = userGroupDAO.search("adminGroup", null, null); + assertEquals(1, groups.size()); + assertEquals("adminGroup", groups.get(0).getName()); } @Test public void testCount() { - assertEquals(5, userGroupDAO.count(new Search())); + assertEquals(5, userGroupDAO.countByNameLike(null)); } @Test public void testSearchPagination() { - Search search = new Search(); - List groups = userGroupDAO.search(search); + List groups = userGroupDAO.search(null, null, null); assertEquals(5, groups.size()); - search.setPage(0); - search.setMaxResults(2); - groups = userGroupDAO.search(search); + groups = userGroupDAO.search(null, 0,2); assertEquals(2, groups.size()); - search.setPage(1); - search.setMaxResults(2); - groups = userGroupDAO.search(search); + groups = userGroupDAO.search(null, 1, 2); assertEquals(2, groups.size()); - search.setPage(2); - search.setMaxResults(2); - groups = userGroupDAO.search(search); + groups = userGroupDAO.search(null, 2, 2); assertEquals(1, groups.size()); } } diff --git a/src/services/modules/login/api/pom.xml b/src/services/modules/login/api/pom.xml index 735cc006..eae3c7bd 100644 --- a/src/services/modules/login/api/pom.xml +++ b/src/services/modules/login/api/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-login-parent - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -36,11 +36,6 @@ - - javassist - javassist - runtime - org.springframework spring-beans diff --git a/src/services/modules/login/impl/pom.xml b/src/services/modules/login/impl/pom.xml index 3e371fe6..4c9e8a18 100644 --- a/src/services/modules/login/impl/pom.xml +++ b/src/services/modules/login/impl/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-login-parent - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -44,11 +44,6 @@ - - javassist - javassist - runtime - org.springframework spring-beans diff --git a/src/services/modules/login/pom.xml b/src/services/modules/login/pom.xml index 8e479b5d..06a1fbf5 100644 --- a/src/services/modules/login/pom.xml +++ b/src/services/modules/login/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-modules - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/modules/pom.xml b/src/services/modules/pom.xml index b9e750cc..62e5e099 100644 --- a/src/services/modules/pom.xml +++ b/src/services/modules/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/modules/rest/api/pom.xml b/src/services/modules/rest/api/pom.xml index 3cf8ed65..0125f1e3 100644 --- a/src/services/modules/rest/api/pom.xml +++ b/src/services/modules/rest/api/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-rest-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence @@ -57,11 +57,6 @@ - - javassist - javassist - runtime - junit junit diff --git a/src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/RESTConfigService.java b/src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/RESTConfigService.java index 9769f8ab..4b7308b8 100644 --- a/src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/RESTConfigService.java +++ b/src/services/modules/rest/api/src/main/java/org/geoserver/geofence/services/rest/RESTConfigService.java @@ -43,6 +43,7 @@ public interface RESTConfigService /** * @deprecated misbehaves since usergroups introduction. Please use backup() */ + @Deprecated(forRemoval = true) @GET @Path("/full") @Produces(MediaType.APPLICATION_XML) @@ -91,6 +92,7 @@ void cleanup() /** * @deprecated */ + @Deprecated @PUT @Path("/full") @Produces(MediaType.APPLICATION_XML) @@ -103,6 +105,7 @@ RESTConfigurationRemapping setConfiguration(@Multipart("configuration") RESTFull /** * @deprecated */ + @Deprecated(forRemoval = true) @GET @Path("/users") @Produces(MediaType.APPLICATION_XML) @@ -112,6 +115,7 @@ RESTFullUserList getUsers() /** * @deprecated */ + @Deprecated(forRemoval = true) @GET @Path("/groups") @Produces(MediaType.APPLICATION_XML) @@ -123,6 +127,7 @@ RESTFullUserGroupList getUserGroups() /** * @deprecated used for testing only */ + @Deprecated(forRemoval = true) @POST @Path("/groups") @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) @@ -135,6 +140,7 @@ void setUserGroups(@Multipart("groups")RESTFullUserGroupList groups) * * @deprecated used for testing only */ + @Deprecated(forRemoval = true) @POST @Path("/users/short") @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) @@ -144,6 +150,7 @@ void setUsers(@Multipart("users")RESTShortUserList users) /** * @deprecated used for testing only */ + @Deprecated(forRemoval = true) @POST @Path("/instances/short") @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) @@ -153,6 +160,7 @@ void setInstances(@Multipart("instances")RESTShortInstanceList instances) /** * @deprecated used for testing only */ + @Deprecated(forRemoval = true) @POST @Path("/rules/short") @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML}) diff --git a/src/services/modules/rest/client/pom.xml b/src/services/modules/rest/client/pom.xml index 274b5b66..5b355af3 100644 --- a/src/services/modules/rest/client/pom.xml +++ b/src/services/modules/rest/client/pom.xml @@ -12,7 +12,7 @@ org.geoserver.geofence geofence-rest-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/modules/rest/impl/pom.xml b/src/services/modules/rest/impl/pom.xml index 31fd202e..071592dd 100644 --- a/src/services/modules/rest/impl/pom.xml +++ b/src/services/modules/rest/impl/pom.xml @@ -12,12 +12,13 @@ org.geoserver.geofence geofence-rest-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence geofence-rest-impl jar + GeoFence - Modules - REST services implementation @@ -56,14 +57,14 @@ org.apache.cxf cxf-rt-frontend-jaxrs - 3.1.5 + org.apache.cxf cxf-rt-rs-extension-providers - 3.1.5 + @@ -79,13 +80,6 @@ - - - javassist - javassist - runtime - - org.springframework spring-beans @@ -99,7 +93,6 @@ net.sf.json-lib json-lib 2.4 - jar jdk15 compile @@ -108,6 +101,11 @@ xom 1.1 --> + + org.geoserver.geofence @@ -138,15 +136,16 @@ javax.servlet - servlet-api + javax.servlet-api test - org.hibernatespatial - hibernate-spatial-h2-geodb + org.hibernate + hibernate-spatial test + org.opengeo geodb diff --git a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/BaseRESTServiceImpl.java b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/BaseRESTServiceImpl.java index bf1753d1..829c83b5 100644 --- a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/BaseRESTServiceImpl.java +++ b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/BaseRESTServiceImpl.java @@ -63,7 +63,7 @@ protected GSUser getUser(IdName userFilter) throws BadRequestRestEx, NotFoundRes if (userFilter.getId() != null) { throw new BadRequestRestEx("Users can only be referenced by name"); } else if (userFilter.getName() != null) { - return userAdminService.get(userFilter.getName()); + return userAdminService.getFull(userFilter.getName()); } else { throw new BadRequestRestEx("Bad GSUser filter " + userFilter); } diff --git a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTConfigServiceImpl.java b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTConfigServiceImpl.java index b6969626..60439131 100644 --- a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTConfigServiceImpl.java +++ b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTConfigServiceImpl.java @@ -294,10 +294,10 @@ public synchronized RESTConfigurationRemapping setConfiguration(RESTFullConfigur RESTConfigurationRemapping remap = new RESTConfigurationRemapping(); - RemapperCache groupCache = new RemapperCache(userGroupAdminService, remap.getUserGroups()); - RemapperCache userCache = new RemapperCache(userAdminService, remap.getUsers()); + RemapperCache groupCache = new RemapperCache<>(userGroupAdminService, remap.getUserGroups()); + RemapperCache userCache = new RemapperCache<>(userAdminService, remap.getUsers()); RemapperCache instanceCache = - new RemapperCache(instanceAdminService, remap.getInstances()); + new RemapperCache<>(instanceAdminService, remap.getInstances()); try { @@ -488,9 +488,9 @@ public void setInstances(RESTShortInstanceList instances) throws BadRequestRestE @Override public void setRules(RESTOutputRuleList rules) throws BadRequestRestEx, NotFoundRestEx, InternalErrorRestEx { int okCnt = 0; - Map groups = new HashMap(); - Map users = new HashMap(); - Map instances = new HashMap(); + Map groups = new HashMap<>(); + Map users = new HashMap<>(); + Map instances = new HashMap<>(); for (RESTOutputRule in : rules) { try { @@ -566,7 +566,7 @@ public void setRestBatchService(RESTBatchService restBatchService) { // ========================================================================== class RemapperCache> { - private Map cache = new HashMap(); + private Map cache = new HashMap<>(); private final Map idRemapper; private final SERVICE service; diff --git a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTUserServiceImpl.java b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTUserServiceImpl.java index def3b8ba..17ec700d 100644 --- a/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTUserServiceImpl.java +++ b/src/services/modules/rest/impl/src/main/java/org/geoserver/geofence/services/rest/impl/RESTUserServiceImpl.java @@ -63,7 +63,7 @@ public Response delete(String username, boolean cascade) throws ConflictRestEx, } } - GSUser user = userAdminService.get(username); // may throw NotFoundServiceEx + GSUser user = userAdminService.getFull(username); // may throw NotFoundServiceEx if ( ! userAdminService.delete(user.getId())) { LOGGER.warn("ILLEGAL STATE -- User not found: " + user); // this should not happen @@ -104,7 +104,7 @@ public Response insert(RESTInputUser user) throws BadRequestRestEx, NotFoundRest boolean exists; // check that no user with same name exists try { - userAdminService.get(user.getName()); + userAdminService.getFull(user.getName()); exists = true; } catch (NotFoundServiceEx ex) { // well, ok, user does not exist @@ -121,7 +121,7 @@ public Response insert(RESTInputUser user) throws BadRequestRestEx, NotFoundRest try { - Set groups = new HashSet(); + Set groups = new HashSet<>(); // resolve groups List inputGroups = user.getGroups(); if ( inputGroups == null || inputGroups.isEmpty() ) { @@ -169,7 +169,7 @@ public Response insert(RESTInputUser user) throws BadRequestRestEx, NotFoundRest @Override public void update(String name, RESTInputUser user) throws BadRequestRestEx, NotFoundRestEx, InternalErrorRestEx { try { - GSUser old = userAdminService.get(name); + GSUser old = userAdminService.getFull(name); update(old.getId(), user); } catch (NotFoundServiceEx ex) { LOGGER.warn("User not found: " + name); diff --git a/src/services/modules/rest/pom.xml b/src/services/modules/rest/pom.xml index 527a9d5f..0de578b1 100644 --- a/src/services/modules/rest/pom.xml +++ b/src/services/modules/rest/pom.xml @@ -14,7 +14,7 @@ org.geoserver.geofence geofence-modules - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence diff --git a/src/services/modules/rest/test/pom.xml b/src/services/modules/rest/test/pom.xml index 091d3445..c1d8048b 100644 --- a/src/services/modules/rest/test/pom.xml +++ b/src/services/modules/rest/test/pom.xml @@ -12,28 +12,28 @@ org.geoserver.geofence geofence-rest-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence geofence-rest-test - war + war GeoFence - Modules - REST services test - - - Emanuele Tajariol - ETj - GeoSolutions - - + + + Emanuele Tajariol + ETj + GeoSolutions + + - - UTF-8 - + + UTF-8 + - + @@ -68,6 +68,13 @@ geofence-rest-impl + + + + org.hibernate + hibernate-spatial + + @@ -81,11 +88,11 @@ - + - dom4j + org.dom4j dom4j @@ -107,11 +114,11 @@ javax.servlet - servlet-api + javax.servlet-api provided - - + - - + + org.springframework spring-beans @@ -183,51 +195,30 @@ org.springframework spring-core - - org.springframework - spring-aop - - - org.springframework - spring-tx - - - - org.springframework - spring-orm - - - org.springframework - spring-jdbc - - - org.springframework - spring-web - - - - - + + org.springframework + spring-aop + + + org.springframework + spring-tx + + + + org.springframework + spring-orm + + + org.springframework + spring-jdbc + + + org.springframework + spring-web + @@ -235,18 +226,17 @@ junit junit - 4.13.1 test - + - - + - - org.apache.maven.plugins - maven-war-plugin - 2.4 - - - + - - - org.mortbay.jetty - maven-jetty-plugin - 6.1.26 - - - - /geofence - - - - 9191 - 60000 - - - manual + + org.apache.maven.plugins + maven-war-plugin + 2.4 + + + + + + org.eclipse.jetty + jetty-maven-plugin + 9.4.25.v20191220 + + + 9966 jetty-stop 10 + + + /geofence + + + 9191 + 60000 + + + + - - - - - + diff --git a/src/services/modules/rest/test/src/main/java/org/geoserver/geofence/services/servicetest/MainTest.java b/src/services/modules/rest/test/src/main/java/org/geoserver/geofence/services/servicetest/MainTest.java index f5ff1e53..1bbb05e4 100644 --- a/src/services/modules/rest/test/src/main/java/org/geoserver/geofence/services/servicetest/MainTest.java +++ b/src/services/modules/rest/test/src/main/java/org/geoserver/geofence/services/servicetest/MainTest.java @@ -97,14 +97,14 @@ private void setUpTestRule() { gs1.setDescription("A sample instance"); instanceAdminService.insert(gs1); - Rule r0 = new Rule(5, u1.getName(), p2.getName(), gs1, null, "s0", "r0", null, null, GrantType.ALLOW); + Rule r0 = new Rule(5, u1.getName(), p2.getName(), gs1, null, "s0", "r0", null, null, null, GrantType.ALLOW); ruleAdminService.insert(r0); final Long r1id; { - Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", "w1", "l1", GrantType.ALLOW); + Rule r1 = new Rule(10, null, null, null, null, "s1", "r1", null, "w1", "l1", GrantType.ALLOW); ruleAdminService.insert(r1); r1id = r1.getId(); } diff --git a/src/services/modules/rest/test/src/main/webapp/WEB-INF/web.xml b/src/services/modules/rest/test/src/main/webapp/WEB-INF/web.xml index bfbde363..60112607 100755 --- a/src/services/modules/rest/test/src/main/webapp/WEB-INF/web.xml +++ b/src/services/modules/rest/test/src/main/webapp/WEB-INF/web.xml @@ -27,9 +27,9 @@ --> - + org.springframework.web.context.ContextLoaderListener diff --git a/src/services/pom.xml b/src/services/pom.xml index ecea92ae..61cb4170 100644 --- a/src/services/pom.xml +++ b/src/services/pom.xml @@ -11,12 +11,12 @@ org.geoserver.geofence geofence - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence geofence-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT pom GeoFence - 0 Services @@ -28,56 +28,60 @@ + 3.8-SNAPSHOT geofence + + 11 + 11 + + 33-SNAPSHOT + 1.20.0 + + + 5.3.39 + 5.8.16 + 2.0.8 + 1.0.1 - 30-SNAPSHOT - - 3.1.5 - 5.3.0.4-fuse - - 4.2.5.RELEASE - 4.0.4.RELEASE - - 1.6.1.2-fuse - - 1.6.2 - 1.4 - 2.7.0 - 2.8.1 - 1.3.03 + 2.7.3 + 2.12.2 + 1.4.01 2.1 1.2 - 2.17.2 + 2.24.3 2.3 - 3.2 - 1.7.0 + 3.2.2 + 1.9.4 1.2.2 - 1.4 + 1.17.1 + 3.3.2 - 1.1 - 4.0 + + 5.6.15.Final - 1.6.1 - - 2.3 + 42.2.18 + 1.3.3 + 1.1 + 4.0 + 2.1.4 3.1.0.2-fuse - 2.0.8 - 1.0.1 1.5.4 + 2.0 9.2.13.v20150730 - 1.2 2.2.3 - 2.2 - - 3.8.0.GA + 3.3.0 + 3.30.2-GA 1.3.3-SNAPSHOT - @@ -185,24 +189,24 @@ ${geofence-version} - + org.geoserver.geofence geofence-ldap ${geofence-version} - - - - - it.geosolutions - geoserver-manager - ${gsmanager-version} - + + + + + it.geosolutions + geoserver-manager + ${gsmanager-version} + - - - + + + quartz quartz @@ -215,9 +219,9 @@ runtime - - - + + + org.apache.logging.log4j @@ -245,12 +249,12 @@ ${log4j-version} - - - + + + - dom4j + org.dom4j dom4j ${dom4j-version} @@ -258,12 +262,13 @@ org.jdom jdom - 1.1 + 1.1.3 - - - + + + + commons-lang commons-lang @@ -272,7 +277,7 @@ commons-io commons-io - 1.4 + 2.6 commons-httpclient @@ -306,9 +311,20 @@ ${commons-logging-version} - - - + + + + + javax.xml.ws + jaxws-api + ${jaxws-version} + + + javax.jws + jsr181-api + + + org.apache.cxf @@ -371,175 +387,139 @@ cxf-rt-rs-client ${cxf-version} - - - - - - - org.apache.velocity - velocity - ${velocity-version} - - - javax.mail - mail - ${javax-mail-version} + org.apache.cxf + cxf-rt-rs-extension-providers + ${cxf-version} - - jakarta.xml.bind - jakarta.xml.bind-api - 2.3.3 + + + + javax.xml.bind + jaxb-api + 2.4.0-b180830.0359 + org.glassfish.jaxb + jaxb-runtime + 2.4.0-b180830.0438 + + + jakarta.xml.ws jakarta.xml.ws-api 2.3.3 - + - - - org.springframework - spring-core - ${spring-version} - - - org.springframework - spring-jmx - ${spring-support} - - - - org.springframework - spring-beans - ${spring-version} - + jakarta.xml.bind + jakarta.xml.bind-api + 4.0.0 - org.springframework - spring-context - ${spring-version} - - - - org.springframework - spring-tx - ${spring-version} - - - org.springframework - spring-context-support - ${spring-version} - - - org.springframework - spring-jdbc - ${spring-version} - - + com.sun.xml.bind + jaxb-impl + 4.0.0 + runtime + --> + + + + + - org.springframework - spring-orm - ${spring-version} + com.sun.xml.ws + jaxws-rt + 2.3.2 + pom - - org.springframework - spring-aop - ${spring-version} + com.sun.xml.ws + jaxws-ri + 2.3.2 + pom - - org.springframework - spring-web - ${spring-version} + com.sun.xml.ws + rt + 2.3.2 - org.springframework - spring-remoting - ${spring-version} + javax.annotation + javax.annotation-api + 1.3.2 + + + + org.springframework - spring-webmvc + spring-framework-bom ${spring-version} + pom + import - - - + + + + org.springframework.security - spring-security-core + spring-security-bom ${spring-security-version} - - - org.springframework - spring-expression - - + pom + import + - org.springframework.security - spring-security-taglibs - ${spring-security-version} - - - org.springframework - spring-expression - - + org.springframework.ldap + spring-ldap-core + 2.3.2.RELEASE - org.springframework.security - spring-security-acl - ${spring-security-version} - - - org.springframework - spring-expression - - + org.springframework.ldap + spring-ldap-test + 2.3.2.RELEASE - - - + + + javax.servlet - servlet-api - 2.3 + javax.servlet-api + 3.1.0 provided - - - + + + asm asm - ${asm-version} - - - cglib - cglib - ${cglib-version} + 3.3.1 cglib @@ -547,13 +527,14 @@ ${cglib-version} - - - + + + + org.codehaus.jettison jettison - ${jettison-version} + 1.4.0 @@ -573,91 +554,39 @@ junit junit - 4.11 + 4.13 test - - - - - - com.googlecode.genericdao - dao - ${hibernate-generic-dao-version} - - - - - javax.persistence - persistence-api - - - - - com.googlecode.genericdao - search-jpa-hibernate - ${hibernate-generic-dao-version} - - - - - - - org.hibernate - hibernate-entitymanager - ${hibernate-version} - + + + org.hibernate hibernate-core ${hibernate-version} + org.hibernate - hibernate-ehcache + hibernate-jcache ${hibernate-version} - - - - + + + - org.hibernatespatial + org.hibernate hibernate-spatial - ${hibernate-spatial-version} - - - - org.hibernatespatial - hibernate-spatial-postgis - ${hibernate-spatial-version} - - - - org.hibernatespatial - hibernate-spatial-oracle - ${hibernate-spatial-version} - - - - org.hibernatespatial - hibernate-spatial-h2-geodb - ${hibernate-spatial-version} - - - - org.javassist - javassist - 3.23.1-GA + ${hibernate-version} - - - - + + + + org.postgis postgis-jdbc @@ -667,7 +596,7 @@ org.postgis postgis-stubs - + org.postgresql @@ -675,17 +604,19 @@ ${postgresql.jdbc.version} - + com.h2database h2 - 1.3.176 + + + 2.3.232 - - + + org.locationtech.jts @@ -693,43 +624,17 @@ ${jts.version} - - - - - javax.media - jai_core - 1.1.3 - - - javax.media - jai_codec - 1.1.3 - - - javax.media - jai_imageio - 1.1 - - - - com.googlecode.jaitools - jt-utils - 1.1.1 - - + + + - net.java.dev.jsr-275 - jsr-275 - 1.0-beta-2 + org.geotools + gt-main + ${gt-version} - - - - org.geotools - gt-main + gt-api ${gt-version} @@ -795,11 +700,23 @@ + + org.apache.maven.plugins + maven-compiler-plugin + + 3.8.1 + + 11 + true + UTF-8 + true + + org.apache.maven.plugins maven-resources-plugin - 2.6 + 3.3.1 UTF-8 @@ -808,7 +725,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.17 + 3.5.2 + - - - org.apache.maven.wagon - wagon-ftp - 2.10 - - - - - apache.snapshots Apache Snapshots Repository - http://people.apache.org/repo/m2-snapshot-repository + https://people.apache.org/repo/m2-snapshot-repository true @@ -866,7 +755,7 @@ apache.incubating Apache Incubating Repository - http://people.apache.org/repo/m2-incubating-repository + https://people.apache.org/repo/m2-incubating-repository false @@ -875,25 +764,6 @@ - - - - - - - diff --git a/src/services/webapp/pom.xml b/src/services/webapp/pom.xml index 8058d62d..195c6eef 100644 --- a/src/services/webapp/pom.xml +++ b/src/services/webapp/pom.xml @@ -11,12 +11,12 @@ org.geoserver.geofence geofence-root - 3.7-SNAPSHOT + 3.8-SNAPSHOT org.geoserver.geofence geofence-web-app - 3.7-SNAPSHOT + 3.8-SNAPSHOT war GeoFence - Webapp @@ -88,7 +88,7 @@ - dom4j + org.dom4j dom4j @@ -113,7 +113,7 @@ javax.servlet - servlet-api + javax.servlet-api provided @@ -238,7 +238,7 @@ - + + copy-dependencies copy - + configure the plugin here org.hibernatespatial @@ -270,7 +270,7 @@ - + --> @@ -287,7 +287,7 @@ https://github.com/geoserver/geofence/issues/97 --> - + diff --git a/src/services/webapp/src/main/resources/geofence-datasource-ovr.properties b/src/services/webapp/src/main/resources/geofence-datasource-ovr.properties index 4f4fd8e4..389f4b41 100644 --- a/src/services/webapp/src/main/resources/geofence-datasource-ovr.properties +++ b/src/services/webapp/src/main/resources/geofence-datasource-ovr.properties @@ -12,9 +12,9 @@ ## contains some info about each setting. ################################################################################ -geofenceVendorAdapter.databasePlatform=org.hibernatespatial.geodb.GeoDBDialect +geofenceVendorAdapter.databasePlatform=org.hibernate.spatial.dialect.h2geodb.GeoDBDialect geofenceDataSource.driverClassName=org.h2.Driver -geofenceDataSource.url=jdbc:h2:geofence_db/geofence +geofenceDataSource.url=jdbc:h2:${user.dir}/geofence_db/geofence geofenceDataSource.username=sa geofenceDataSource.password=sa geofenceEntityManagerFactory.jpaPropertyMap[hibernate.default_schema]=public