Search in sources :

Example 1 with UserModel

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

the class AuthenticationEventHandler method handle.

@Override
public void handle(AlertAuthenticationEvent event) {
    UserModel user = event.getUser();
    if (null != user) {
        try {
            Optional<UserModel> userModel = userAccessor.getUser(user.getName());
            if (userModel.isPresent() && user.isExternal()) {
                UserModel model = userModel.get();
                UserModel updatedUser = UserModel.existingUser(model.getId(), user.getName(), user.getPassword(), user.getEmailAddress(), user.getAuthenticationType(), user.getRoles(), user.isEnabled());
                userAccessor.updateUser(updatedUser, true);
            } else {
                userAccessor.addUser(user, true);
            }
        } catch (AlertForbiddenOperationException ignored) {
        // Cannot update an external user's credentials
        } catch (AlertConfigurationException ignored) {
        // User already exists. Nothing to do.
        }
    }
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Example 2 with UserModel

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

the class AuthenticationEventManager method sendAuthenticationEvent.

private void sendAuthenticationEvent(String username, String emailAddress, AuthenticationType authenticationType, Collection<? extends GrantedAuthority> authorities) throws AlertException {
    if (username == null) {
        throw new AlertException("Unable to send authentication event with null username");
    }
    Set<UserRoleModel> alertRoles = authorities.stream().map(this::getRoleFromAuthority).flatMap(Optional::stream).map(UserRoleModel::of).collect(Collectors.toSet());
    // The database users will not be enabled because they already exist in the database when this is called. So a new entry will not be added to the database.
    UserModel userModel = UserModel.newUser(username, null, emailAddress, authenticationType, alertRoles, true);
    sendAuthenticationEvent(userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) Optional(java.util.Optional) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel)

Example 3 with UserModel

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

the class UserModelTest method testUserModel.

@Test
public void testUserModel() {
    String expectedUserName = "expectedUser";
    String expectedPassword = "expectedPassword";
    String expectedEmail = "expectedEmail";
    Set<String> roleNames = new LinkedHashSet<>(Arrays.asList(DefaultUserRole.values()).stream().map(DefaultUserRole::name).collect(Collectors.toList()));
    Set<UserRoleModel> expectedRoles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
    UserModel userModel = UserModel.newUser(expectedUserName, expectedPassword, expectedEmail, AuthenticationType.DATABASE, expectedRoles, true);
    assertEquals(expectedUserName, userModel.getName());
    assertEquals(expectedPassword, userModel.getPassword());
    assertEquals(expectedEmail, userModel.getEmailAddress());
    assertEquals(expectedRoles.size(), userModel.getRoles().size());
    assertTrue(userModel.hasRole(DefaultUserRole.ALERT_ADMIN.name()));
    assertFalse(userModel.hasRole("UNKNOWN_ROLE"));
    assertFalse(userModel.isExpired());
    assertFalse(userModel.isLocked());
    assertFalse(userModel.isPasswordExpired());
    assertTrue(userModel.isEnabled());
    assertFalse(userModel.isExternal());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) DefaultUserRole(com.synopsys.integration.alert.common.enumeration.DefaultUserRole) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 4 with UserModel

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

the class UserAccessorTestIT method testUpdateUser.

@Test
public void testUpdateUser() throws AlertForbiddenOperationException, AlertConfigurationException {
    String userName = "testUser";
    String password = "testPassword";
    String email = "testEmail";
    UserModel userModel = userAccessor.addUser(userName, password, email);
    assertNotNull(userModel);
    assertEquals(userName, userModel.getName());
    assertEquals(email, userModel.getEmailAddress());
    assertTrue(userModel.getRoles().isEmpty());
    String another_role = "ANOTHER_ROLE";
    String admin_role = AlertIntegrationTestConstants.ROLE_ALERT_ADMIN;
    Set<String> roleNames = new LinkedHashSet<>(Arrays.asList(admin_role, another_role));
    Set<UserRoleModel> roles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
    UserModel updatedModel = userAccessor.updateUser(UserModel.existingUser(userModel.getId(), userModel.getName(), userModel.getPassword(), userModel.getEmailAddress(), AuthenticationType.DATABASE, roles, true), true);
    assertEquals(userModel.getName(), updatedModel.getName());
    assertEquals(userModel.getEmailAddress(), updatedModel.getEmailAddress());
    assertEquals(userModel.getPassword(), updatedModel.getPassword());
    assertEquals(1, updatedModel.getRoles().size());
    assertFalse(updatedModel.hasRole(another_role));
    assertTrue(updatedModel.hasRole(admin_role));
    assertFalse(updatedModel.isExternal());
    userAccessor.deleteUser(userName);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) LinkedHashSet(java.util.LinkedHashSet) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with UserModel

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

the class UserAccessorTestIT method testExternalUserUpdateEmailException.

@Test
public void testExternalUserUpdateEmailException() throws AlertForbiddenOperationException, AlertConfigurationException {
    String userName = "testUser";
    String password = "testPassword";
    String email = "testEmail";
    UserModel userModel = UserModel.newUser(userName, password, email, AuthenticationType.LDAP, Collections.emptySet(), true);
    userModel = userAccessor.addUser(userModel, false);
    UserModel updatedUser = UserModel.existingUser(userModel.getId(), userName, null, email + "_updated", AuthenticationType.LDAP, Collections.emptySet(), true);
    testUserUpdateException(updatedUser);
    userAccessor.deleteUser(userName);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) 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