Search in sources :

Example 1 with AuthenticationType

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());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) UserManagementDescriptorKey(com.synopsys.integration.alert.component.users.UserManagementDescriptorKey) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 2 with AuthenticationType

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 3 with AuthenticationType

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);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Test(org.junit.jupiter.api.Test)

Example 4 with AuthenticationType

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");
    }
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Test(org.junit.jupiter.api.Test)

Example 5 with AuthenticationType

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);
}
Also used : AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)6 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)4 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)4 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)4 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)4 Test (org.junit.jupiter.api.Test)4 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)2 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)2 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)1 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)1 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)1 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)1 UserManagementDescriptorKey (com.synopsys.integration.alert.component.users.UserManagementDescriptorKey)1 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)1 Transactional (org.springframework.transaction.annotation.Transactional)1