updated code for create user api
This commit is contained in:
@@ -163,4 +163,8 @@ public class GepafinConstant {
|
|||||||
public static final String TOTAL_STEPS_NOT_BE_ZERO="total.steps.not.zero";
|
public static final String TOTAL_STEPS_NOT_BE_ZERO="total.steps.not.zero";
|
||||||
public static final String COMPLETED_STEPS_NOT_VALID="completed.steps.not.valid";
|
public static final String COMPLETED_STEPS_NOT_VALID="completed.steps.not.valid";
|
||||||
public static final String FIELD_ID_NOT_FOUND="field.id.not.found";
|
public static final String FIELD_ID_NOT_FOUND="field.id.not.found";
|
||||||
|
public static final String VALIDATE_EMAIL = "validate.email";
|
||||||
|
public static final String ROLE_ID_MANDATORY = "role.id.mandatory";
|
||||||
|
public static final String VALIDATE_PASSWORD = "validate.password";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import net.gepafin.tendermanagement.config.Translator;
|
|||||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||||
import net.gepafin.tendermanagement.entities.RegionEntity;
|
import net.gepafin.tendermanagement.entities.RegionEntity;
|
||||||
import net.gepafin.tendermanagement.entities.RoleEntity;
|
import net.gepafin.tendermanagement.entities.RoleEntity;
|
||||||
|
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
|
||||||
import net.gepafin.tendermanagement.model.request.RoleReq;
|
import net.gepafin.tendermanagement.model.request.RoleReq;
|
||||||
import net.gepafin.tendermanagement.model.response.RegionResponseBean;
|
import net.gepafin.tendermanagement.model.response.RegionResponseBean;
|
||||||
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
|
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
|
||||||
@@ -119,4 +120,8 @@ public class RoleDao {
|
|||||||
log.info("Total roles found: {}", roles.size());
|
log.info("Total roles found: {}", roles.size());
|
||||||
return roles;
|
return roles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public RoleEntity getRoleByType(RoleStatusEnum roleStatus) {
|
||||||
|
return roleRepository.findByRoleType(roleStatus.getValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import net.gepafin.tendermanagement.config.Translator;
|
|||||||
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
import net.gepafin.tendermanagement.constants.GepafinConstant;
|
||||||
import net.gepafin.tendermanagement.entities.RoleEntity;
|
import net.gepafin.tendermanagement.entities.RoleEntity;
|
||||||
import net.gepafin.tendermanagement.entities.UserEntity;
|
import net.gepafin.tendermanagement.entities.UserEntity;
|
||||||
|
import net.gepafin.tendermanagement.enums.RoleStatusEnum;
|
||||||
import net.gepafin.tendermanagement.enums.UserStatusEnum;
|
import net.gepafin.tendermanagement.enums.UserStatusEnum;
|
||||||
import net.gepafin.tendermanagement.model.request.*;
|
import net.gepafin.tendermanagement.model.request.*;
|
||||||
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
|
import net.gepafin.tendermanagement.model.response.RoleResponseBean;
|
||||||
@@ -28,6 +29,8 @@ import org.springframework.stereotype.Repository;
|
|||||||
|
|
||||||
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
|
import static net.gepafin.tendermanagement.util.Utils.setIfUpdated;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public class UserDao {
|
public class UserDao {
|
||||||
|
|
||||||
@@ -46,32 +49,57 @@ public class UserDao {
|
|||||||
private RoleDao roleDao;
|
private RoleDao roleDao;
|
||||||
|
|
||||||
public JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq) {
|
public JWTToken createUser(HttpServletRequest request, String tempToken, UserReq userReq) {
|
||||||
log.info("Creating user with email: {}", userReq.getEmail());
|
|
||||||
if (userRepository.existsByEmailIgnoreCase(userReq.getEmail())) {
|
if (Boolean.FALSE.equals(isValidEmail(userReq.getEmail()))) {
|
||||||
log.error("User creation failed: Email {} already exists", userReq.getEmail());
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
|
Translator.toLocale(GepafinConstant.VALIDATE_EMAIL));
|
||||||
}
|
}
|
||||||
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale())) && userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
|
log.info("Creating user with email: {}", userReq.getEmail());
|
||||||
log.error("User creation failed: CodiceFiscale {} already exists", userReq.getCodiceFiscale());
|
if (userRepository.existsByEmailIgnoreCase(userReq.getEmail())) {
|
||||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.CODICE_FISCALE_EXISTS));
|
log.error("User creation failed: Email {} already exists", userReq.getEmail());
|
||||||
}
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||||
if(tempToken == null && (StringUtils.isEmpty(userReq.getPassword()) || StringUtils.isEmpty(userReq.getConfPassword()))) {
|
Translator.toLocale(GepafinConstant.EMAIL_ALREADY_EXISTS));
|
||||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.INVALID_REQUEST));
|
}
|
||||||
}
|
if (Boolean.FALSE.equals(StringUtils.isEmpty(userReq.getCodiceFiscale()))
|
||||||
if (tempToken == null && !userReq.getPassword().equals(userReq.getConfPassword())) {
|
&& userRepository.existsByCodiceFiscale(userReq.getCodiceFiscale())) {
|
||||||
log.error("User creation failed: Passwords do not match for email {}", userReq.getEmail());
|
log.error("User creation failed: CodiceFiscale {} already exists", userReq.getCodiceFiscale());
|
||||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
|
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||||
}
|
Translator.toLocale(GepafinConstant.CODICE_FISCALE_EXISTS));
|
||||||
if (tempToken == null && userReq.getPassword().length() < 8) {
|
}
|
||||||
log.error("User creation failed: Password length is less than 8 characters for email {}", userReq.getEmail());
|
if (tempToken == null && userReq.getRoleId() == null) {
|
||||||
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
|
throw new ResourceNotFoundException(Status.VALIDATION_ERROR,
|
||||||
}
|
Translator.toLocale(GepafinConstant.ROLE_ID_MANDATORY));
|
||||||
|
}
|
||||||
|
if(tempToken != null) {
|
||||||
|
userReq.setRoleId(null);
|
||||||
|
}
|
||||||
|
validatePassword(userReq.getPassword(), userReq.getConfPassword(), tempToken);
|
||||||
|
|
||||||
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
|
UserEntity userEntity = convertUserRequestToUserEntity(userReq);
|
||||||
userEntity = userRepository.save(userEntity);
|
userEntity = userRepository.save(userEntity);
|
||||||
log.info("User created with ID: {}", userEntity.getId());
|
log.info("User created with ID: {}", userEntity.getId());
|
||||||
return authService.getJWTTokenBean(userEntity, Boolean.TRUE);
|
return authService.getJWTTokenBean(userEntity, Boolean.TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void validatePassword(String password, String confirmPassword, String tempToken) {
|
||||||
|
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(confirmPassword)) {
|
||||||
|
if(tempToken == null) {
|
||||||
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VALIDATE_PASSWORD));
|
||||||
|
}else if(Boolean.FALSE.equals(StringUtils.isEmpty(password) && StringUtils.isEmpty(confirmPassword))){
|
||||||
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.VALIDATE_PASSWORD));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password != null && !password.equals(confirmPassword)) {
|
||||||
|
log.error("User creation failed: Passwords do not match");
|
||||||
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_DOESNT_MATCH));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password != null && password.length() < 8) {
|
||||||
|
log.error("User creation failed: Password length is less than 8 characters");
|
||||||
|
throw new CustomValidationException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.PASSWORD_MIN_LEN));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
|
public UserResponseBean updateUser(Long userId, UpdateUserReq userReq) {
|
||||||
log.info("Updating user with ID: {}", userId);
|
log.info("Updating user with ID: {}", userId);
|
||||||
@@ -108,13 +136,21 @@ public class UserDao {
|
|||||||
userEntity.setOrganization(userReq.getOrganization());
|
userEntity.setOrganization(userReq.getOrganization());
|
||||||
userEntity.setAddress(userReq.getAddress());
|
userEntity.setAddress(userReq.getAddress());
|
||||||
userEntity.setPhoneNumber(userReq.getPhoneNumber());
|
userEntity.setPhoneNumber(userReq.getPhoneNumber());
|
||||||
userEntity.setRoleEntity(roleDao.validateRole(userReq.getRoleId()));
|
userEntity.setRoleEntity(getRoleEntity(userReq.getRoleId()));
|
||||||
userEntity.setCodiceFiscale(userReq.getCodiceFiscale());
|
userEntity.setCodiceFiscale(userReq.getCodiceFiscale());
|
||||||
userEntity.setDateOfBirth(userReq.getDateOfBirth());
|
userEntity.setDateOfBirth(userReq.getDateOfBirth());
|
||||||
return userEntity;
|
return userEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
|
private RoleEntity getRoleEntity(Long roleId) {
|
||||||
|
if(roleId != null) {
|
||||||
|
return roleDao.validateRole(roleId);
|
||||||
|
} else {
|
||||||
|
return roleDao.getRoleByType(RoleStatusEnum.ROLE_BENEFICIARY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private UserResponseBean convertUserEntityToUserResponse(UserEntity userEntity) {
|
||||||
UserResponseBean userResponseBean = new UserResponseBean();
|
UserResponseBean userResponseBean = new UserResponseBean();
|
||||||
userResponseBean.setId(userEntity.getId());
|
userResponseBean.setId(userEntity.getId());
|
||||||
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
|
userResponseBean.setCreatedDate(userEntity.getCreatedDate());
|
||||||
@@ -225,6 +261,16 @@ public class UserDao {
|
|||||||
authService.logout(request, response);
|
authService.logout(request, response);
|
||||||
log.info("User successfully logged out.");
|
log.info("User successfully logged out.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Boolean isValidEmail(String email) {
|
||||||
|
String EMAIL_REGEX = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$";
|
||||||
|
if (email == null || email.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
Pattern pattern = Pattern.compile(EMAIL_REGEX);
|
||||||
|
return pattern.matcher(email).matches();
|
||||||
|
}
|
||||||
|
|
||||||
public UserResponseBean updateUserStatus(Long userId, UserStatusEnum statusReq) {
|
public UserResponseBean updateUserStatus(Long userId, UserStatusEnum statusReq) {
|
||||||
log.info("Updating status for user with ID: {}", userId);
|
log.info("Updating status for user with ID: {}", userId);
|
||||||
|
|||||||
@@ -1,17 +1,11 @@
|
|||||||
package net.gepafin.tendermanagement.model.request;
|
package net.gepafin.tendermanagement.model.request;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
import jakarta.validation.constraints.Email;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class UserReq {
|
public class UserReq {
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
@Email
|
|
||||||
private String email;
|
private String email;
|
||||||
|
|
||||||
private String password;
|
private String password;
|
||||||
@@ -23,7 +17,7 @@ public class UserReq {
|
|||||||
private String lastName;
|
private String lastName;
|
||||||
|
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
@NotNull
|
|
||||||
private Long roleId;
|
private Long roleId;
|
||||||
|
|
||||||
private String organization;
|
private String organization;
|
||||||
|
|||||||
@@ -6,4 +6,6 @@ import org.springframework.stereotype.Repository;
|
|||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface RoleRepository extends JpaRepository<RoleEntity, Long> {
|
public interface RoleRepository extends JpaRepository<RoleEntity, Long> {
|
||||||
|
|
||||||
|
RoleEntity findByRoleType(String roleType);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ get_user_success_msg=User retrieved successfully.
|
|||||||
get_user_error_msg=An error occurred while retrieving the user.
|
get_user_error_msg=An error occurred while retrieving the user.
|
||||||
user.not.active=User is not active. Please contact support.
|
user.not.active=User is not active. Please contact support.
|
||||||
user.already.exist.msg=User already exist for this codice fiscale.
|
user.already.exist.msg=User already exist for this codice fiscale.
|
||||||
|
validate.email=The email is mandatory and must be in the correct format. Please verify and try again.
|
||||||
|
validate.password=The password and confPassword are mandatory. Please verify and try again.
|
||||||
# Role-related messages
|
# Role-related messages
|
||||||
role.created.success=Role created successfully.
|
role.created.success=Role created successfully.
|
||||||
role.updated.success=Role updated successfully.
|
role.updated.success=Role updated successfully.
|
||||||
@@ -18,6 +20,7 @@ create.role.error=Error occurred while creating the role.
|
|||||||
update.role.error=Error occurred while updating the role.
|
update.role.error=Error occurred while updating the role.
|
||||||
role.fetch.success=Role fetched successfully.
|
role.fetch.success=Role fetched successfully.
|
||||||
delete.role.error=Error occurred while deleting the role.
|
delete.role.error=Error occurred while deleting the role.
|
||||||
|
role.id.mandatory=Role id is mandatory.
|
||||||
|
|
||||||
# Region-related messages
|
# Region-related messages
|
||||||
region.created.success=Region created successfully.
|
region.created.success=Region created successfully.
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ get_user_success_msg=Utente recuperato con successo.
|
|||||||
get_user_error_msg=Si � verificato un errore durante il recupero dell'utente.
|
get_user_error_msg=Si � verificato un errore durante il recupero dell'utente.
|
||||||
user.not.active=Utente non attivo. Si prega di contattare il supporto.
|
user.not.active=Utente non attivo. Si prega di contattare il supporto.
|
||||||
user.already.exist.msg=L'utente esiste gi� per questo codice fiscale.
|
user.already.exist.msg=L'utente esiste gi� per questo codice fiscale.
|
||||||
|
validate.email=L'email è obbligatoria e deve essere nel formato corretto. Si prega di verificare e riprovare.
|
||||||
|
validate.password=La password e confPassword sono obbligatorie. Verifica e riprova.
|
||||||
# Role-related messages
|
# Role-related messages
|
||||||
role.created.success=Ruolo creato con successo.
|
role.created.success=Ruolo creato con successo.
|
||||||
role.updated.success=Ruolo aggiornato con successo.
|
role.updated.success=Ruolo aggiornato con successo.
|
||||||
@@ -18,6 +20,7 @@ create.role.error=Errore durante la creazione del ruolo.
|
|||||||
update.role.error=Errore durante l'aggiornamento del ruolo.
|
update.role.error=Errore durante l'aggiornamento del ruolo.
|
||||||
role.fetch.success=Ruolo recuperato con successo.
|
role.fetch.success=Ruolo recuperato con successo.
|
||||||
delete.role.error=Errore durante l'eliminazione del ruolo.
|
delete.role.error=Errore durante l'eliminazione del ruolo.
|
||||||
|
role.id.mandatory=L'ID del ruolo è obbligatorio.
|
||||||
|
|
||||||
# Region-related messages
|
# Region-related messages
|
||||||
region.created.success=Regione creata con successo.
|
region.created.success=Regione creata con successo.
|
||||||
|
|||||||
Reference in New Issue
Block a user