1 package org.usermanagement.service;
3 import java.util.Calendar;
5 import java.util.HashMap;
6 import java.util.HashSet;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.security.core.GrantedAuthority;
23 import org.springframework.security.core.userdetails.UserDetails;
24 import org.springframework.security.core.userdetails.UserDetailsService;
25 import org.springframework.security.core.userdetails.UsernameNotFoundException;
26 import org.springframework.stereotype.Service;
27 import org.springframework.transaction.annotation.Propagation;
28 import org.springframework.transaction.annotation.Transactional;
55 private static final Logger LOGGER = LoggerFactory.getLogger(
UserService.class);
95 UserEntity user = userRepository.findByUsername(username);
97 Set<GrantedAuthority> authorities =
new HashSet<GrantedAuthority>(0);
99 LOGGER.error(
"User with username '" + username +
"' not found.");
100 throw new UsernameNotFoundException(username);
103 return new org.springframework.security.core.userdetails.User(username, user.
getPassword(),
113 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
115 userValidator.validateCreateUser(userRequest);
117 Set<RoleEntity> roleEntities = roleService.getRolesById(userRequest.getRoleIds());
123 userEntity = userRepository.save(userEntity);
124 LOGGER.info(
"User with username '" + userEntity.
getUsername() +
"' created successfully.");
129 Map<String, Object> map =
new HashMap<String, Object>();
130 map.put(
"name", userEntity.
getName());
132 map.put(
"password", password);
133 mailService.send(userEntity.
getEmail(), mailUtils.getSubjectAccountUsername(),
135 mailService.send(userEntity.
getEmail(), mailUtils.getSubjectAccountPassword(),
137 LOGGER.info(
"Username and password email sent successfully to user(username: " 151 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
153 UserEntity userEntity = userValidator.validateUpdateUser(userInfo);
155 if (userInfo.getRoleIds() != null) {
157 Set<RoleEntity> roleEntities = roleService.getRolesById(userInfo.getRoleIds());
158 userEntity.
getRoles().addAll(roleEntities);
162 userEntity = userRepository.save(userEntity);
165 LOGGER.info(
"User updated successfully (id: " + userId +
")");
176 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
178 List<UserEntity> userEntities = userRepository.findAll();
189 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
191 UserEntity userEntity = userValidator.validateUserId(userId);
202 @Transactional(propagation = Propagation.REQUIRED, readOnly =
true)
204 return userRepository.findByUsername(userName);
213 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
215 UserEntity userEntity = userRepository.findByUsername(userName);
217 userRepository.save(userEntity);
218 LOGGER.info(
"User 2FA updated successfully (username: " + userName +
")");
226 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
228 UserEntity userEntity = userRepository.findByUsername(userName);
230 LOGGER.error(
"User with username '" + userName +
"' not found. Error: " 231 + messageUtil.getAttributeInvalid(
"username", userName +
""));
233 messageUtil.getAttributeInvalid(
"username", userName +
""));
235 userEntity.
setLoginTime(Calendar.getInstance().getTime());
237 userRepository.save(userEntity);
238 LOGGER.info(
"User last login updated successfully (username: " + userName +
")");
246 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
248 UserEntity userEntity = userRepository.findByUserId(userId);
250 LOGGER.error(
"User with user id '" + userId +
"' not found. Error: " 251 + messageUtil.getAttributeInvalid(
"user_id", userId +
""));
253 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
256 userRepository.delete(userEntity);
259 LOGGER.info(
"User deleted successfully (userId: " + userId +
")");
269 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
272 RoleEntity roleEntity = roleRepository.findByRoleId(roleId);
274 LOGGER.error(
"Role with role id '" + roleId +
"' not found. Error: " 275 + messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
277 messageUtil.getAttributeInvalid(
"role_id", roleId +
""));
280 if (role.getUserInfo() != null) {
281 for (
UserInfo user : role.getUserInfo()) {
282 UserEntity userEntity = userRepository.findByUserId(user.getUserId());
283 roleEntity.
getUsers().add(userEntity);
286 roleEntity = roleRepository.save(roleEntity);
289 LOGGER.info(
"Users assigned with role successfully (role id: " + roleId +
")");
300 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
302 userValidator.validateChangePassword(userInfo);
304 UserEntity userEntity = userRepository.findByUserId(userId);
306 LOGGER.error(
"User Entity not found for user(id: " + userId +
")");
308 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
312 LOGGER.error(
"Password not matched for user (id: " + userId +
"). Error: " 313 + messageUtil.getAttributePasswordInvalid());
319 LOGGER.error(
"2FA key is not configured for user(id: " + userId +
"). Error: " 320 + messageUtil.getAttribute2faNotConfiured());
323 if (userInfo.getCode() == null || userInfo.getCode().isEmpty()) {
324 LOGGER.error(
"OTP code is madatory as 2FA is configured for user (id: " + userId +
"). Error: " 325 + messageUtil.getAttributeNotNull(
"OTP"));
329 LOGGER.error(
"Invalid OTP for user (id: " + userId +
"). Error: " 330 + messageUtil.getAttributeNotvalid(
"OTP"));
332 messageUtil.getAttributeNotvalid(
"OTP"));
339 userEntity = userRepository.save(userEntity);
342 LOGGER.info(
"User(userId: " + userId +
") password changed successfully.");
344 Map<String, Object> context =
new HashMap<>();
345 context.put(
"name", userEntity.
getName());
346 mailService.send(userEntity.
getEmail(), mailUtils.getSubjectChangePassword(),
348 LOGGER.info(
"Changed password mail sent successfully for user(userId: " + userId +
").");
360 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
365 UserEntity userEntity = userRepository.findByUserId(userId);
368 LOGGER.error(
"User Entity not found for user(id: " + userId +
")");
370 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
378 userEntity = userRepository.save(userEntity);
385 LOGGER.info(
"Password reset successfully for user(userId: " + userId +
").");
387 Map<String, Object> context =
new HashMap<>();
388 context.put(
"name", userEntity.
getName());
389 context.put(
"password", randomPassword);
390 mailService.send(userEntity.
getEmail(), mailUtils.getSubjectResetPassword(),
392 LOGGER.info(
"Reset password mail sent successfully for user(userId: " + userId +
").");
403 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
405 UserEntity userEntity = userRepository.findByUserId(userId);
408 LOGGER.error(
"User Entity not found for user(user_id: " + userId +
")");
410 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
414 userEntity = userRepository.save(userEntity);
417 LOGGER.info(
"2FA reset successfully for user(user_id: " + userId +
").");
419 Map<String, Object> context =
new HashMap<>();
420 context.put(
"name", userEntity.
getName());
422 mailService.send(userEntity.
getEmail(), mailUtils.getSubjectReset2fa(),
424 LOGGER.info(
"Reset 2FA mail sent successfully for user(user_id: " + userId +
").");
428 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
432 LOGGER.error(
"Validation failed for user (id: " + userInfo.getUserId() +
"). Error: " 433 + messageUtil.getAttributeInvalid(
"user_id", userInfo.getUserId() +
""));
437 UserSettingEntity userSettingEntity = userSettingRepository.findOneByUserId(userInfo.getUserId());
438 if (userSettingEntity == null) {
440 userSettingEntity.
setUserId(userInfo.getUserId());
443 userSettingEntity.
setData(userInfo.getData());
444 userSettingEntity = userSettingRepository.save(userSettingEntity);
447 LOGGER.info(
"User Settings saved successfully for user(user_id: " + userInfo.getUserId() +
").");
451 @Transactional(propagation = Propagation.REQUIRED, readOnly =
false)
455 LOGGER.error(
"Validation failed for user (id: " + userId +
"). Error: " 456 + messageUtil.getAttributeInvalid(
"user_id", userId +
""));
460 UserSettingEntity userSettingEntity = userSettingRepository.findOneByUserId(userId);
461 if (userSettingEntity == null) {
462 LOGGER.error(
"User settings not found for user(user_id: " + userId +
")" + messageCodeUtil.getAttributeNotFoundCode());
464 messageUtil.getAttributeNotFound(
"User settings"));
466 return userSettingEntity.
getData();
470 UserEntity userEntity = userRepository.findByUserId(userId);
472 LOGGER.error(
"User Entity not found for user(id: " + userId +
")");
474 messageUtil.getAttributeInvalid(
"user_id", userId +
""));
479 LOGGER.error(
"2FA key is not configured for user(id: " + userId
480 +
"). Error: " + messageUtil.getAttribute2faNotConfiured());
483 if (otp == null || otp.isEmpty()) {
484 LOGGER.error(
"OTP code is madatory as 2FA is configured for user (id: " 485 + userId +
"). Error: " 486 + messageUtil.getAttributeNotNull(
"OTP"));
490 LOGGER.error(
"Invalid OTP for user (id: " + userId +
"). Error: " 491 + messageUtil.getAttributeNotvalid(
"OTP"));
496 LOGGER.error(
"2FA is not enabled for user(id: " + userId +
"). Error: " 497 + messageUtil.getAttribute2faNotEnabled());
void setIs2FaConfigured(final boolean is2FaConfigured)
boolean getIs2FaConfigured()
UserInfo changePassword(final UserInfo userInfo, final Long userId)
UserInfo updateUser(final UserInfo userInfo, final Long userId)
void setTwoFaKey(final String twoFaKey)
void setUpdatedDate(Date updatedDate)
static boolean validateOtp(final String otp, final String decryptKey)
String getUserSettings(final long userId)
boolean getIs2FaEnabled()
void reset2fa(final long userId)
void deleteUserById(final Long userId)
static boolean matches(String rawPassword, String encodedPassword)
static UserEntity toUserEntity(final UserInfo userInfo, final Set< RoleEntity > roleEntities)
void setIs2FaEnabled(final boolean is2FaEnabled)
List< UserInfo > getAllUsers()
UserDetails loadUserByUsername(final String username)
static String randomAlphaNumeric(final int count)
static boolean isNull(final Object obj)
static final String TOPOLOGY_SETTING
void setSettings(String settings)
void setPassword(final String password)
static String encodeString(String data)
Set< UserEntity > getUsers()
static List< UserInfo > toAllUsers(final List< UserEntity > userEntityList)
UserInfo saveOrUpdateSettings(UserInfo userInfo)
static UserEntity toResetPwdUserEntity(final UserEntity userEntity, final String randomPassword)
void setUserId(Long userId)
Set< RoleEntity > getRoles()
void setLoginTime(final Date loginTime)
void setData(String data)
UserEntity getUserByUsername(final String userName)
void updateUser2FAKey(final String userName, final String secretKey)
Role assignUserByRoleId(final Long roleId, final Role role)
static void toUpateUserEntity(final UserInfo userInfo, final UserEntity userEntity)
UserInfo getUserById(final Long userId)
void setPassword(final String password)
boolean validateOTP(final long userId, final String otp)
static Role toRole(final RoleEntity roleEntity, final boolean withPermissions, final boolean withUsers)
static UserInfo toUserInfo(final UserEntity userEntity)
UserInfo resetPassword(final long userId, final boolean adminFlag)
UserInfo createUser(final UserInfo userRequest)
void updateLoginDetail(final String userName)
void setUserId(final Long userId)