Search in sources :

Example 1 with AuthenticationTypeDetails

use of com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails in project hub-alert by blackducksoftware.

the class DefaultUserAccessor method addUser.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserModel addUser(UserModel user, boolean passwordEncoded) throws AlertConfigurationException {
    String username = user.getName();
    Optional<UserEntity> userWithSameUsername = userRepository.findByUserName(username);
    if (userWithSameUsername.isPresent()) {
        throw new AlertConfigurationException(String.format("A user with username '%s' is already present", username));
    }
    String password = passwordEncoded ? user.getPassword() : defaultPasswordEncoder.encode(user.getPassword());
    AuthenticationTypeDetails authenticationType = authenticationTypeAccessor.getAuthenticationTypeDetails(user.getAuthenticationType()).orElseThrow(() -> new AlertRuntimeException("Cannot find Authentication Type."));
    UserEntity newEntity = new UserEntity(username, password, user.getEmailAddress(), authenticationType.getId());
    UserEntity savedEntity = userRepository.save(newEntity);
    UserModel model = createModel(savedEntity);
    roleAccessor.updateUserRoles(model.getId(), user.getRoles());
    return model;
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AlertRuntimeException(com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with AuthenticationTypeDetails

use of com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails in project hub-alert by blackducksoftware.

the class UserActionsTest method testReadAllWithoutChecks.

@Test
public void testReadAllWithoutChecks() {
    UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
    AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(1L, authenticationType.name());
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
    Mockito.when(userAccessor.getUsers()).thenReturn(List.of(userModel));
    Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails));
    UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
    ActionResponse<MultiUserConfigResponseModel> actionResponse = userActions.getAll();
    assertTrue(actionResponse.hasContent());
    List<UserConfig> userModels = actionResponse.getContent().get().getUsers();
    assertEquals(1, userModels.size());
    UserConfig userConfig = userModels.get(0);
    assertUserConfig(userConfig);
    assertEquals(authenticationType.name(), userConfig.getAuthenticationType());
    assertNull(userConfig.getPassword());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserManagementDescriptorKey(com.synopsys.integration.alert.component.users.UserManagementDescriptorKey) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Test(org.junit.jupiter.api.Test)

Example 3 with AuthenticationTypeDetails

use of com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails in project hub-alert by blackducksoftware.

the class DefaultUserAccessorTest method addUserTest.

@Test
public void addUserTest() throws Exception {
    final String roleName = "userName";
    UserEntity userEntity = new UserEntity(username, password, emailAddress, 2L);
    userEntity.setId(1L);
    AuthenticationTypeDetails authenticationTypeDetails = new AuthenticationTypeDetails(2L, "authentication-name");
    UserRoleRelation userRoleRelation = new UserRoleRelation(1L, 2L);
    UserRoleModel userRoleModel = createUserRoleModel(1L, roleName, true);
    Mockito.when(userRepository.findByUserName(Mockito.any())).thenReturn(Optional.empty());
    Mockito.when(authenticationTypeAccessor.getAuthenticationTypeDetails(Mockito.any())).thenReturn(Optional.of(authenticationTypeDetails));
    Mockito.when(userRepository.save(Mockito.any())).thenReturn(userEntity);
    createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    UserModel userModel = defaultUserAccessor.addUser(username, password, emailAddress);
    testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserEntity(com.synopsys.integration.alert.database.user.UserEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 4 with AuthenticationTypeDetails

use of com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails in project hub-alert by blackducksoftware.

the class DefaultAuthenticationTypeAccessorTest method getAuthenticationTypeDetailsNullTest.

@Test
public void getAuthenticationTypeDetailsNullTest() {
    AuthenticationTypeRepository authenticationTypeRepository = Mockito.mock(AuthenticationTypeRepository.class);
    DefaultAuthenticationTypeAccessor authenticationTypeAccessor = new DefaultAuthenticationTypeAccessor(authenticationTypeRepository);
    Mockito.when(authenticationTypeRepository.findById(Mockito.any())).thenReturn(Optional.empty());
    Optional<AuthenticationTypeDetails> testAuthenticationTypeDetails = authenticationTypeAccessor.getAuthenticationTypeDetails(AuthenticationType.DATABASE);
    assertFalse(testAuthenticationTypeDetails.isPresent());
}
Also used : AuthenticationTypeRepository(com.synopsys.integration.alert.database.user.AuthenticationTypeRepository) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) Test(org.junit.jupiter.api.Test)

Example 5 with AuthenticationTypeDetails

use of com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails in project hub-alert by blackducksoftware.

the class DefaultAuthenticationTypeAccessorTest method getAuthenticationTypeDetailsTest.

@Test
public void getAuthenticationTypeDetailsTest() {
    AuthenticationTypeRepository authenticationTypeRepository = Mockito.mock(AuthenticationTypeRepository.class);
    DefaultAuthenticationTypeAccessor authenticationTypeAccessor = new DefaultAuthenticationTypeAccessor(authenticationTypeRepository);
    AuthenticationTypeEntity authenticationTypeEntity = new AuthenticationTypeEntity("name-test");
    Mockito.when(authenticationTypeRepository.findById(Mockito.any())).thenReturn(Optional.of(authenticationTypeEntity));
    Optional<AuthenticationTypeDetails> testAuthenticationTypeDetails = authenticationTypeAccessor.getAuthenticationTypeDetails(AuthenticationType.DATABASE);
    assertTrue(testAuthenticationTypeDetails.isPresent());
    AuthenticationTypeDetails authenticationTypeDetails = testAuthenticationTypeDetails.get();
    assertEquals(authenticationTypeEntity.getName(), authenticationTypeDetails.getName());
    assertEquals(authenticationTypeEntity.getId(), authenticationTypeDetails.getId());
}
Also used : AuthenticationTypeRepository(com.synopsys.integration.alert.database.user.AuthenticationTypeRepository) AuthenticationTypeEntity(com.synopsys.integration.alert.database.user.AuthenticationTypeEntity) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) Test(org.junit.jupiter.api.Test)

Aggregations

AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)5 Test (org.junit.jupiter.api.Test)4 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)3 AuthenticationTypeRepository (com.synopsys.integration.alert.database.user.AuthenticationTypeRepository)2 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)2 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)1 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)1 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)1 UserManagementDescriptorKey (com.synopsys.integration.alert.component.users.UserManagementDescriptorKey)1 AuthenticationTypeEntity (com.synopsys.integration.alert.database.user.AuthenticationTypeEntity)1 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)1 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)1 Transactional (org.springframework.transaction.annotation.Transactional)1