Search in sources :

Example 11 with AcceptedException

use of eu.bcvsolutions.idm.core.api.exception.AcceptedException in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleRequestServiceIntegrationTest method testDeleteTreeAutomaticRoleApproval.

@Test
public void testDeleteTreeAutomaticRoleApproval() {
    IdmRoleDto role = prepareRole();
    IdmTreeNodeDto nodeOne = helper.createTreeNode();
    IdmIdentityDto guaranteeIdentity = helper.createIdentity();
    IdmRoleGuaranteeDto guarantee = new IdmRoleGuaranteeDto();
    guarantee.setRole(role.getId());
    guarantee.setGuarantee(guaranteeIdentity.getId());
    role.getGuarantees().add(guarantee);
    role = roleService.save(role);
    IdmRoleTreeNodeDto automaticRole = new IdmRoleTreeNodeDto();
    automaticRole.setRole(role.getId());
    automaticRole.setName(role.getName());
    automaticRole.setTreeNode(nodeOne.getId());
    // Create automatic role via manager
    automaticRole = automaticRoleManager.createAutomaticRoleByTree(automaticRole, true);
    Assert.assertNotNull(automaticRole.getId());
    IdmRoleTreeNodeDto treeAutomaticRole = roleTreeNodeService.get(automaticRole.getId());
    Assert.assertNotNull(treeAutomaticRole);
    Assert.assertEquals(nodeOne.getId(), treeAutomaticRole.getTreeNode());
    Assert.assertEquals(role.getId(), treeAutomaticRole.getRole());
    // Delete automatic role via manager
    try {
        automaticRoleManager.deleteAutomaticRole(automaticRole, false);
    } catch (AcceptedException ex) {
        // The request is in approval
        Assert.assertNotNull(ex.getIdentifier());
        UUID requestId = UUID.fromString(ex.getIdentifier());
        loginAsNoAdmin(guaranteeIdentity.getUsername());
        try {
            completeTasksFromUsers(guaranteeIdentity.getUsername(), "approve");
        } catch (ResultCodeException e) {
            fail("User has permission to approve task. Error message: " + e.getLocalizedMessage());
        } catch (Exception e) {
            fail("Some problem: " + e.getLocalizedMessage());
        }
        IdmAutomaticRoleRequestDto request = roleRequestService.get(requestId);
        Assert.assertEquals(RequestState.EXECUTED, request.getState());
        IdmRoleTreeNodeDto deletedAutomaticRole = roleTreeNodeService.get(automaticRole.getId());
        Assert.assertNull(deletedAutomaticRole);
        return;
    }
    fail("Automatic role request have to be approving by gurantee!");
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmRoleGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleGuaranteeDto) IdmRoleTreeNodeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleTreeNodeDto) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmTreeNodeDto(eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 12 with AcceptedException

use of eu.bcvsolutions.idm.core.api.exception.AcceptedException in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleRequestServiceIntegrationTest method testCreateAutomaticAttributeRoleWithApproval.

@Test
public void testCreateAutomaticAttributeRoleWithApproval() {
    IdmRoleDto role = prepareRole();
    IdmIdentityDto identity = helper.createIdentity();
    IdmIdentityDto guaranteeIdentity = helper.createIdentity();
    IdmRoleGuaranteeDto guarantee = new IdmRoleGuaranteeDto();
    guarantee.setRole(role.getId());
    guarantee.setGuarantee(guaranteeIdentity.getId());
    role.getGuarantees().add(guarantee);
    role = roleService.save(role);
    IdmAutomaticRoleAttributeDto automaticRole = new IdmAutomaticRoleAttributeDto();
    automaticRole.setRole(role.getId());
    automaticRole.setName(role.getName());
    IdmAutomaticRoleAttributeRuleDto rule = new IdmAutomaticRoleAttributeRuleDto();
    rule.setAttributeName(IdmIdentity_.username.getName());
    rule.setComparison(AutomaticRoleAttributeRuleComparison.EQUALS);
    rule.setType(AutomaticRoleAttributeRuleType.IDENTITY);
    rule.setValue(identity.getUsername());
    // Create automatic role via manager
    try {
        automaticRole = automaticRoleManager.createAutomaticRoleByAttribute(automaticRole, false, rule);
    } catch (AcceptedException ex) {
        // The request is in approval
        Assert.assertNotNull(ex.getIdentifier());
        UUID requestId = UUID.fromString(ex.getIdentifier());
        loginAsNoAdmin(guaranteeIdentity.getUsername());
        try {
            completeTasksFromUsers(guaranteeIdentity.getUsername(), "approve");
        } catch (ResultCodeException e) {
            fail("User has permission to approve task. Error message: " + e.getLocalizedMessage());
        } catch (Exception e) {
            fail("Some problem: " + e.getLocalizedMessage());
        }
        IdmAutomaticRoleRequestDto request = roleRequestService.get(requestId);
        Assert.assertEquals(RequestState.EXECUTED, request.getState());
        Assert.assertNotNull(request.getAutomaticRole());
        automaticRole = automaticRoleAttributeService.get(request.getAutomaticRole());
        Assert.assertNotNull(automaticRole);
        Assert.assertEquals(role.getId(), automaticRole.getRole());
        return;
    }
    fail("Automatic role request have to be approving by gurantee!");
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) IdmRoleGuaranteeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleGuaranteeDto) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) IdmAutomaticRoleAttributeRuleDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmAutomaticRoleAttributeDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto) AbstractCoreWorkflowIntegrationTest(eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest) Test(org.junit.Test)

