implemented login and registration (unfinished)

This commit is contained in:
DieGurke 2020-01-18 23:50:41 +01:00
parent 5f6f94502a
commit 8161c5fd28
3 changed files with 30 additions and 5 deletions

View File

@ -3,13 +3,15 @@ package envoy.server.data;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import javax.persistence.ElementCollection; import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.NamedQueries; import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery; import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Temporal; import javax.persistence.Temporal;
import javax.persistence.TemporalType; import javax.persistence.TemporalType;
@ -31,6 +33,7 @@ import javax.persistence.TemporalType;
@Table(name = "users") @Table(name = "users")
@NamedQueries( @NamedQueries(
{ @NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById"), { @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 @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 Date lastSeen;
private envoy.data.User.UserStatus status; private envoy.data.User.UserStatus status;
@ElementCollection // TODO: Fix Error
@OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts")
private List<User> contacts; private List<User> contacts;
/** /**

View File

@ -78,6 +78,9 @@ public class PersistenceManager {
*/ */
public User getUserById(long id) { return (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); } 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. * Searches for a {@link Message} with a specific id.
* *

View File

@ -23,12 +23,12 @@ import envoy.server.net.ObjectWriteProxy;
* Created: <strong>30.12.2019</strong><br> * Created: <strong>30.12.2019</strong><br>
* *
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> { public class LoginCredentialProcessor implements ObjectProcessor<LoginCredentials> {
// TODO: Acquire user IDs from database private PersistenceManager persistenceManager = PersistenceManager.getPersistenceManager();
private static long currentUserId = 1;
@Override @Override
public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; } public Class<LoginCredentials> getInputClass() { return LoginCredentials.class; }
@ -38,7 +38,25 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId)); System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
// Create user // Create user
User user = new User(currentUserId++, input.getName()); User user = null;
if (input.isRegistration()) {
envoy.server.data.User dbUser = new envoy.server.data.User();
dbUser.setName(input.getName());
dbUser.setLastSeen(new Date());
dbUser.setStatus(User.UserStatus.ONLINE);
dbUser.setPasswordHash(input.getPasswordHash());
persistenceManager.addUser(dbUser);
user = dbUser.toCommonUser();
}else {
// TODO: Implement error when user does not exist
envoy.server.data.User requestedUser = persistenceManager.getUserByName(input.getName());
if (input.getPasswordHash() == requestedUser.getPasswordHash()) {
user = requestedUser.toCommonUser();
} else {
// TODO: Wrong Password Response
}
}
ConnectionManager.getInstance().registerUser(socketId, user.getId()); ConnectionManager.getInstance().registerUser(socketId, user.getId());
// Create contacts // Create contacts