use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.
the class UserService method saveOrUpdateSettings.
/**
* Save or update settings.
*
* @param userInfo the user info
* @return the user info
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo saveOrUpdateSettings(UserInfo userInfo) {
if (ValidatorUtil.isNull(userInfo.getUserId())) {
LOGGER.warn("Validation failed for user (id: " + userInfo.getUserId() + "). Error: " + messageUtil.getAttributeInvalid("user_id", userInfo.getUserId() + ""));
throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userInfo.getUserId() + ""));
}
UserSettingEntity userSettingEntity = userSettingRepository.findOneByUserId(userInfo.getUserId());
if (userSettingEntity == null) {
userSettingEntity = new UserSettingEntity();
userSettingEntity.setUserId(userInfo.getUserId());
}
userSettingEntity.setSettings(IConstants.Settings.TOPOLOGY_SETTING);
userSettingEntity.setData(userInfo.getData());
userSettingEntity = userSettingRepository.save(userSettingEntity);
// activityLogger.log(ActivityType.UPDATE_USER_SETTINGS,
// userInfo.getUserId() + "");
LOGGER.info("User Settings saved successfully for user(user_id: " + userInfo.getUserId() + ").");
return userInfo;
}
use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.
the class UserService method reset2fa.
/**
* Reset 2 fa.
*
* @param userId the user id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void reset2fa(final long userId) {
UserEntity userEntity = userRepository.findByUserId(userId);
if (ValidatorUtil.isNull(userEntity)) {
LOGGER.warn("User Entity not found for user(user_id: " + userId + ")");
throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
}
userEntity.setIs2FaConfigured(false);
userEntity.setTwoFaKey(null);
userEntity = userRepository.save(userEntity);
activityLogger.log(ActivityType.RESET_2FA, userEntity.getUsername());
LOGGER.info("2FA reset successfully for user(user_id: " + userId + ").");
try {
if (!userEntity.getIs2FaConfigured()) {
Map<String, Object> context = new HashMap<>();
context.put("name", userEntity.getName());
mailService.send(userEntity.getEmail(), mailUtils.getSubjectReset2fa(), TemplateService.Template.RESET_2FA, context);
LOGGER.info("Reset 2FA mail sent successfully for user(user_id: " + userId + ").");
}
} catch (Exception e) {
LOGGER.warn("Reset 2FA mail failed for user: " + userId);
}
}
use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.
the class UserService method changePassword.
/**
* Change password.
*
* @param userInfo the user info
* @param userId the user id
* @return the user info
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public UserInfo changePassword(final UserInfo userInfo, final Long userId) {
userValidator.validateChangePassword(userInfo);
UserEntity userEntity = userRepository.findByUserId(userId);
if (ValidatorUtil.isNull(userEntity)) {
LOGGER.warn("User Entity not found for user(id: " + userId + ")");
throw new RequestValidationException(messageUtil.getAttributeInvalid("user_id", userId + ""));
}
if (!StringUtil.matches(userInfo.getPassword(), userEntity.getPassword())) {
LOGGER.warn("Password not matched for user (id: " + userId + "). Error: " + messageUtil.getAttributePasswordInvalid());
throw new RequestValidationException(messageUtil.getAttributePasswordInvalid());
}
if (userEntity.getIs2FaEnabled()) {
if (!userEntity.getIs2FaConfigured()) {
LOGGER.warn("2FA key is not configured for user(id: " + userId + "). Error: " + messageUtil.getAttribute2faNotConfiured());
throw new TwoFaKeyNotSetException(messageUtil.getAttribute2faNotConfiured());
} else {
if (userInfo.getCode() == null || userInfo.getCode().isEmpty()) {
LOGGER.warn("OTP code is madatory as 2FA is configured for user (id: " + userId + "). Error: " + messageUtil.getAttributeNotNull("OTP"));
throw new OtpRequiredException(messageUtil.getAttributeNotNull("OTP"));
} else if (!TwoFactorUtility.validateOtp(userInfo.getCode(), userEntity.getTwoFaKey())) {
LOGGER.warn("Invalid OTP for user (id: " + userId + "). Error: " + messageUtil.getAttributeNotvalid("OTP"));
throw new RequestValidationException(messageUtil.getAttributeNotvalid("OTP"));
}
}
}
userEntity.setPassword(StringUtil.encodeString(userInfo.getNewPassword()));
userEntity.setUpdatedDate(new Date());
userEntity = userRepository.save(userEntity);
activityLogger.log(ActivityType.CHANGE_PASSWORD, userEntity.getUsername());
LOGGER.info("User(userId: " + userId + ") password changed successfully.");
try {
Map<String, Object> context = new HashMap<>();
context.put("name", userEntity.getName());
mailService.send(userEntity.getEmail(), mailUtils.getSubjectChangePassword(), TemplateService.Template.CHANGE_PASSWORD, context);
LOGGER.info("Changed password mail sent successfully for user(userId: " + userId + ").");
} catch (Exception e) {
LOGGER.warn("Change password email failed for username: " + userEntity.getUsername());
}
return UserConversionUtil.toUserInfo(userEntity);
}
use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.
the class PermissionService method getPermissionById.
/**
* Gets the permission by id.
*
* @param permissionId the permission id
* @return the permission by id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public Permission getPermissionById(final Long permissionId) {
PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
if (ValidatorUtil.isNull(permissionEntity)) {
LOGGER.warn("Permission with permissionId '" + permissionId + "' not found. Error: " + messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
}
Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
return PermissionConversionUtil.toPermission(permissionEntity, roleEntityList);
}
use of org.usermanagement.exception.RequestValidationException in project open-kilda by telstra.
the class PermissionService method deletePermissionById.
/**
* Delete permission by id.
*
* @param permissionId the permission id
*/
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void deletePermissionById(final Long permissionId) {
PermissionEntity permissionEntity = permissionRepository.findByPermissionId(permissionId);
if (ValidatorUtil.isNull(permissionEntity)) {
throw new RequestValidationException(messageUtil.getAttributeInvalid("permission_id", permissionId + ""));
}
Set<RoleEntity> roleEntityList = roleRepository.findByPermissions_permissionId(permissionId);
if (roleEntityList.size() > 0) {
String roles = "";
for (RoleEntity roleEntity : roleEntityList) {
roles += !"".equals(roles) ? "," + roleEntity.getName() : roleEntity.getName();
}
LOGGER.warn("Permission with permissionId '" + permissionId + "' not allowed to delete. Error: " + messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
throw new RequestValidationException(messageUtil.getAttributeDeletionNotAllowed(permissionEntity.getName(), roles));
}
permissionRepository.delete(permissionEntity);
LOGGER.info("Permission(permissionId: " + permissionId + ") deleted successfully.");
activityLogger.log(ActivityType.DELETE_PERMISSION, permissionEntity.getName());
}
Aggregations