Example 13 with AcceptedException

use of eu.bcvsolutions.idm.core.api.exception.AcceptedException in project CzechIdMng by bcvsolutions.

the class RoleDeleteProcessor method process.

@Override
public EventResult<IdmRoleDto> process(EntityEvent<IdmRoleDto> event) {
    IdmRoleDto role = event.getContent();
    // role assigned to identity could not be deleted
    if (identityRoleRepository.countByRole_Id(role.getId()) > 0) {
        throw new ResultCodeException(CoreResultCode.ROLE_DELETE_FAILED_IDENTITY_ASSIGNED, ImmutableMap.of("role", role.getName()));
    }
    // 
    // automatic role attribute has assigned this role
    IdmAutomaticRoleFilter automaticRoleFilter = new IdmAutomaticRoleFilter();
    automaticRoleFilter.setRoleId(role.getId());
    long totalElements = automaticRoleAttributeService.find(automaticRoleFilter, new PageRequest(0, 1)).getTotalElements();
    if (totalElements > 0) {
        // some automatic role attribute has assigned this role
        throw new ResultCodeException(CoreResultCode.ROLE_DELETE_FAILED_AUTOMATIC_ROLE_ASSIGNED, ImmutableMap.of("role", role.getName()));
    }
    // 
    // remove related automatic roles
    IdmRoleTreeNodeFilter filter = new IdmRoleTreeNodeFilter();
    filter.setRoleId(role.getId());
    roleTreeNodeService.find(filter, null).forEach(roleTreeNode -> {
        try {
            roleTreeNodeService.delete(roleTreeNode);
        } catch (AcceptedException ex) {
            throw new ResultCodeException(CoreResultCode.ROLE_DELETE_FAILED_HAS_TREE_NODE, ImmutableMap.of("role", role.getName(), "roleTreeNode", roleTreeNode.getId()));
        }
    });
    // Find all concepts and remove relation on role
    IdmConceptRoleRequestFilter conceptRequestFilter = new IdmConceptRoleRequestFilter();
    conceptRequestFilter.setRoleId(role.getId());
    conceptRoleRequestService.find(conceptRequestFilter, null).getContent().forEach(concept -> {
        IdmRoleRequestDto request = roleRequestService.get(concept.getRoleRequest());
        String message = null;
        if (concept.getState().isTerminatedState()) {
            message = MessageFormat.format("Role [{0}] (requested in concept [{1}]) was deleted (not from this role request)!", role.getName(), concept.getId());
        } else {
            message = MessageFormat.format("Request change in concept [{0}], was not executed, because requested role [{1}] was deleted (not from this role request)!", concept.getId(), role.getName());
            concept.setState(RoleRequestState.CANCELED);
        }
        roleRequestService.addToLog(request, message);
        conceptRoleRequestService.addToLog(concept, message);
        concept.setRole(null);
        roleRequestService.save(request);
        conceptRoleRequestService.save(concept);
    });
    // remove all policies
    IdmAuthorizationPolicyFilter policyFilter = new IdmAuthorizationPolicyFilter();
    policyFilter.setRoleId(role.getId());
    authorizationPolicyService.find(policyFilter, null).forEach(dto -> {
        authorizationPolicyService.delete(dto);
    });
    // Find all automatic role requests and remove relation on automatic role
    UUID roleId = role.getId();
    if (roleId != null) {
        IdmAutomaticRoleRequestFilter automaticRoleRequestFilter = new IdmAutomaticRoleRequestFilter();
        automaticRoleRequestFilter.setRoleId(roleId);
        automaticRoleRequestService.find(automaticRoleRequestFilter, null).getContent().forEach(request -> {
            request.setRole(null);
            automaticRoleRequestService.save(request);
            automaticRoleRequestService.cancel(request);
        });
    }
    // 
    // remove role guarantees, sub roles and catalog works automatically by hibenate mapping
    service.deleteInternal(role);
    // 
    return new DefaultEventResult<>(event, this);
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmAuthorizationPolicyFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAuthorizationPolicyFilter) IdmAutomaticRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleFilter) IdmConceptRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmConceptRoleRequestFilter) PageRequest(org.springframework.data.domain.PageRequest) IdmRoleTreeNodeFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRoleTreeNodeFilter) DefaultEventResult(eu.bcvsolutions.idm.core.api.event.DefaultEventResult) UUID(java.util.UUID) IdmAutomaticRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleRequestFilter) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)

