Search in sources :

Example 31 with UserRoleModel

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

the class UserModelTest method testUserModelEmptyRoles.

@Test
public void testUserModelEmptyRoles() {
    String expectedUserName = "expectedUser";
    String expectedPassword = "expectedPassword";
    String expectedEmail = "expectedEmail";
    Set<UserRoleModel> expectedRoles = new LinkedHashSet<>();
    UserModel userModel = UserModel.newUser(expectedUserName, expectedPassword, expectedEmail, AuthenticationType.DATABASE, expectedRoles, true);
    assertEquals(expectedUserName, userModel.getName());
    assertEquals(expectedPassword, userModel.getPassword());
    assertEquals(expectedEmail, userModel.getEmailAddress());
    assertTrue(userModel.getRoles().isEmpty());
    assertFalse(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) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Test(org.junit.jupiter.api.Test)

Example 32 with UserRoleModel

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

the class DefaultUserAccessor method createModel.

private UserModel createModel(UserEntity user) {
    List<UserRoleRelation> roleRelations = userRoleRepository.findAllByUserId(user.getId());
    List<Long> roleIdsForUser = roleRelations.stream().map(UserRoleRelation::getRoleId).collect(Collectors.toList());
    Set<UserRoleModel> roles = roleAccessor.getRoles(roleIdsForUser);
    AuthenticationType authenticationType = authenticationTypeAccessor.getAuthenticationType(user.getAuthenticationType()).orElse(null);
    return UserModel.existingUser(user.getId(), user.getUserName(), user.getPassword(), user.getEmailAddress(), authenticationType, roles, user.isEnabled());
}
Also used : UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) AuthenticationType(com.synopsys.integration.alert.common.enumeration.AuthenticationType)

Example 33 with UserRoleModel

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

the class MockRoleAccessor method updatePermissionsForRole.

@Override
public PermissionMatrixModel updatePermissionsForRole(String roleName, PermissionMatrixModel permissionMatrix) throws AlertConfigurationException {
    Long roleId = roleMap.entrySet().stream().filter(entry -> entry.getValue().getName().equals(roleName)).map(Map.Entry::getKey).findFirst().orElseThrow(() -> new AlertConfigurationException(String.format("role with name %s not found", roleName)));
    roleMap.computeIfPresent(roleId, (ignored, role) -> new UserRoleModel(role.getId(), role.getName(), role.isCustom(), permissionMatrix));
    return permissionMatrix;
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) RoleAccessor(com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) Collection(java.util.Collection) Map(java.util.Map) Set(java.util.Set) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) HashMap(java.util.HashMap) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) Collectors(java.util.stream.Collectors) AtomicLong(java.util.concurrent.atomic.AtomicLong) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)

Example 34 with UserRoleModel

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

the class UserDetailsService method loadUserBySAML.

@Override
public Object loadUserBySAML(SAMLCredential credential) throws UsernameNotFoundException {
    String userName = credential.getNameID().getValue();
    String emailAddress = StringUtils.contains(userName, "@") ? userName : null;
    String[] alertRoles = credential.getAttributeAsStringArray(authoritiesPopulator.getSAMLRoleAttributeName("AlertRoles"));
    Set<String> existingRoles = Set.of();
    if (alertRoles != null) {
        existingRoles = Arrays.stream(alertRoles).collect(Collectors.toSet());
    }
    Set<String> roleNames = authoritiesPopulator.addAdditionalRoleNames(userName, existingRoles, false);
    Set<UserRoleModel> roles = roleNames.stream().map(UserRoleModel::of).collect(Collectors.toSet());
    UserModel userModel = UserModel.newUser(userName, "", emailAddress, AuthenticationType.SAML, roles, true);
    return new UserPrincipal(userModel);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) UserPrincipal(com.synopsys.integration.alert.common.security.UserPrincipal)

Example 35 with UserRoleModel

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

the class UserDetailsServiceTest method initializeAuthoritiesPopulator.

@BeforeEach
public void initializeAuthoritiesPopulator() {
    Set<UserRoleModel> roles = Arrays.stream(VALID_DB_ROLES).map(UserRoleModel::of).collect(Collectors.toSet());
    UserModel userModel = UserModel.newUser(USER_NAME, "password", EMAIL, AuthenticationType.SAML, roles, true);
    AuthenticationDescriptorKey key = new AuthenticationDescriptorKey();
    ConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = Mockito.mock(ConfigurationModelConfigurationAccessor.class);
    ConfigurationModel configuration = Mockito.mock(ConfigurationModel.class);
    UserAccessor userAccessor = Mockito.mock(UserAccessor.class);
    Mockito.when(configuration.getField(Mockito.anyString())).thenReturn(Optional.empty());
    Mockito.when(configurationModelConfigurationAccessor.getConfigurationsByDescriptorKey(Mockito.eq(key))).thenReturn(List.of(configuration));
    Mockito.when(userAccessor.getUser(Mockito.anyString())).thenReturn(Optional.of(userModel));
    authoritiesPopulator = new UserManagementAuthoritiesPopulator(key, configurationModelConfigurationAccessor, userAccessor);
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) UserManagementAuthoritiesPopulator(com.synopsys.integration.alert.component.authentication.security.UserManagementAuthoritiesPopulator) UserAccessor(com.synopsys.integration.alert.common.persistence.accessor.UserAccessor) AuthenticationDescriptorKey(com.synopsys.integration.alert.component.authentication.descriptor.AuthenticationDescriptorKey) ConfigurationModelConfigurationAccessor(com.synopsys.integration.alert.common.persistence.accessor.ConfigurationModelConfigurationAccessor) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)36 Test (org.junit.jupiter.api.Test)24 UserModel (com.synopsys.integration.alert.common.persistence.model.UserModel)17 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)8 UserEntity (com.synopsys.integration.alert.database.user.UserEntity)7 RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)4 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)4 AuthenticationType (com.synopsys.integration.alert.common.enumeration.AuthenticationType)4 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)4 LinkedHashSet (java.util.LinkedHashSet)4 Transactional (org.springframework.transaction.annotation.Transactional)4 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)3 RoleAccessor (com.synopsys.integration.alert.common.descriptor.accessor.RoleAccessor)3 DefaultUserRole (com.synopsys.integration.alert.common.enumeration.DefaultUserRole)3 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 UserAccessor (com.synopsys.integration.alert.common.persistence.accessor.UserAccessor)3 AuthenticationTypeDetails (com.synopsys.integration.alert.common.persistence.model.AuthenticationTypeDetails)3 Collection (java.util.Collection)3