From 8161c5fd287e09c7b0dda2c4ddaa430859f92b19 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 18 Jan 2020 23:50:41 +0100 Subject: [PATCH] implemented login and registration (unfinished) --- src/main/java/envoy/server/data/User.java | 8 +++++-- .../server/database/PersistenceManager.java | 3 +++ .../processors/LoginCredentialProcessor.java | 24 ++++++++++++++++--- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index e596cca..c71e0ac 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -3,13 +3,15 @@ package envoy.server.data; import java.util.Date; import java.util.List; -import javax.persistence.ElementCollection; +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; @@ -31,6 +33,7 @@ import javax.persistence.TemporalType; @Table(name = "users") @NamedQueries( { @NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById"), + @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested } ) @@ -46,7 +49,8 @@ public class User { private Date lastSeen; private envoy.data.User.UserStatus status; - @ElementCollection + // TODO: Fix Error + @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts") private List contacts; /** diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index a61275f..0339be6 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -78,6 +78,9 @@ public class PersistenceManager { */ public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); } + public User getUserByName(String name) { + return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult(); + } /** * Searches for a {@link Message} with a specific id. * diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 3574f0e..b658445 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -23,12 +23,12 @@ import envoy.server.net.ObjectWriteProxy; * Created: 30.12.2019
* * @author Kai S. K. Engelbart + * @author Maximilian Käfer * @since Envoy Server Standalone v0.1-alpha */ public class LoginCredentialProcessor implements ObjectProcessor { - // TODO: Acquire user IDs from database - private static long currentUserId = 1; + private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager(); @Override public Class getInputClass() { return LoginCredentials.class; } @@ -38,7 +38,25 @@ public class LoginCredentialProcessor implements ObjectProcessor