Example 14 with AcceptedException

use of eu.bcvsolutions.idm.core.api.exception.AcceptedException in project CzechIdMng by bcvsolutions.

the class DefaultAutomaticRoleManager method changeAutomaticRoleRules.

@Override
public IdmAutomaticRoleAttributeDto changeAutomaticRoleRules(IdmAutomaticRoleAttributeDto automaticRole, boolean executeImmediately, IdmAutomaticRoleAttributeRuleDto... newRules) {
    Assert.notNull(automaticRole);
    Assert.notNull(automaticRole.getId(), "Automatic role must exists!");
    IdmAutomaticRoleRequestDto request = new IdmAutomaticRoleRequestDto();
    request.setOperation(RequestOperationType.UPDATE);
    request.setRequestType(AutomaticRoleRequestType.ATTRIBUTE);
    request.setExecuteImmediately(executeImmediately);
    request.setAutomaticRole(automaticRole.getId());
    request.setName(automaticRole.getName());
    request.setRole(automaticRole.getRole());
    final IdmAutomaticRoleRequestDto createdRequest = roleRequestService.save(request);
    ArrayList<IdmAutomaticRoleAttributeRuleDto> rules = Lists.newArrayList(newRules);
    if (rules != null) {
        // Creates request for change or add rule
        rules.forEach(rule -> {
            IdmAutomaticRoleAttributeRuleRequestDto ruleRequest = new IdmAutomaticRoleAttributeRuleRequestDto();
            ruleRequest.setRequest(createdRequest.getId());
            ruleRequest.setOperation(rule.getId() != null ? RequestOperationType.UPDATE : RequestOperationType.ADD);
            ruleRequest.setAttributeName(rule.getAttributeName());
            ruleRequest.setComparison(rule.getComparison());
            ruleRequest.setType(rule.getType());
            ruleRequest.setFormAttribute(rule.getFormAttribute());
            ruleRequest.setValue(rule.getValue());
            ruleRequest.setRule(rule.getId());
            ruleRequest = ruleRequestService.save(ruleRequest);
        });
    }
    IdmAutomaticRoleAttributeRuleFilter ruleFilter = new IdmAutomaticRoleAttributeRuleFilter();
    ruleFilter.setAutomaticRoleAttributeId(automaticRole.getId());
    List<IdmAutomaticRoleAttributeRuleDto> currentRules = ruleService.find(ruleFilter, null).getContent();
    currentRules.stream().filter(currentRule -> {
        return rules == null || !rules.contains(currentRule);
    }).forEach(ruleToDelete -> {
        // Creates request for remove rule
        IdmAutomaticRoleAttributeRuleRequestDto ruleRequest = new IdmAutomaticRoleAttributeRuleRequestDto();
        ruleRequest.setRequest(createdRequest.getId());
        ruleRequest.setOperation(RequestOperationType.REMOVE);
        ruleRequest.setAttributeName(ruleToDelete.getAttributeName());
        ruleRequest.setComparison(ruleToDelete.getComparison());
        ruleRequest.setType(ruleToDelete.getType());
        ruleRequest.setFormAttribute(ruleToDelete.getFormAttribute());
        ruleRequest.setValue(ruleToDelete.getValue());
        ruleRequest.setRule(ruleToDelete.getId());
        ruleRequest = ruleRequestService.save(ruleRequest);
    });
    IdmAutomaticRoleRequestDto executedRequest = roleRequestService.startRequestInternal(createdRequest.getId(), true);
    if (RequestState.EXECUTED == executedRequest.getState()) {
        UUID createdAutomaticRoleId = executedRequest.getAutomaticRole();
        Assert.notNull(createdAutomaticRoleId);
        return automaticRoleAttributeService.get(executedRequest.getAutomaticRole());
    }
    if (RequestState.IN_PROGRESS == executedRequest.getState()) {
        throw new AcceptedException(executedRequest.getId().toString());
    }
    if (RequestState.EXCEPTION == executedRequest.getState()) {
        throw new CoreException(executedRequest.getResult().getCause());
    }
    return null;
}
Also used : RequestOperationType(eu.bcvsolutions.idm.core.api.domain.RequestOperationType) AbstractIdmAutomaticRoleDto(eu.bcvsolutions.idm.core.api.dto.AbstractIdmAutomaticRoleDto) RequestState(eu.bcvsolutions.idm.core.api.domain.RequestState) IdmRoleTreeNodeService(eu.bcvsolutions.idm.core.api.service.IdmRoleTreeNodeService) Autowired(org.springframework.beans.factory.annotation.Autowired) IdmAutomaticRoleAttributeRuleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleAttributeRuleFilter) ArrayList(java.util.ArrayList) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) AutomaticRoleManager(eu.bcvsolutions.idm.core.api.service.AutomaticRoleManager) IdmAutomaticRoleAttributeDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto) IdmAutomaticRoleAttributeRuleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleRequestDto) Lists(com.google.common.collect.Lists) IdmAutomaticRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleRequestService) IdmAutomaticRoleAttributeRuleDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto) Service(org.springframework.stereotype.Service) AutomaticRoleRequestType(eu.bcvsolutions.idm.core.api.domain.AutomaticRoleRequestType) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IdmAutomaticRoleAttributeRuleRequestService(eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleAttributeRuleRequestService) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) UUID(java.util.UUID) IdmRoleTreeNodeDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleTreeNodeDto) List(java.util.List) IdmAutomaticRoleAttributeRuleService(eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleAttributeRuleService) IdmAutomaticRoleAttributeService(eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleAttributeService) Assert(org.springframework.util.Assert) IdmAutomaticRoleAttributeRuleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleRequestDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) IdmAutomaticRoleAttributeRuleDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) IdmAutomaticRoleAttributeRuleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleAttributeRuleFilter) UUID(java.util.UUID)

