Search in sources :

Example 6 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel 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 7 with UserModel

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

the class AuthenticationActionsTestIT method testAuthenticateDBUserRoleFailIT.

@Test
public void testAuthenticateDBUserRoleFailIT() throws AlertForbiddenOperationException, AlertConfigurationException {
    HttpServletRequest servletRequest = new MockHttpServletRequest();
    HttpServletResponse servletResponse = new MockHttpServletResponse();
    // add a user test then delete a user.
    String userName = String.format("testuser_%s", UUID.randomUUID());
    mockLoginRestModel.setAlertUsername(userName);
    AuthenticationActions authenticationActions = new AuthenticationActions(authenticationProvider, csrfTokenRepository);
    userAccessor.addUser(userName, mockLoginRestModel.getAlertPassword(), "");
    ActionResponse<Void> response = authenticationActions.authenticateUser(servletRequest, servletResponse, mockLoginRestModel.createRestModel());
    assertTrue(response.isError());
    Optional<UserModel> userModel = userAccessor.getUser(userName);
    assertTrue(userModel.isPresent());
    UserModel model = userModel.get();
    assertFalse(model.hasRole(AlertIntegrationTestConstants.ROLE_ALERT_ADMIN));
    assertTrue(model.getRoles().isEmpty());
    userAccessor.deleteUser(userName);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 8 with UserModel

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

the class UserActionsTest method testReadWithoutChecks.

@Test
public void testReadWithoutChecks() {
    UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
    Mockito.when(authorizationManager.hasReadPermission(Mockito.any(ConfigContextEnum.class), Mockito.any(DescriptorKey.class))).thenReturn(true);
    Mockito.when(userAccessor.getUser(id)).thenReturn(Optional.of(userModel));
    Mockito.when(userAccessor.getUser(2L)).thenReturn(Optional.empty());
    UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
    ActionResponse<UserConfig> actionResponse = userActions.getOne(id);
    ActionResponse<UserConfig> actionResponseEmpty = userActions.getOne(2L);
    assertTrue(actionResponse.hasContent());
    assertEquals(HttpStatus.OK, actionResponse.getHttpStatus());
    UserConfig userConfig = actionResponse.getContent().get();
    assertUserConfig(userConfig);
    assertFalse(actionResponseEmpty.hasContent());
    assertEquals(HttpStatus.NOT_FOUND, actionResponseEmpty.getHttpStatus());
}
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) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Test(org.junit.jupiter.api.Test)

Example 9 with UserModel

use of com.synopsys.integration.alert.common.persistence.model.UserModel 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 10 with UserModel

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

the class UserActionsTest method testDeleteWithoutChecks.

@Test
public void testDeleteWithoutChecks() throws Exception {
    UserModel userModel = UserModel.existingUser(id, name, password, emailAddress, authenticationType, roles, true);
    Mockito.when(userAccessor.getUser(id)).thenReturn(Optional.of(userModel));
    UserActions userActions = new UserActions(userManagementDescriptorKey, userAccessor, roleAccessor, authorizationManager, authenticationTypeAccessor, userSystemValidator);
    ActionResponse<UserConfig> userConfigActionResponse = userActions.deleteWithoutChecks(id);
    Mockito.verify(userAccessor).deleteUser(id);
    assertFalse(userConfigActionResponse.hasContent());
    assertEquals(HttpStatus.NO_CONTENT, userConfigActionResponse.getHttpStatus());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) Test(org.junit.jupiter.api.Test)

Aggregations

UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)42 Test (org.junit.jupiter.api.Test)32 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)17 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)12 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)8 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)7 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)6 UserManagementDescriptorKey (com.synopsys.integration.alert.component.users.UserManagementDescriptorKey)6 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)5 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)4 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)4 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 DefaultUserRole (com.synopsys.integration.alert.common.enumeration.DefaultUserRole)3 UserAccessor (com.synopsys.integration.alert.common.persistence.accessor.UserAccessor)3 LoginConfig (com.synopsys.integration.alert.component.authentication.web.LoginConfig)3