mirror of
				https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
				synced 2025-11-03 18:43:06 +00:00 
			
		
		
		
	Module Server, packg module.account : création impl IAccountDao nommée FileAccountDao, suppr TODO associés (fait); création clss AccountEntry
This commit is contained in:
		@@ -0,0 +1,58 @@
 | 
			
		||||
package com.pqt.server.module.account;
 | 
			
		||||
 | 
			
		||||
import com.pqt.core.entities.user_account.AccountLevel;
 | 
			
		||||
 | 
			
		||||
import java.io.Serializable;
 | 
			
		||||
 | 
			
		||||
public class AccountEntry implements Serializable{
 | 
			
		||||
    private String username, passwordHash, salt;
 | 
			
		||||
    private AccountLevel level;
 | 
			
		||||
 | 
			
		||||
    public AccountEntry() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AccountEntry(String username, String passwordHash, String salt, AccountLevel level) {
 | 
			
		||||
        this.username = username;
 | 
			
		||||
        this.passwordHash = passwordHash;
 | 
			
		||||
        this.salt = salt;
 | 
			
		||||
        this.level = level;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String getUsername() {
 | 
			
		||||
        return username;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String getPasswordHash() {
 | 
			
		||||
        return passwordHash;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    String getSalt() {
 | 
			
		||||
        return salt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AccountLevel getLevel() {
 | 
			
		||||
        return level;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean equals(Object o) {
 | 
			
		||||
        if (this == o) return true;
 | 
			
		||||
        if (o == null || getClass() != o.getClass()) return false;
 | 
			
		||||
 | 
			
		||||
        AccountEntry that = (AccountEntry) o;
 | 
			
		||||
 | 
			
		||||
        if (!username.equals(that.username)) return false;
 | 
			
		||||
        if (!passwordHash.equals(that.passwordHash)) return false;
 | 
			
		||||
        if (!salt.equals(that.salt)) return false;
 | 
			
		||||
        return level == that.level;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int hashCode() {
 | 
			
		||||
        int result = username.hashCode();
 | 
			
		||||
        result = 31 * result + passwordHash.hashCode();
 | 
			
		||||
        result = 31 * result + salt.hashCode();
 | 
			
		||||
        result = 31 * result + level.hashCode();
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -9,18 +9,18 @@ public class AccountService {
 | 
			
		||||
	private IAccountDao dao;
 | 
			
		||||
 | 
			
		||||
    public AccountService() {
 | 
			
		||||
        //TODO add dao instanciation
 | 
			
		||||
		dao = new FileAccountDao();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isAccountConnected(Account acc) {
 | 
			
		||||
		return dao.isAccountConnected(acc);
 | 
			
		||||
    public boolean isAccountConnected(Account account) {
 | 
			
		||||
		return dao.isAccountConnected(account);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean setAccountConnected(Account acc, boolean connected) {
 | 
			
		||||
		return dao.setAccountConnected(acc, connected);
 | 
			
		||||
	public boolean submitAccountCredentials(Account acc, boolean desiredState) {
 | 
			
		||||
		return dao.submitAccountCredentials(acc, desiredState);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public boolean isAccountRegistered(Account acc){
 | 
			
		||||
		return dao.isAccountRegistered(acc);
 | 
			
		||||
	public boolean isAccountRegistered(Account account){
 | 
			
		||||
		return dao.isAccountRegistered(account);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,93 @@
 | 
			
		||||
package com.pqt.server.module.account;
 | 
			
		||||
 | 
			
		||||
import com.pqt.core.entities.user_account.Account;
 | 
			
		||||
import com.pqt.server.tools.io.ISerialFileManager;
 | 
			
		||||
import com.pqt.server.tools.io.SimpleSerialFileManagerFactory;
 | 
			
		||||
import com.pqt.server.tools.security.IHashTool;
 | 
			
		||||
import com.pqt.server.tools.security.MD5HashTool;
 | 
			
		||||
 | 
			
		||||
import java.util.*;
 | 
			
		||||
 | 
			
		||||
//TODO écrire Javadoc
 | 
			
		||||
//TODO ajouter logs
 | 
			
		||||
public class FileAccountDao implements IAccountDao {
 | 
			
		||||
 | 
			
		||||
    private static final String ACCOUNT_FILE_NAME = "acc.pqt";
 | 
			
		||||
 | 
			
		||||
    private Set<AccountEntry> accountEntries;
 | 
			
		||||
    private Set<AccountEntry> connectedAccount;
 | 
			
		||||
    private IHashTool hashTool;
 | 
			
		||||
    private ISerialFileManager<AccountEntry> fileManager;
 | 
			
		||||
 | 
			
		||||
    public FileAccountDao() {
 | 
			
		||||
        accountEntries = new HashSet<>();
 | 
			
		||||
        connectedAccount = new HashSet<>();
 | 
			
		||||
        hashTool = new MD5HashTool();
 | 
			
		||||
        fileManager = SimpleSerialFileManagerFactory.getFileManager(AccountEntry.class, ACCOUNT_FILE_NAME);
 | 
			
		||||
        loadFromFile();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private AccountEntry lookupMatchingEntry(Account account, Collection<AccountEntry> entries){
 | 
			
		||||
        return entries.stream().filter(accountEntry -> accountEntry.getUsername().equals(account.getUsername())).findFirst().orElse(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isAccountConnected(Account account) {
 | 
			
		||||
        return lookupMatchingEntry(account, connectedAccount)!=null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean submitAccountCredentials(Account acc, boolean desiredState) {
 | 
			
		||||
        if(isAccountRegistered(acc)){
 | 
			
		||||
            if(desiredState!=isAccountConnected(acc)){
 | 
			
		||||
                if(desiredState)
 | 
			
		||||
                    return connect(acc);
 | 
			
		||||
                else
 | 
			
		||||
                    return disconnect(acc);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean connect(Account account){
 | 
			
		||||
        Optional<AccountEntry> entry = accountEntries.stream().filter(accountEntry -> accountEntry.getUsername().equals(account.getUsername())).findFirst();
 | 
			
		||||
        if(!entry.isPresent())
 | 
			
		||||
            return false;
 | 
			
		||||
        else{
 | 
			
		||||
            String expectedUsername = entry.get().getUsername();
 | 
			
		||||
            String expectedPasswordHash = entry.get().getPasswordHash();
 | 
			
		||||
            String salt = entry.get().getSalt();
 | 
			
		||||
 | 
			
		||||
            if(expectedUsername.equals(account.getUsername()) && hashTool.hashAndSalt(account.getPassword(), salt).equals(expectedPasswordHash)){
 | 
			
		||||
                connectedAccount.add(entry.get());
 | 
			
		||||
                return true;
 | 
			
		||||
            }else
 | 
			
		||||
                return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private boolean disconnect(Account account){
 | 
			
		||||
        Optional<AccountEntry> entry = accountEntries.stream().filter(accountEntry -> accountEntry.getUsername().equals(account.getUsername())).findFirst();
 | 
			
		||||
        if(entry.isPresent() && connectedAccount.contains(entry.get())){
 | 
			
		||||
            connectedAccount.remove(entry.get());
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isAccountRegistered(Account account) {
 | 
			
		||||
        return lookupMatchingEntry(account, accountEntries)!=null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void saveToFile(){
 | 
			
		||||
        fileManager.saveSetToFile(accountEntries);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void loadFromFile(){
 | 
			
		||||
        this.accountEntries = new HashSet<>(fileManager.loadSetFromFile());
 | 
			
		||||
        //TODO faire check des comptes au lieu de tout déconnecter?
 | 
			
		||||
        this.connectedAccount.clear();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -3,12 +3,11 @@ package com.pqt.server.module.account;
 | 
			
		||||
import com.pqt.core.entities.user_account.Account;
 | 
			
		||||
 | 
			
		||||
//TODO écrire Javadoc
 | 
			
		||||
//TODO créer implémentaiton
 | 
			
		||||
public interface IAccountDao {
 | 
			
		||||
 | 
			
		||||
	boolean isAccountConnected(Account acc);
 | 
			
		||||
	boolean isAccountConnected(Account account);
 | 
			
		||||
 | 
			
		||||
	boolean setAccountConnected(Account acc, boolean connected);
 | 
			
		||||
	boolean submitAccountCredentials(Account acc, boolean desiredState);
 | 
			
		||||
 | 
			
		||||
	boolean isAccountRegistered(Account acc);
 | 
			
		||||
	boolean isAccountRegistered(Account account);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user