Example 15 with AcceptedException

use of eu.bcvsolutions.idm.core.api.exception.AcceptedException in project CzechIdMng by bcvsolutions.

the class DefaultIdmAutomaticRoleAttributeService method delete.

/**
 * Publish {@link AutomaticRoleAttributeEvent} only.
 *
 * @see {@link AutomaticRoleAttributeDeleteProcessor}
 */
@Override
@Transactional(noRollbackFor = AcceptedException.class)
public void delete(IdmAutomaticRoleAttributeDto dto, BasePermission... permission) {
    Assert.notNull(dto);
    checkAccess(this.getEntity(dto.getId()), permission);
    // 
    LOG.debug("Deleting automatic role by attribute [{}]", dto.getRole());
    // 
    EventContext<IdmAutomaticRoleAttributeDto> context = entityEventManager.process(new AutomaticRoleAttributeEvent(AutomaticRoleAttributeEventType.DELETE, dto));
    // 
    if (context.isSuspended()) {
        throw new AcceptedException();
    }
}
Also used : AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) AutomaticRoleAttributeEvent(eu.bcvsolutions.idm.core.model.event.AutomaticRoleAttributeEvent) IdmAutomaticRoleAttributeDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)17 IdmAutomaticRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto)10 UUID (java.util.UUID)10 IdmRoleTreeNodeDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleTreeNodeDto)8 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)7 IdmAutomaticRoleAttributeDto (eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeDto)6 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)6 AbstractCoreWorkflowIntegrationTest (eu.bcvsolutions.idm.core.AbstractCoreWorkflowIntegrationTest)5 IdmAutomaticRoleAttributeRuleDto (eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleDto)5 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)5 IdmRoleGuaranteeDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleGuaranteeDto)5 RoleRequestException (eu.bcvsolutions.idm.core.api.exception.RoleRequestException)5 Test (org.junit.Test)5 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)4 IdmTreeNodeDto (eu.bcvsolutions.idm.core.api.dto.IdmTreeNodeDto)3 IdmRoleTreeNodeFilter (eu.bcvsolutions.idm.core.api.dto.filter.IdmRoleTreeNodeFilter)3 Transactional (org.springframework.transaction.annotation.Transactional)3 IdmAutomaticRoleAttributeRuleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleRequestDto)2 DefaultEventResult (eu.bcvsolutions.idm.core.api.event.DefaultEventResult)2 RoleTreeNodeEvent (eu.bcvsolutions.idm.core.model.event.RoleTreeNodeEvent)2