use of com.synopsys.integration.alert.common.enumeration.AuthenticationType in project hub-alert by blackducksoftware.
the class UserActionsTest method testExternalUserNoEmailValidation.
@Test
public void testExternalUserNoEmailValidation() throws Exception {
AuthenticationType authenticationTypeLDAP = AuthenticationType.LDAP;
UserModel userModel = UserModel.existingUser(id, name, password, null, authenticationTypeLDAP, roles, true);
Mockito.when(authorizationManager.hasExecutePermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
Mockito.when(userAccessor.getUser(Mockito.anyLong())).thenReturn(Optional.of(userModel));
Set<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet());
roleNames.add(DefaultUserRole.ALERT_ADMIN.name());
UserConfig userConfig = new UserConfig(id.toString(), name, "newPassword", null, roleNames, false, false, false, true, false, authenticationTypeLDAP.name(), true);
UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
ValidationActionResponse validationActionResponse = userActions.validate(userConfig);
assertFalse(validationActionResponse.isError());
assertTrue(validationActionResponse.hasContent());
ValidationResponseModel validationResponseModel = validationActionResponse.getContent().get();
assertFalse(validationResponseModel.hasErrors());
assertEquals("The user is valid", validationActionResponse.getMessage().get());
}
use of com.synopsys.integration.alert.common.enumeration.AuthenticationType in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserTest.
@Test
public void updateUserTest() throws Exception {
final String roleName = "userName";
AuthenticationType authenticationType = AuthenticationType.DATABASE;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, password, emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel newUserModel = defaultUserAccessor.updateUser(userModel, false);
Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
testUserModel(userEntity.getId(), username, emailAddress, roleName, newUserModel);
}
use of com.synopsys.integration.alert.common.enumeration.AuthenticationType in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserNonDatabaseAuthTest.
@Test
public void updateUserNonDatabaseAuthTest() throws Exception {
final String roleName = "roleName";
AuthenticationType authenticationType = AuthenticationType.LDAP;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, "", emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(userEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
UserModel updatedUserModel = defaultUserAccessor.updateUser(userModel, false);
Mockito.verify(roleAccessor).updateUserRoles(Mockito.eq(userEntity.getId()), Mockito.any());
testUserModel(userEntity.getId(), username, emailAddress, roleName, updatedUserModel);
}
use of com.synopsys.integration.alert.common.enumeration.AuthenticationType in project hub-alert by blackducksoftware.
the class DefaultUserAccessorTest method updateUserNonDatabaseAuthInvalidTest.
@Test
public void updateUserNonDatabaseAuthInvalidTest() throws Exception {
final String roleName = "roleName";
AuthenticationType authenticationType = AuthenticationType.LDAP;
UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
userEntity.setId(1L);
UserEntity existingUserEntity = new UserEntity("usernam-teste", "existing-password", "old-email.noreply@blackducksoftware.com", 2L);
existingUserEntity.setId(1L);
UserRoleModel roles = createUserRoleModel(1L, roleName, true);
UserModel userModel = UserModel.existingUser(1L, username, password, emailAddress, authenticationType, Set.of(roles), true);
UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
Mockito.when(userRepository.findById(Mockito.any())).thenReturn(Optional.of(existingUserEntity));
Mockito.when(authenticationTypeAccessor.getAuthenticationType(Mockito.any())).thenReturn(Optional.of(authenticationType));
Mockito.when(userRepository.save(Mockito.any())).thenReturn(existingUserEntity);
createModelMocks(userRoleRelation, userRoleModel, authenticationType);
DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
try {
defaultUserAccessor.updateUser(userModel, false);
fail("External user with ? did not throw expected " + AlertForbiddenOperationException.class.getSimpleName());
} catch (AlertForbiddenOperationException e) {
assertNotNull(e);
} catch (AlertConfigurationException wrongException) {
fail("Wrong exception thrown");
}
}
use of com.synopsys.integration.alert.common.enumeration.AuthenticationType 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);
}
Aggregations