{bcrypt} encryption added user password

This commit is contained in:
MathewFrancis 2025-05-14 12:05:20 +05:30
parent 285a41cb44
commit cb53ca38e5
3 changed files with 6 additions and 14 deletions

View File

@ -30,28 +30,20 @@ public class UserOpsDAOImpl implements UserOpsDAO{
// get roles from the database
// Admin sets a username and password for the first time
// TODO make sure you Hash the password
// TODO prepend {bcrypt} before commiting the password
// TODO admin can only set the password once
@Override
@Transactional
public ReturnStatus adminSetPasswordToDb(UserEntity userEntity) {
System.out.println("Entity manager Entered");
try {
if (checkIfAdminExists(userEntity)) {
return new ReturnStatus(false, "Admin already exists", "");
}
// Fetch existing ROLE_Admin from DB
Role adminRole = (Role) entityManager.createQuery("FROM Role r WHERE r.role = :roleName")
.setParameter("roleName", "ROLE_Admin")
.getSingleResult();
userEntity.setARole(adminRole);
System.out.println("ADMIN_role id = "+adminRole.getId());
// Persist the user
entityManager.persist(userEntity);
return new ReturnStatus(true, "Admin created", "");

View File

@ -5,6 +5,7 @@ import com.example.cezenPBX.DTO.ReturnStatus;
import com.example.cezenPBX.entity.user.Role;
import com.example.cezenPBX.entity.user.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
@ -15,20 +16,19 @@ public class PbxUserService {
@Autowired
private UserOpsDAO userOpsDAO;
@Autowired
private PasswordEncoder passwordEncoder;
// must perform the sanity checks before being set to the database
// method will return a faulty return status if the damin exists
public ReturnStatus adminSetUserNamePasswordSet(String userName, String password, String confirmPassword){
// password will be checked here
if(!password.equals(confirmPassword)){
return new ReturnStatus(false, "Passwords do not match", "Passwords do not match");
}
UserEntity userEntity = new UserEntity(userName, password);
//userEntity.setARole(new Role("ROLE_Admin"));
//System.out.println("Reached here already service layer");
// password encryption
UserEntity userEntity = new UserEntity(userName, "{bcrypt}"+passwordEncoder.encode(password));
// commit the username and password to the database
return userOpsDAO.adminSetPasswordToDb(userEntity);