diff --git a/app/src/main/java/lightcontainer/domains/client/Context.java b/app/src/main/java/lightcontainer/domains/client/Context.java index 8e63323..5ff19c4 100644 --- a/app/src/main/java/lightcontainer/domains/client/Context.java +++ b/app/src/main/java/lightcontainer/domains/client/Context.java @@ -1,5 +1,7 @@ package lightcontainer.domains.client; +import lightcontainer.repository.ClientHandlerRepository; +import lightcontainer.repository.ReadOnlyClientRepository; import lightcontainer.storage.File; import lightcontainer.storage.ReadOnlyFile; import lightcontainer.storage.Repository; @@ -18,6 +20,7 @@ public class Context { // Variables private final Repository repository; private RequestBundle requestBundle; + private ReadOnlyClientRepository clientRepository; /** * Login de l'utilisateur @@ -30,8 +33,9 @@ public class Context { private String domain; // Constructeur - public Context(Repository repository) { + public Context(Repository repository, ReadOnlyClientRepository clientRepository) { this.repository = repository; + this.clientRepository = clientRepository; } @@ -43,6 +47,10 @@ public class Context { * @return TRUE si l'utilisateur a pu être créé */ public boolean createUser(String login, String password) { + if (clientRepository.hasClient(login)) { // Empêcher double connection + return false; + } + try { String key = AES_GCM.generateSecretKey(); String hashedPassword = BCryptHasher.hashPassword(password); @@ -73,7 +81,12 @@ public class Context { * @return TRUE si l'utilisateur a été authentifié */ public boolean signIn(String login, String password) { - if (this.repository.verifyUser(login, password)) { + if (login == null || password == null) { + return false; + } + + // Empêcher double connection + if (!clientRepository.hasClient(login) && this.repository.verifyUser(login, password)) { this.login = login; return true; } diff --git a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java index 5603d5f..cc704e2 100644 --- a/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java +++ b/app/src/main/java/lightcontainer/domains/server/UnicastServerListener.java @@ -55,7 +55,7 @@ public class UnicastServerListener implements Runnable { SSLSocket client = (SSLSocket) this.server.accept(); // Create a new Handler client by passing these dependencies to it - ClientHandler clientHandler = new ClientHandler(this.repository, client, ffe, protocolRep, new Context(repositoryStorage)); + ClientHandler clientHandler = new ClientHandler(this.repository, client, ffe, protocolRep, new Context(repositoryStorage, repository)); // Add the client handler to its repository (clienthandlerrepository) this.repository.addClient(clientHandler); // Start the thread diff --git a/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java b/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java index 21441b3..51f54ef 100644 --- a/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java +++ b/app/src/main/java/lightcontainer/interfaces/UnicastCHR.java @@ -5,11 +5,12 @@ import lightcontainer.domains.server.MulticastServerListener; import lightcontainer.domains.server.UnicastServerListener; import lightcontainer.protocol.ProtocolWriter; import lightcontainer.repository.ClientHandlerRepository; +import lightcontainer.repository.ReadOnlyClientRepository; /** * A communication interface between a {@link ClientHandler} and the {@link ClientHandlerRepository}. */ -public interface UnicastCHR { +public interface UnicastCHR extends ReadOnlyClientRepository { /** * Setter, allow to define the ServerListener of a repository. * @@ -24,6 +25,7 @@ public interface UnicastCHR { */ void addClient(ClientHandler client); + /** * Permet de demander la déconnection d'un client * diff --git a/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java index 9c69784..b1f1731 100644 --- a/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java +++ b/app/src/main/java/lightcontainer/repository/ClientHandlerRepository.java @@ -55,6 +55,22 @@ public class ClientHandlerRepository implements AutoCloseable, UnicastCHR { this.handlers.add(client); } + /** + * Permet de savoir si un client est déjà connecté avec ce login. + * + * @param login Login du client. + */ + @Override + public boolean hasClient(String login) { + for (ClientHandler client : handlers) { + if (client.getLogin() != null && client.getLogin().equals(login)) { + return true; + } + } + return false; + } + + @Override public void disconnect(ClientHandler client) { if (handlers.remove(client)) diff --git a/app/src/main/java/lightcontainer/repository/ReadOnlyClientRepository.java b/app/src/main/java/lightcontainer/repository/ReadOnlyClientRepository.java new file mode 100644 index 0000000..6244a2c --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/ReadOnlyClientRepository.java @@ -0,0 +1,12 @@ +package lightcontainer.repository; + +public interface ReadOnlyClientRepository { + + /** + * Permet de savoir si un client est déjà connecté avec ce login. + * + * @param login Login du client. + */ + boolean hasClient(String login); + +}