Search in sources :

Example 16 with UserModel

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

the class UserActions method createWithoutChecks.

@Override
protected ActionResponse<UserConfig> createWithoutChecks(UserConfig resource) {
    try {
        String userName = resource.getUsername();
        String password = resource.getPassword();
        String emailAddress = resource.getEmailAddress();
        logger.debug(actionMessageCreator.createStartMessage("user", userName));
        UserModel userModel = userAccessor.addUser(userName, password, emailAddress);
        Long userId = userModel.getId();
        Set<String> configuredRoleNames = resource.getRoleNames();
        if (null != configuredRoleNames && !configuredRoleNames.isEmpty()) {
            Collection<UserRoleModel> roleNames = roleAccessor.getRoles().stream().filter(role -> configuredRoleNames.contains(role.getName())).collect(Collectors.toList());
            authorizationManager.updateUserRoles(userId, roleNames);
        }
        userModel = userAccessor.getUser(userId).orElse(userModel);
        logger.debug(actionMessageCreator.createSuccessMessage("User", userName));
        return new ActionResponse<>(HttpStatus.CREATED, convertDatabaseModelToRestModel(userModel));
    } catch (AlertException ex) {
        logger.error(actionMessageCreator.createErrorMessage("user", resource.getUsername()));
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("There was an issue creating the user. %s", ex.getMessage()));
    }
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserManagementDescriptorKey(com.synopsys.integration.alert.component.users.UserManagementDescriptorKey) AbstractResourceActions(com.synopsys.integration.alert.common.action.api.AbstractResourceActions) StringUtils(org.apache.commons.lang.StringUtils) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) Autowired(org.springframework.beans.factory.annotation.Autowired) DefaultUserRole(com.synopsys.integration.alert.common.enumeration.DefaultUserRole) UserSystemValidator(com.synopsys.integration.alert.component.users.UserSystemValidator) ArrayList(java.util.ArrayList) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertLoggerFactory(com.synopsys.integration.alert.common.logging.AlertLoggerFactory) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) ActionMessageCreator(com.synopsys.integration.alert.common.action.api.ActionMessageCreator) AuthenticationTypeDetails(com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails) UserAccessor(com.synopsys.integration.alert.common.persistence.accessor.UserAccessor) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Logger(org.slf4j.Logger) Collection(java.util.Collection) Set(java.util.Set) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) HttpStatus(org.springframework.http.HttpStatus) RoleAccessor(com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor) List(java.util.List) Component(org.springframework.stereotype.Component) AuthenticationTypeAccessor(com.synopsys.integration.alert.common.persistence.accessor.AuthenticationTypeAccessor) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Transactional(org.springframework.transaction.annotation.Transactional) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 17 with UserModel

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

the class ConfigurationOverridesStartupComponentTest method cleanUp.

