{bcrypt} encryption added user password
This commit is contained in:
parent
285a41cb44
commit
cb53ca38e5
Binary file not shown.
@ -30,28 +30,20 @@ public class UserOpsDAOImpl implements UserOpsDAO{
|
|||||||
// get roles from the database
|
// get roles from the database
|
||||||
|
|
||||||
// Admin sets a username and password for the first time
|
// 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
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public ReturnStatus adminSetPasswordToDb(UserEntity userEntity) {
|
public ReturnStatus adminSetPasswordToDb(UserEntity userEntity) {
|
||||||
System.out.println("Entity manager Entered");
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (checkIfAdminExists(userEntity)) {
|
if (checkIfAdminExists(userEntity)) {
|
||||||
return new ReturnStatus(false, "Admin already exists", "");
|
return new ReturnStatus(false, "Admin already exists", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch existing ROLE_Admin from DB
|
// Fetch existing ROLE_Admin from DB
|
||||||
Role adminRole = (Role) entityManager.createQuery("FROM Role r WHERE r.role = :roleName")
|
Role adminRole = (Role) entityManager.createQuery("FROM Role r WHERE r.role = :roleName")
|
||||||
.setParameter("roleName", "ROLE_Admin")
|
.setParameter("roleName", "ROLE_Admin")
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
|
|
||||||
userEntity.setARole(adminRole);
|
userEntity.setARole(adminRole);
|
||||||
|
|
||||||
System.out.println("ADMIN_role id = "+adminRole.getId());
|
|
||||||
|
|
||||||
// Persist the user
|
// Persist the user
|
||||||
entityManager.persist(userEntity);
|
entityManager.persist(userEntity);
|
||||||
return new ReturnStatus(true, "Admin created", "");
|
return new ReturnStatus(true, "Admin created", "");
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import com.example.cezenPBX.DTO.ReturnStatus;
|
|||||||
import com.example.cezenPBX.entity.user.Role;
|
import com.example.cezenPBX.entity.user.Role;
|
||||||
import com.example.cezenPBX.entity.user.UserEntity;
|
import com.example.cezenPBX.entity.user.UserEntity;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -15,20 +16,19 @@ public class PbxUserService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private UserOpsDAO userOpsDAO;
|
private UserOpsDAO userOpsDAO;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
|
|
||||||
// must perform the sanity checks before being set to the database
|
// must perform the sanity checks before being set to the database
|
||||||
// method will return a faulty return status if the damin exists
|
// method will return a faulty return status if the damin exists
|
||||||
public ReturnStatus adminSetUserNamePasswordSet(String userName, String password, String confirmPassword){
|
public ReturnStatus adminSetUserNamePasswordSet(String userName, String password, String confirmPassword){
|
||||||
|
|
||||||
// password will be checked here
|
// password will be checked here
|
||||||
if(!password.equals(confirmPassword)){
|
if(!password.equals(confirmPassword)){
|
||||||
return new ReturnStatus(false, "Passwords do not match", "Passwords do not match");
|
return new ReturnStatus(false, "Passwords do not match", "Passwords do not match");
|
||||||
}
|
}
|
||||||
|
// password encryption
|
||||||
UserEntity userEntity = new UserEntity(userName, password);
|
UserEntity userEntity = new UserEntity(userName, "{bcrypt}"+passwordEncoder.encode(password));
|
||||||
//userEntity.setARole(new Role("ROLE_Admin"));
|
|
||||||
|
|
||||||
//System.out.println("Reached here already service layer");
|
|
||||||
|
|
||||||
// commit the username and password to the database
|
// commit the username and password to the database
|
||||||
return userOpsDAO.adminSetPasswordToDb(userEntity);
|
return userOpsDAO.adminSetPasswordToDb(userEntity);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user