Search in sources :

Example 1 with UserRoleModel

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

the class AuthorizationManager method createRoleWithPermissions.

public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) {
    UserRoleModel roleWithPermissions = roleAccessor.createRoleWithPermissions(roleName, permissionMatrix);
    updateRoleInCache(roleWithPermissions.getName(), roleWithPermissions.getPermissions());
    return roleWithPermissions;
}
Also used : UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel)

Example 2 with UserRoleModel

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

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

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

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

the class DefaultRoleAccessorTest method createRoleWithPermissions.

@Test
public void createRoleWithPermissions() {
    final String roleName = "roleName";
    final String contextString = "context-test";
    final String descriptorName = "descriptorName";
    RoleEntity roleEntity = new RoleEntity(roleName, true);
    roleEntity.setId(1L);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString);
    configContextEntity.setId(1L);
    RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L);
    registeredDescriptorEntity.setId(1L);
    PermissionKey permissionKey = new PermissionKey(contextString, descriptorName);
    PermissionMatrixModel permissionMatrixModel = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()));
    PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit());
    Mockito.when(roleRepository.save(Mockito.any())).thenReturn(new RoleEntity(roleName, true));
    mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity);
    mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity);
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    UserRoleModel userRoleModel = authorizationUtility.createRoleWithPermissions(roleName, permissionMatrixModel);
    Mockito.verify(permissionMatrixRepository).deleteAll(Mockito.any());
    assertEquals(roleName, userRoleModel.getName());
    assertTrue(userRoleModel.isCustom());
    assertEquals(permissionMatrixModel, userRoleModel.getPermissions());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) PermissionKey(com.synopsys.integration.alert.common.persistence.model.PermissionKey) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

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