package restImplementation; import blockchain.query.TransactionWrapper; import database.user.User; import database.user.UserDao; import org.springframework.security.crypto.bcrypt.BCrypt; import java.time.Instant; import java.util.HashMap; import java.util.Map; public class UserImplementation { public Map registerUser(User user) throws Exception { TransactionWrapper transactionWrapper = new TransactionWrapper(); UserDao userDao = new UserDao(); //CREATE USER HASH. TEMPORARY USER HASH IS : name+email+firstname+association user.setUser_hash(hashPassword(user.getName()+user.getEmail()+user.getFirstname()+user.getAssociation())); System.out.println("user hash: "+user.getUser_hash()); //REGISTER IN BLOCKCHAIN if(user.getPhone()==null){ user.setPhone("0000000000"); } String[] userInfos = new String[]{user.getUser_hash(),user.getName(),user.getFirstname(),""+user.getPhone(),user.getAssociation()}; transactionWrapper.sendTransaction("registerUser",userInfos); //REGISTER IN REPLICA DB FOR BI Map response = new HashMap<>(); User dbUser = userDao.getUserWithEmail(user.getEmail()); // check if user exist if(dbUser != null){ response.put("response","false"); return response; }else { user.setPassword(hashPassword(user.getPassword())); long now = Instant.now().toEpochMilli(); user.setCreation_date(now); user.setModification_date(now); user.setVerified(true); user.setApproved(false); userDao.addUser(user); response.put("userHash",user.getUser_hash()); response.put("response","true"); return response; } } public void approveUser(User user) throws Exception{ TransactionWrapper transactionWrapper = new TransactionWrapper(); UserDao userDao = new UserDao(); transactionWrapper.sendTransaction("setUserPermission",new String[]{user.getUser_hash()}); userDao.approveUser(user.getEmail()); } public void deleteUser(User user) throws Exception { TransactionWrapper transactionWrapper = new TransactionWrapper(); transactionWrapper.sendTransaction("deleteUser",new String[]{user.getUser_hash()}); UserDao userDao = new UserDao(); userDao.deleteUser(user.getEmail()); } public User getUser(String email, String password) throws Exception{ UserDao userDao = new UserDao(); User user1 = userDao.getUserWithEmail(email); if(user1 != null){ String hash = user1.getPassword(); if(BCrypt.checkpw(password, hash)){ return user1; }else{ return null; } }else { return null; } } public int getUserId(String user_hash, String user_email) throws Exception{ UserDao userDao = new UserDao(); return userDao.getUserIdWithHashAndEmail(user_hash,user_email); } public Map userLogger(User user) throws Exception { UserDao userDao = new UserDao(); Map response = new HashMap<>(); User userResponse = userDao.getUserWithEmail(user.getEmail()); if(userResponse != null){ String hash = userResponse.getPassword(); if(!BCrypt.checkpw(user.getPassword(), hash)){ response.put("response","Not Allowed"); }else{ response.put("response","true"); response.put("userHash",userResponse.getUser_hash()); } } else { response.put("response","Not Exist"); } return response; } private String hashPassword(String plainTextPassword){ return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt()); } }