Open Kilda Java Documentation
UserValidator.java
Go to the documentation of this file.
1 package org.usermanagement.validator;
2 
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
5 
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Component;
8 
9 import java.util.regex.Matcher;
10 import java.util.regex.Pattern;
11 
19 
20 @Component
21 public class UserValidator {
22 
23  private static final Logger LOGGER = LoggerFactory.getLogger(UserValidator.class);
24 
25  @Autowired
26  private MessageUtils messageUtil;
27 
28  @Autowired
29  private UserRepository userRepository;
30 
31  public void validateCreateUser(final UserInfo userInfo) {
32  if (ValidatorUtil.isNull(userInfo.getName())) {
33  LOGGER.error("Validation fail for user(username: " + userInfo.getUsername()
34  + "). Error: " + messageUtil.getAttributeNotNull("name"));
35  throw new RequestValidationException(messageUtil.getAttributeNotNull("name"));
36  } else if (ValidatorUtil.isNull(userInfo.getUsername())) {
37  LOGGER.error("Validation fail for user(name: " + userInfo.getName() + "). Error: "
38  + messageUtil.getAttributeNotNull("username"));
39  throw new RequestValidationException(messageUtil.getAttributeNotNull("username"));
40  } else if (ValidatorUtil.isNull(userInfo.getEmail())) {
41  LOGGER.error("Validation fail for user(username: " + userInfo.getUsername()
42  + "). Error: " + messageUtil.getAttributeNotNull("email"));
43  throw new RequestValidationException(messageUtil.getAttributeNotNull("email"));
44  } else if (ValidatorUtil.isNull(userInfo.getRoleIds())) {
45  LOGGER.error("Validation fail for user(username: " + userInfo.getUsername()
46  + "). Error: " + messageUtil.getAttributeNotNull("role"));
47  throw new RequestValidationException(messageUtil.getAttributeNotNull("role"));
48  }
49 
50  UserEntity userEntityTemp = userRepository.findByUsername(userInfo.getUsername());
51  if (userEntityTemp != null) {
52  LOGGER.error("Validation fail for user(username: " + userInfo.getUsername()
53  + "). Error: " + messageUtil.getAttributeUnique("username"));
54  throw new RequestValidationException(messageUtil.getAttributeUnique("username"));
55  }
56  }
57 
58  public UserEntity validateUpdateUser(final UserInfo userInfo) {
59  UserEntity userEntity = validateUserId(userInfo.getUserId());
60 
61  if (ValidatorUtil.isNull(userEntity)) {
62  LOGGER.error("Validation fail for update user request(id: " + userInfo.getUserId()
63  + "). Error: " + messageUtil.getAttributeNotNull("user"));
64  throw new RequestValidationException(messageUtil.getAttributeNotNull("user"));
65  }
66 
67  if (ValidatorUtil.isNull(userInfo.getName()) && ValidatorUtil.isNull(userInfo.getRoleIds())
68  && ValidatorUtil.isNull(userInfo.getStatus())) {
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"));
73  }
74 
75  if (!ValidatorUtil.isNull(userInfo.getStatus())
76  && Status.getStatusByName(userInfo.getStatus()) == null) {
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()));
81  }
82 
83 
84  UserEntity userEntityTemp = userRepository.findByUsername(userInfo.getUsername());
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"));
88  throw new RequestValidationException(messageUtil.getAttributeUnique("username"));
89  }
90  return userEntity;
91  }
92 
93  public UserEntity validateUserId(final long userId) {
94  UserEntity userEntity = userRepository.findByUserId(userId);
95 
96  if (ValidatorUtil.isNull(userEntity) || userId == 1) {
97  LOGGER.error("Validation failed for user (id: " + userId
98  + "). Error: " + messageUtil.getAttributeInvalid("user_id", userId + ""));
100  messageUtil.getAttributeInvalid("user_id", userId + ""));
101  }
102  return userEntity;
103  }
104 
105  public void validateChangePassword(final UserInfo userInfo) {
106  String REGEX_ONE = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\\w\\s]).{8,15}$";
107  String REGEX_TWO = "[%,&,+,\\,\\s,\"]";
108 
109  if (ValidatorUtil.isNull(userInfo.getNewPassword())) {
110  LOGGER.error("Validation fail for change user password request (id: " + userInfo.getUserId()
111  + "). Error: " + messageUtil.getAttributeNotNull("password"));
112  throw new RequestValidationException(messageUtil.getAttributeNotNull("password"));
113  }
114 
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());
119  throw new RequestValidationException(messageUtil.getAttributePasswordMustContain());
120  }
121 
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());
126  throw new RequestValidationException(messageUtil.getAttributePasswordMustNotContain());
127  }
128 
129  if(userInfo.getNewPassword().equals(userInfo.getPassword())){
130  LOGGER.error("New password not valid (id: " + userInfo.getUserId() + "). Error: "
131  + messageUtil.getAttributePasswordShouldNotSame());
132  throw new RequestValidationException(messageUtil.getAttributePasswordShouldNotSame());
133  }
134 
135  if(userInfo.getNewPassword().equals(userInfo.getPassword())){
136  LOGGER.error("New password not valid (id: " + userInfo.getUserId() + "). Error: "
137  + messageUtil.getAttributePasswordLength("8","15"));
138  throw new RequestValidationException(messageUtil.getAttributePasswordLength("8","15"));
139  }
140  }
141 }
void validateCreateUser(final UserInfo userInfo)
UserEntity validateUserId(final long userId)
void validateChangePassword(final UserInfo userInfo)
static boolean isNull(final Object obj)
static Status getStatusByName(final String name)
Definition: Status.java:80
UserEntity validateUpdateUser(final UserInfo userInfo)