@AfterEach
public void cleanUp() throws AlertException {
    Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    UserModel sysadmin = sysadminOptional.get();
    UserModel updatedSysadmin = changeUserPassword(sysadmin, DEFAULT_PASSWORD_ENCODED);
    userAccessor.updateUser(updatedSysadmin, true);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AfterEach(org.junit.jupiter.api.AfterEach)

Example 18 with UserModel

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

the class ConfigurationOverridesStartupComponentTest method testInitializeNoChange.

@Test
public void testInitializeNoChange() throws AlertException {
    Environment environment = Mockito.mock(Environment.class);
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
    // Update the sysadmin password
    Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    UserModel sysadmin = sysadminOptional.get();
    assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
    userAccessor.updateUser(updatedSysadmin, false);
    // Run the initialize method
    configurationOverridesStartupComponent.initialize();
    // Verify the sysadmin password is the updated password
    sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    sysadmin = sysadminOptional.get();
    assertNotEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    HttpSession session = Mockito.mock(HttpSession.class);
    Mockito.when(servletRequest.getSession()).thenReturn(session);
    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
    // Try to login with the updated password
    LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
    ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
    assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
    // Try to login with the default password
    LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
    actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
    assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) HttpSession(javax.servlet.http.HttpSession) LoginConfig(com.synopsys.integration.alert.component.authentication.web.LoginConfig) Environment(org.springframework.core.env.Environment) HttpServletResponse(javax.servlet.http.HttpServletResponse) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 19 with UserModel

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

the class ConfigurationOverridesStartupComponentTest method testInitializeResetPassword.

@Test
public void testInitializeResetPassword() throws AlertException {
    Environment environment = Mockito.mock(Environment.class);
    Mockito.when(environment.getProperty(ConfigurationOverridesStartupComponent.ENV_VAR_ADMIN_USER_PASSWORD_RESET)).thenReturn("true");
    EnvironmentVariableUtility environmentVariableUtility = new EnvironmentVariableUtility(environment);
    ConfigurationOverridesStartupComponent configurationOverridesStartupComponent = new ConfigurationOverridesStartupComponent(environmentVariableUtility, userAccessor, descriptorKey, configurationModelConfigurationAccessor, apiAction, configurationFieldModelConverter);
    // Update the sysadmin password
    Optional<UserModel> sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    UserModel sysadmin = sysadminOptional.get();
    assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    UserModel updatedSysadmin = changeUserPassword(sysadmin, UPDATED_PASSWORD);
    userAccessor.updateUser(updatedSysadmin, false);
    // Run the initialize method
    configurationOverridesStartupComponent.initialize();
    // Verify the sysadmin password is the default password
    sysadminOptional = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID);
    assertTrue(sysadminOptional.isPresent());
    sysadmin = sysadminOptional.get();
    assertEquals(DEFAULT_PASSWORD_ENCODED, sysadmin.getPassword());
    HttpServletRequest servletRequest = Mockito.mock(HttpServletRequest.class);
    HttpSession session = Mockito.mock(HttpSession.class);
    Mockito.when(servletRequest.getSession()).thenReturn(session);
    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
    // Try to login with the updated password
    LoginConfig updatedLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, UPDATED_PASSWORD);
    ActionResponse<Void> actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, updatedLoginConfig);
    assertEquals(HttpStatus.UNAUTHORIZED, actionResponse.getHttpStatus());
    // Try to login with the default password
    LoginConfig defaultLoginConfig = new LoginConfig(DEFAULT_ADMIN_USER, DEFAULT_PASSWORD);
    actionResponse = authenticationActions.authenticateUser(servletRequest, servletResponse, defaultLoginConfig);
    assertEquals(HttpStatus.NO_CONTENT, actionResponse.getHttpStatus());
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) HttpServletRequest(javax.servlet.http.HttpServletRequest) EnvironmentVariableUtility(com.synopsys.integration.alert.environment.EnvironmentVariableUtility) HttpSession(javax.servlet.http.HttpSession) LoginConfig(com.synopsys.integration.alert.component.authentication.web.LoginConfig) Environment(org.springframework.core.env.Environment) HttpServletResponse(javax.servlet.http.HttpServletResponse) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 20 with UserModel

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

the class DefaultUserAccessorTest method getUserByUserIdTest.

@Test
public void getUserByUserIdTest() {
    final Long userId = 1L;
    final Long emptyUserId = 5L;
    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.findById(userId)).thenReturn(Optional.of(userEntity));
    Mockito.when(userRepository.findById(emptyUserId)).thenReturn(Optional.empty());
    createModelMocks(userRoleRelation, userRoleModel, AuthenticationType.DATABASE);
    DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(userRepository, userRoleRepository, defaultPasswordEncoder, roleAccessor, authenticationTypeAccessor);
    Optional<UserModel> userModelOptional = defaultUserAccessor.getUser(userId);
    Optional<UserModel> userModelOptionalEmpty = defaultUserAccessor.getUser(emptyUserId);
    assertTrue(userModelOptional.isPresent());
    assertFalse(userModelOptionalEmpty.isPresent());
    UserModel userModel = userModelOptional.get();
    testUserModel(userEntity.getId(), username, emailAddress, roleName, userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) 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)

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