1 package org.usermanagement.validator;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Component;
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
23 private static final Logger LOGGER = LoggerFactory.getLogger(
UserValidator.class);
33 LOGGER.error(
"Validation fail for user(username: " + userInfo.
getUsername()
34 +
"). Error: " + messageUtil.getAttributeNotNull(
"name"));
37 LOGGER.error(
"Validation fail for user(name: " + userInfo.
getName() +
"). Error: " 38 + messageUtil.getAttributeNotNull(
"username"));
41 LOGGER.error(
"Validation fail for user(username: " + userInfo.
getUsername()
42 +
"). Error: " + messageUtil.getAttributeNotNull(
"email"));
45 LOGGER.error(
"Validation fail for user(username: " + userInfo.
getUsername()
46 +
"). Error: " + messageUtil.getAttributeNotNull(
"role"));
51 if (userEntityTemp != null) {
52 LOGGER.error(
"Validation fail for user(username: " + userInfo.
getUsername()
53 +
"). Error: " + messageUtil.getAttributeUnique(
"username"));
62 LOGGER.error(
"Validation fail for update user request(id: " + userInfo.
getUserId()
63 +
"). Error: " + messageUtil.getAttributeNotNull(
"user"));
69 LOGGER.error(
"Validation fail for update user request(id: " + userInfo.
getUserId()
70 +
"). Error: " + messageUtil.getAttributeNotNull(
"name, status and role_id"));
72 messageUtil.getAttributeNotNull(
"name, status and role_id"));
77 LOGGER.error(
"Validation fail for update user request(id: " + userInfo.
getUserId()
78 +
"). Error: " + messageUtil.getAttributeInvalid(
"status", userInfo.
getStatus()));
80 messageUtil.getAttributeInvalid(
"status", userInfo.
getStatus()));
85 if (userEntityTemp != null && !userEntityTemp.
getUserId().equals(userInfo.
getUserId())) {
86 LOGGER.error(
"Validation fail for update user request(id: " + userInfo.
getUserId()
87 +
"). Error: " + messageUtil.getAttributeUnique(
"username"));
94 UserEntity userEntity = userRepository.findByUserId(userId);
97 LOGGER.error(
"Validation failed for user (id: " + userId
98 +
"). Error: " + messageUtil.getAttributeInvalid(
"user_id", userId +
""));
100 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
106 String REGEX_ONE =
"^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\\w\\s]).{8,15}$";
107 String REGEX_TWO =
"[%,&,+,\\,\\s,\"]";
110 LOGGER.error(
"Validation fail for change user password request (id: " + userInfo.
getUserId()
111 +
"). Error: " + messageUtil.getAttributeNotNull(
"password"));
115 Matcher matcher_one = Pattern.compile(REGEX_ONE).matcher(userInfo.
getNewPassword());
116 if(!matcher_one.matches()){
117 LOGGER.error(
"Validation fail for change user password request (id: " + userInfo.
getUserId()
118 +
"). Error: " + messageUtil.getAttributePasswordMustContain());
122 Matcher matcher_two = Pattern.compile(REGEX_TWO).matcher(userInfo.
getNewPassword());
123 if(matcher_two.find()){
124 LOGGER.error(
"Validation fail for change user password request (id: " + userInfo.
getUserId()
125 +
"). Error: " + messageUtil.getAttributePasswordMustNotContain());
130 LOGGER.error(
"New password not valid (id: " + userInfo.
getUserId() +
"). Error: " 131 + messageUtil.getAttributePasswordShouldNotSame());
136 LOGGER.error(
"New password not valid (id: " + userInfo.
getUserId() +
"). Error: " 137 + messageUtil.getAttributePasswordLength(
"8",
"15"));
void validateCreateUser(final UserInfo userInfo)
UserEntity validateUserId(final long userId)
void validateChangePassword(final UserInfo userInfo)
List< Long > getRoleIds()
static boolean isNull(final Object obj)
static Status getStatusByName(final String name)
UserEntity validateUpdateUser(final UserInfo userInfo)