Search in sources :

Example 6 with RoleEntity

use of com.synopsys.integration.alert.database.user.RoleEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessorTest method deleteRoleTest.

@Test
public void deleteRoleTest() throws Exception {
    final String roleName = "roleName";
    final Long roleId = 1L;
    RoleEntity roleEntity = new RoleEntity(roleName, true);
    roleEntity.setId(1L);
    Mockito.when(roleRepository.findById(Mockito.any())).thenReturn(Optional.of(roleEntity));
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    authorizationUtility.deleteRole(roleId);
    Mockito.verify(roleRepository).deleteById(Mockito.any());
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) Test(org.junit.jupiter.api.Test)

Example 7 with RoleEntity

use of com.synopsys.integration.alert.database.user.RoleEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessorTest method deleteRoleCustomFalseTest.

@Test
public void deleteRoleCustomFalseTest() {
    DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository);
    RoleEntity roleEntity = new RoleEntity("name", false);
    roleEntity.setId(1L);
    Mockito.when(roleRepository.findById(Mockito.any())).thenReturn(Optional.of(roleEntity));
    try {
        authorizationUtility.deleteRole(1L);
        fail("Custom parameter of roleEntity set to 'false' did not throw expected AlertForbiddenOperationException.");
    } catch (AlertForbiddenOperationException e) {
        assertNotNull(e);
    }
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) AlertForbiddenOperationException(com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException) Test(org.junit.jupiter.api.Test)

Example 8 with RoleEntity

use of com.synopsys.integration.alert.database.user.RoleEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessor method createRoleWithPermissions.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) {
    RoleEntity roleEntity = createRole(roleName, true);
    List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix);
    return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions));
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) PermissionMatrixRelation(com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with RoleEntity

use of com.synopsys.integration.alert.database.user.RoleEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessor method getRoles.

@Override
@Transactional(readOnly = true)
public Set<UserRoleModel> getRoles() {
    List<RoleEntity> roleList = roleRepository.findAll();
    Set<UserRoleModel> userRoles = new LinkedHashSet<>();
    for (RoleEntity entity : roleList) {
        userRoles.add(new UserRoleModel(entity.getId(), entity.getRoleName(), entity.getCustom(), readPermissionsForRole(entity.getId())));
    }
    return userRoles;
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) LinkedHashSet(java.util.LinkedHashSet) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with RoleEntity

use of com.synopsys.integration.alert.database.user.RoleEntity in project hub-alert by blackducksoftware.

the class DefaultRoleAccessor method updateUserRoles.

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void updateUserRoles(Long userId, Collection<UserRoleModel> roles) {
    if (null != userId) {
        userRoleRepository.bulkDeleteAllByUserId(userId);
        if (null != roles && !roles.isEmpty()) {
            Collection<String> roleNames = roles.stream().map(UserRoleModel::getName).collect(Collectors.toSet());
            List<RoleEntity> roleEntities = roleRepository.findRoleEntitiesByRoleNames(roleNames);
            List<UserRoleRelation> roleRelations = new LinkedList<>();
            for (RoleEntity role : roleEntities) {
                roleRelations.add(new UserRoleRelation(userId, role.getId()));
            }
            userRoleRepository.saveAll(roleRelations);
        }
    }
}
Also used : RoleEntity(com.synopsys.integration.alert.database.user.RoleEntity) UserRoleRelation(com.synopsys.integration.alert.database.user.UserRoleRelation) LinkedList(java.util.LinkedList) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RoleEntity (com.synopsys.integration.alert.database.user.RoleEntity)15 Test (org.junit.jupiter.api.Test)9 UserRoleModel (com.synopsys.integration.alert.common.persistence.model.UserRoleModel)6 Transactional (org.springframework.transaction.annotation.Transactional)6 PermissionMatrixRelation (com.synopsys.integration.alert.database.authorization.PermissionMatrixRelation)5 AlertForbiddenOperationException (com.synopsys.integration.alert.common.exception.AlertForbiddenOperationException)3 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)3 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)3 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)3 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)3 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 PermissionMatrixRepository (com.synopsys.integration.alert.database.authorization.PermissionMatrixRepository)1 ConfigContextRepository (com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository)1 RegisteredDescriptorRepository (com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository)1 RoleRepository (com.synopsys.integration.alert.database.user.RoleRepository)1 UserRoleRelation (com.synopsys.integration.alert.database.user.UserRoleRelation)1 UserRoleRepository (com.synopsys.integration.alert.database.user.UserRoleRepository)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1