Skip to content

Commit

Permalink
Merge pull request #315 from sumeetphadnis/next
Browse files Browse the repository at this point in the history
Enable eeuser module
  • Loading branch information
ar authored Sep 19, 2024
2 parents a6407da + 4b93b46 commit 78eac97
Show file tree
Hide file tree
Showing 17 changed files with 92 additions and 110 deletions.
11 changes: 5 additions & 6 deletions modules/eeuser/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ description = 'jPOS-EE :: User Module'
dependencies {
api project(':modules:dbsupport')
testImplementation project(':modules:db-h2')
api libraries.commons_lang
api libs.commonsLang3
}

ext {
testRuntimeDir = "$buildDir/runtime" as File
testRuntimeDir = "$layout.buildDirectory/runtime" as File
}

task jposeeSetup(dependsOn: 'classes', type: JavaExec) {
tasks.register('jposeeSetup', JavaExec) {
dependsOn 'classes'
classpath = sourceSets.test.runtimeClasspath
main = 'org.jpos.q2.install.Install'
mainClass = 'org.jpos.q2.install.Install'
args = ["--quiet", "--force", "--outputDir=" + testRuntimeDir]
}

Expand All @@ -21,5 +22,3 @@ test {
scanForTestClasses true
workingDir testRuntimeDir
}

apply from: "${rootProject.projectDir}/jpos-app.gradle"
11 changes: 5 additions & 6 deletions modules/eeuser/src/main/java/org/jpos/ee/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public class Consumer extends Cloneable implements Serializable, SoftDelete {

public Consumer() {
super();
roles = new LinkedHashSet<Role> ();
props = new LinkedHashMap<String,String>();
roles = new LinkedHashSet<> ();
props = new LinkedHashMap<>();
}
public String getId() {
return id;
Expand Down Expand Up @@ -130,7 +130,7 @@ public void removeAllRoles () {
public String getRolesAsString () {
StringBuilder sb = new StringBuilder();
for (Role r : roles) {
if (sb.length() > 0)
if (!sb.isEmpty())
sb.append (", ");
sb.append (r.getName());
}
Expand All @@ -140,7 +140,7 @@ public void setProps (Map<String,String> props) {
this.props = props;
}
public Map<String,String> getProps () {
return (props = props == null ? new HashMap<String,String> () : props);
return Objects.requireNonNullElseGet(props, HashMap::new);
}
public void set (String prop, String value) {
getProps().put (prop, value);
Expand All @@ -162,8 +162,7 @@ public String toString() {
}

public boolean equals(Object other) {
if ( !(other instanceof Consumer) ) return false;
Consumer castOther = (Consumer) other;
if ( !(other instanceof Consumer castOther) ) return false;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
Expand Down
4 changes: 2 additions & 2 deletions modules/eeuser/src/main/java/org/jpos/ee/ConsumerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

package org.jpos.ee;

import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.hibernate.HibernateException;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.*;

@SuppressWarnings("unused")
Expand Down
29 changes: 15 additions & 14 deletions modules/eeuser/src/main/java/org/jpos/ee/HashVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;

public enum HashVersion {
UNKNOWN((byte)0xFF, 0, 0, 0, null) {
Expand Down Expand Up @@ -58,7 +59,7 @@ public boolean check (String seed, String secret, String hash) throws NoSuchAlgo
},
ONE((byte) 1,100000,2048, 388, Base64.decode("K7f2dgQQHK5CW6Wz+CscUA==")) {
@Override
public String hash (String seed, String secret, byte[] salt) throws Exception {
public String hash (String seed, String secret, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
if (salt == null) {
salt = ONE.genSalt();
}
Expand All @@ -76,19 +77,19 @@ public String hash (String seed, String secret, byte[] salt) throws Exception {
@Override
public boolean check (String seed, String secret, String hash) throws Exception {
byte[] b = Base64.decode(hash);
byte[] salt = new byte[ONE.getSalt().length];
System.arraycopy (b, 1, salt, 0, salt.length);
String computedHash = ONE.hash(seed, secret, ONE.getSalt(salt));
byte[] s = new byte[ONE.getSalt().length];
System.arraycopy (b, 1, s, 0, s.length);
String computedHash = ONE.hash(seed, secret, ONE.getSalt(s));
return computedHash.equals(hash);
}

};

private byte version;
private int iterations;
private int keylength;
private int encodedLength;
private byte[] salt;
private final byte version;
private final int iterations;
private final int keylength;
private final int encodedLength;
private final byte[] salt;

HashVersion (byte version, int iterations, int keylength, int encodedLength, byte[] salt) {
this.version = version;
Expand Down Expand Up @@ -136,14 +137,14 @@ public static HashVersion getVersion (String hash) {
public abstract String hash (String seed, String secret, byte[] salt) throws Exception;
public abstract boolean check (String seed, String secret, String hash) throws Exception;

private byte[] genSalt () throws NoSuchAlgorithmException {
private byte[] genSalt () {
return genSalt(salt.length);
}

private byte[] genSalt(int len) throws NoSuchAlgorithmException {
private byte[] genSalt(int len) {
SecureRandom sr = new SecureRandom();
byte[] salt = new byte[len];
sr.nextBytes(salt);
return salt;
byte[] s = new byte[len];
sr.nextBytes(s);
return s;
}
}
2 changes: 1 addition & 1 deletion modules/eeuser/src/main/java/org/jpos/ee/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String getName () {
return name;
}
public boolean equals (Object obj) {
return this == obj || obj instanceof Permission && this.getName().equals(((Permission) obj).getName());
return this == obj || (obj instanceof Permission o) && this.getName().equals(o.getName());
}
public String toString () {
return getName ();
Expand Down
5 changes: 4 additions & 1 deletion modules/eeuser/src/main/java/org/jpos/ee/Realm.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

import org.hibernate.annotations.CacheConcurrencyStrategy;

import javax.persistence.*;
import jakarta.persistence.*;

import java.io.Serial;
import java.io.Serializable;
import java.util.Objects;

@Entity
@Table(name = "realm", uniqueConstraints={@UniqueConstraint(columnNames={"name"}, name="UK_realm_name")})
@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Realm extends Cloneable implements Serializable {
@Serial
private static final long serialVersionUID = -5382324365781372973L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
39 changes: 16 additions & 23 deletions modules/eeuser/src/main/java/org/jpos/ee/RevisionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,34 @@
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.criteria.internal.OrderImpl;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import org.hibernate.query.Query;

@SuppressWarnings("unused")
public class RevisionManager extends DBManager<Revision> {

public RevisionManager (DB db) {
super(db,Revision.class);
}
@SuppressWarnings("unchecked")

public List<Revision> getRevisionsByRef (String ref)
throws HibernateException
{
Criteria crit = db.session().createCriteria (Revision.class)
.add (Restrictions.eq ("ref", ref))
.addOrder (Order.desc("id"));
return (List<Revision>) crit.list();
Query<Revision> q = db.session().createQuery("from revision where ref = :ref order by id desc");
q.setParameter("ref", ref);
return q.list();
}
@SuppressWarnings("unchecked")

public List<Revision> getRevisionsByAuthor (User author)
throws HibernateException
{
Criteria crit = db.session().createCriteria (Revision.class)
.add (Restrictions.eq ("author", author))
.addOrder (Order.desc("id"));
return (List<Revision>) crit.list();
Query<Revision> q = db.session().createQuery("from revision where author = :author order by id desc");
q.setParameter("author", author);
return q.list();
}

//overridden to avoid LazyInitializationExc
Expand All @@ -66,12 +61,11 @@ public List<Revision> getAll(int offset, int limit, Map<String,Boolean> orders)
Root<Revision> root = query.from(Revision.class);
// To avoid LazyInitializationExc
root.fetch("author");
List<javax.persistence.criteria.Order> orderList = new ArrayList<>();
List<jakarta.persistence.criteria.Order> orderList = new ArrayList<>();
//ORDERS
for (Map.Entry<String,Boolean> entry : orders.entrySet()) {
OrderImpl order = new OrderImpl(root.get(entry.getKey()),entry.getValue());
orderList.add(order);
}
orders.forEach((key, value) ->
orderList.add(Boolean.TRUE.equals(value) ? criteriaBuilder.asc(root.get(key)) : criteriaBuilder.desc(root.get(key)))
);
query.select(root);
query.orderBy(orderList);
return db.session().createQuery(query)
Expand All @@ -81,7 +75,6 @@ public List<Revision> getAll(int offset, int limit, Map<String,Boolean> orders)
}



/**
* factory method used to create a Revision associated with this user.
*
Expand Down
17 changes: 8 additions & 9 deletions modules/eeuser/src/main/java/org/jpos/ee/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public class User extends Cloneable implements Serializable, SoftDelete {

public User() {
super();
roles = new LinkedHashSet<Role> ();
passwordhistory = new LinkedList<PasswordHistory> ();
roles = new LinkedHashSet<> ();
passwordhistory = new LinkedList<> ();
}
public String getNick() {
return nick;
Expand Down Expand Up @@ -191,7 +191,7 @@ public void removeAllRoles () {
public String getRolesAsString () {
StringBuilder sb = new StringBuilder();
for (Role r : roles) {
if (sb.length() > 0)
if (!sb.isEmpty())
sb.append (", ");
sb.append (r.getName());
}
Expand All @@ -201,7 +201,7 @@ public String getRealmsAsString () {
StringBuilder sb = new StringBuilder();
for (Role r : roles) {
if (r.getRealm() != null) {
if (sb.length() > 0)
if (!sb.isEmpty())
sb.append (", ");
sb.append (r.getRealm().getName());
}
Expand All @@ -218,14 +218,14 @@ public boolean containsPasswordHistoryValue (String passwordhistoryhash) {
}
public void prunePasswordHistoryValue (int numPasswordHistoryValue) {
while (passwordhistory.size() > numPasswordHistoryValue) {
passwordhistory.remove(0);
passwordhistory.removeFirst();
}
}
public void setProps (Map<String,String> props) {
this.props = props;
}
public Map<String,String> getProps () {
return (props = props == null ? new HashMap<String,String> () : props);
return Objects.requireNonNullElseGet(props, HashMap::new);
}
public void set (String prop, String value) {
getProps().put (prop, value);
Expand All @@ -247,8 +247,7 @@ public String toString() {
.toString();
}
public boolean equals(Object other) {
if ( !(other instanceof User) ) return false;
User castOther = (User) other;
if ( !(other instanceof User castOther) ) return false;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
Expand All @@ -265,7 +264,7 @@ public int hashCode() {
public String getNickAndId() {
StringBuilder sb = new StringBuilder (getNick());
sb.append ('(');
sb.append (Long.toString(getId()));
sb.append (getId());
sb.append (')');
return sb.toString();
}
Expand Down
Loading

0 comments on commit 78eac97

Please sign in to comment.