package com.sudoku.data.manager;

/**
 *
 * @author clesaege
 */

import com.sudoku.data.model.User;
import org.springframework.security.crypto.codec.Base64;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public final class UserManager { // This is the manager for users.
  /*
  * Attributs
  */
  private static volatile UserManager instance = null;
  private User loggedUser;
  private List<User> users;

  private UserManager() {
    this.loggedUser = null;
    this.users = new ArrayList<User>(); //Will need to load and deserialise
  }

  public final static UserManager getInstance() {
    if (instance == null) {
      synchronized (UserManager.class) {
        if (instance == null) {
          instance = new UserManager();
        }
      }
    }
    return instance;
  }

  public User getLoggedUser() {
    return this.loggedUser;
  }

  public List<User> getAllUsers() {
    return this.users;
  }

  public boolean addUser(User u) {
    return this.users.add(u);
  }

  public boolean removeUser(User u) {
    return users.remove(u);
  }

  public User authenticate(String pseudo, String password)
      throws NoSuchAlgorithmException, UnsupportedEncodingException {
    MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
    for (User u : this.users) { //For all users
      if (u.getPseudo() == pseudo) //For all correspoding pseudos
      {
        String toBeHashed = password + u.getSalt();
        if (new String(Base64.encode(
              mDigest.digest(toBeHashed.getBytes("UTF-8"))))
            .equals(u.getPassword())) { //If the hash of pwd+salt is good
          loggedUser = u; // If the password is correct, log the user
          return u; // and return the identified user
        }
      }
    }
    return null; // If we didn't manage to authenticate
  }

  public User createUser() {
    return null;
  }

  public List<User> getConnectedUsers() {
    return null; // Appeler com
  }

}

       

@@ -508,36 +508,177 @@
 ser;
-%0A  private List%3CUser%3E users;
+ // The user actualy logged on this machine%0A  private List%3CUser%3E localUsers; // Local Users%0A  private List%3CUser%3E distantUsers; // Users connected but not on this machine
 %0A%0A  
@@ -734,25 +734,30 @@
 l;%0A    this.
-u
+localU
 sers = new A
@@ -769,19 +769,16 @@
 ist%3C
-User
 %3E(); //
+ 
 Will
@@ -807,16 +807,62 @@
 rialise%0A
+    this.distantUsers = new ArrayList%3C%3E(); //%0A
   %7D%0A%0A  p
@@ -1187,18 +1187,20 @@
 ser%3E get
-Al
+Loca
 lUsers()
@@ -1193,32 +1193,33 @@
 etLocalUsers() %7B
+ 
 %0A    return this
@@ -1215,25 +1215,105 @@
 return this.
-u
+localUsers;%0A  %7D%0A  public List%3CUser%3E getDistantUsers() %7B %0A    return this.distantU
 sers;%0A  %7D%0A%0A 
@@ -1306,24 +1306,26 @@
 tUsers;%0A  %7D%0A
+  
 %0A  public bo
@@ -1333,16 +1333,21 @@
 lean add
+Local
 User(Use
@@ -1365,25 +1365,113 @@
 return this.
-u
+localUsers.add(u);%0A  %7D%0A  public boolean addDistantUser(User u) %7B%0A    return this.distantU
 sers.add(u);
@@ -1466,32 +1466,35 @@
 sers.add(u);%0A  %7D
+%0A  
 %0A%0A  public boole
@@ -1502,16 +1502,21 @@
 n remove
+Local
 User(Use
@@ -1533,17 +1533,106 @@
  return 
-u
+localUsers.remove(u);%0A  %7D%0A  public boolean removeDistantUser(User u) %7B%0A    return distantU
 sers.rem
@@ -1862,17 +1862,22 @@
  : this.
-u
+localU
 sers) %7B 
@@ -1919,19 +1919,24 @@
 do()
- == 
+.equals(
 pseudo)
+)
  //F
@@ -2399,24 +2399,89 @@
 enticate%0A  %7D
+%0A  %0A  public void logoff()%0A  %7B%0A      this.loggedUser=null;%0A  %7D%0A  
 %0A%0A  public U

Add distant users