java-api/src/main/java/restImplementation/UserImplementation.java
2019-04-16 18:24:33 +02:00

116 lines
4.1 KiB
Java

package restImplementation;
import blockchain.query.TransactionWrapper;
import database.user.User;
import database.user.UserDao;
import org.springframework.security.crypto.bcrypt.BCrypt;
import javax.json.JsonArray;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
public class UserImplementation {
public Map<String,String> 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<String,String> 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("user_hash",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 Boolean getUser(String email) throws Exception{
UserDao userDao = new UserDao();
return userDao.verifyUserExist(email);
}
public User getUserWithEmail(String email) throws Exception{
UserDao userDao = new UserDao();
return userDao.getUserWithEmail(email);
}
public User getUserWithPhone(String phone) throws Exception{
UserDao userDao = new UserDao();
return userDao.getUserWithPhone(phone);
}
public User getUserWithMailAndPhone(String email, String phone) throws Exception{
UserDao userDao = new UserDao();
return userDao.getUserWithMailAndPhone(email,phone);
}
public int getUserId(String user_hash, String user_email) throws Exception{
UserDao userDao = new UserDao();
return userDao.getUserIdWithHashAndEmail(user_hash,user_email);
}
public Map<String,String> userLogger(User user) throws Exception {
UserDao userDao = new UserDao();
Map<String,String> 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("user_hash",userResponse.getUser_hash());
}
} else {
response.put("response","Not Exist");
}
return response;
}
private String hashPassword(String plainTextPassword){
return BCrypt.hashpw(plainTextPassword,BCrypt.gensalt());
}
}