use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method getUsersTest.
@Test
public void getUsersTest() {
final Long authenticationTypeId = 1L;
final String roleName = "userName";
UserEntity userEntity = new UserEntity(username, password, emailAddress, authenticationTypeId);
userEntity.setId(1L);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findAll()).thenReturn(List.of(userEntity));
createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
List<UserModel> userModelList = defaultUserAccessor.getUsers();
assertEquals(1, userModelList.size());
UserModel userModel = userModelList.get(0);
testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method deleteUser.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public void deleteUser(Long userId) throws AlertForbiddenOperationException {
Optional<UserEntity> optionalUser = userRepository.findById(userId);
if (optionalUser.isPresent()) {
UserEntity user = optionalUser.get();
deleteUserEntity(user);
}
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method changeUserEmailAddress.
private void changeUserEmailAddress(UserEntity oldEntity, String emailAddress) {
UserEntity updatedEntity = new UserEntity(oldEntity.getUserName(), oldEntity.getPassword(), emailAddress, oldEntity.getAuthenticationType());
updatedEntity.setId(oldEntity.getId());
userRepository.save(updatedEntity);
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method updateUser.
@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel updateUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException, AlertForbiddenOperationException {
Long userId = user.getId();
UserEntity existingUser = userRepository.findById(userId).orElseThrow(() -> new AlertConfigurationException(String.format("No user found with id '%s'", userId)));
Long existingUserId = existingUser.getId();
UserEntity savedEntity = existingUser;
// if it isn't an external user then update username, password, and email.
Optional<AuthenticationType> authenticationType = authenticationTypeAccessor.getAuthenticationType(existingUser.getAuthenticationType());
if (authenticationType.isEmpty()) {
throw new AlertRuntimeException("Unknown Authentication Type, user not updated.");
} else if (AuthenticationType.DATABASE != authenticationType.get()) {
boolean isUserNameInvalid = !StringUtils.equals(existingUser.getUserName(), user.getName());
boolean isEmailInvalid = !StringUtils.equals(existingUser.getEmailAddress(), user.getEmailAddress());
boolean isPasswordSet = StringUtils.isNotBlank(user.getPassword());
if (isUserNameInvalid || isEmailInvalid || isPasswordSet) {
throw new AlertForbiddenOperationException("An external user cannot change its credentials.");
}
} else {
String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword());
UserEntity newEntity = new UserEntity(user.getName(), password, user.getEmailAddress(), user.isExpired(), user.isLocked(), user.isPasswordExpired(), user.isEnabled(), existingUser.getAuthenticationType());
newEntity.setId(existingUserId);
savedEntity = userRepository.save(newEntity);
}
roleAccessor.updateUserRoles(existingUserId, user.getRoles());
return createModel(savedEntity);
}
use of com.synopsys.integration.alert.database.user.UserEntity in project hub-alert by blackducksoftware.
the class DefaultUserAccessor method changeUserPassword.
private void changeUserPassword(UserEntity oldEntity, String newPassword) {
UserEntity updatedEntity = new UserEntity(oldEntity.getUserName(), defaultPasswordEncoder.encode(newPassword), oldEntity.getEmailAddress(), oldEntity.getAuthenticationType());
updatedEntity.setId(oldEntity.getId());
userRepository.save(updatedEntity);
}
Aggregations