Search in sources :

Example 1 with RoleRequestException

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

the class DefaultIdmAutomaticRoleRequestService method startApprovalProcess.

@Override
@Transactional
public boolean startApprovalProcess(IdmAutomaticRoleRequestDto request, boolean checkRight, EntityEvent<IdmAutomaticRoleRequestDto> event, String wfDefinition) {
    // and do realization immediately (without start approval process)
    if (request.isExecuteImmediately()) {
        boolean haveRightExecuteImmediately = securityService.hasAnyAuthority(CoreGroupPermission.AUTOMATIC_ROLE_REQUEST_ADMIN);
        if (checkRight && !haveRightExecuteImmediately) {
            throw new RoleRequestException(CoreResultCode.ROLE_REQUEST_NO_EXECUTE_IMMEDIATELY_RIGHT, ImmutableMap.of("new", request));
        }
        // Execute request immediately
        return true;
    } else {
        Map<String, Object> variables = new HashMap<>();
        // Minimize size of DTO persisting to WF
        IdmAutomaticRoleRequestDto eventRequest = event.getContent();
        eventRequest.setEmbedded(null);
        variables.put(EntityEvent.EVENT_PROPERTY, event);
        variables.put("approvalForAutomaticRole", Boolean.TRUE);
        ProcessInstance processInstance = workflowProcessInstanceService.startProcess(wfDefinition, IdmRoleDto.class.getSimpleName(), request.getCreator(), request.getCreatorId().toString(), variables);
        // We have to refresh request (maybe was changed in wf process)
        request = this.get(request.getId());
        request.setWfProcessId(processInstance.getProcessInstanceId());
        this.save(request);
    }
    return false;
}
Also used : IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) HashMap(java.util.HashMap) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with RoleRequestException

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

the class DefaultIdmRoleRequestService method startApprovalProcess.

@Override
@Transactional
public boolean startApprovalProcess(IdmRoleRequestDto request, boolean checkRight, EntityEvent<IdmRoleRequestDto> event, String wfDefinition) {
    // and do realization immediately (without start approval process)
    if (request.isExecuteImmediately()) {
        boolean haveRightExecuteImmediately = securityService.hasAnyAuthority(CoreGroupPermission.ROLE_REQUEST_EXECUTE);
        if (checkRight && !haveRightExecuteImmediately) {
            throw new RoleRequestException(CoreResultCode.ROLE_REQUEST_NO_EXECUTE_IMMEDIATELY_RIGHT, ImmutableMap.of("new", request));
        }
        // All concepts in progress state will be set on approved (we can
        // execute it immediately)
        request.getConceptRoles().stream().filter(concept -> {
            return RoleRequestState.IN_PROGRESS == concept.getState();
        }).forEach(concept -> {
            concept.setState(RoleRequestState.APPROVED);
            conceptRoleRequestService.save(concept);
        });
        // Execute request immediately
        return true;
    } else {
        IdmIdentityDto applicant = identityService.get(request.getApplicant());
        Map<String, Object> variables = new HashMap<>();
        // Minimize size of DTO persisting to WF
        IdmRoleRequestDto eventRequest = event.getContent();
        trimRequest(eventRequest);
        eventRequest.setConceptRoles(null);
        eventRequest.setOriginalRequest(null);
        variables.put(EntityEvent.EVENT_PROPERTY, event);
        ProcessInstance processInstance = workflowProcessInstanceService.startProcess(wfDefinition, IdmIdentity.class.getSimpleName(), applicant.getUsername(), applicant.getId().toString(), variables);
        // We have to refresh request (maybe was changed in wf process)
        request = this.get(request.getId());
        request.setWfProcessId(processInstance.getProcessInstanceId());
        this.save(request);
    }
    return false;
}
Also used : IdmConceptRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmConceptRoleRequestService) DtoUtils(eu.bcvsolutions.idm.core.api.utils.DtoUtils) IdmIdentityRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityRoleDto) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) WorkflowProcessInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowProcessInstanceDto) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) SecurityService(eu.bcvsolutions.idm.core.security.api.service.SecurityService) IdmRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmRoleRequestService) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) Predicate(javax.persistence.criteria.Predicate) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) Loggable(eu.bcvsolutions.idm.core.api.domain.Loggable) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) IdmRoleRequest(eu.bcvsolutions.idm.core.model.entity.IdmRoleRequest) UUID(java.util.UUID) Serializable(java.io.Serializable) IdmRoleRequestRepository(eu.bcvsolutions.idm.core.model.repository.IdmRoleRequestRepository) List(java.util.List) IdmRoleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmRoleRequestFilter) Optional(java.util.Optional) IdmIdentityService(eu.bcvsolutions.idm.core.api.service.IdmIdentityService) RoleRequestEventType(eu.bcvsolutions.idm.core.model.event.RoleRequestEvent.RoleRequestEventType) RoleRequestApprovalProcessor(eu.bcvsolutions.idm.core.model.event.processor.role.RoleRequestApprovalProcessor) IdmIdentityRole_(eu.bcvsolutions.idm.core.model.entity.IdmIdentityRole_) IdmRoleRequest_(eu.bcvsolutions.idm.core.model.entity.IdmRoleRequest_) RoleRequestedByType(eu.bcvsolutions.idm.core.api.domain.RoleRequestedByType) HashMap(java.util.HashMap) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) IdmConceptRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmConceptRoleRequestDto) WorkflowProcessInstanceService(eu.bcvsolutions.idm.core.workflow.service.WorkflowProcessInstanceService) AbstractReadWriteDtoService(eu.bcvsolutions.idm.core.api.service.AbstractReadWriteDtoService) WorkflowFilterDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowFilterDto) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) EntityEvent(eu.bcvsolutions.idm.core.api.event.EntityEvent) Root(javax.persistence.criteria.Root) IdmIdentityRoleService(eu.bcvsolutions.idm.core.api.service.IdmIdentityRoleService) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) DateTime(org.joda.time.DateTime) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Throwables(com.google.common.base.Throwables) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) RoleRequestState(eu.bcvsolutions.idm.core.api.domain.RoleRequestState) ApplicationContext(org.springframework.context.ApplicationContext) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) CoreResultCode(eu.bcvsolutions.idm.core.api.domain.CoreResultCode) RoleRequestEvent(eu.bcvsolutions.idm.core.model.event.RoleRequestEvent) IdmIdentity_(eu.bcvsolutions.idm.core.model.entity.IdmIdentity_) ConceptRoleRequestOperation(eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) HashMap(java.util.HashMap) ProcessInstance(org.activiti.engine.runtime.ProcessInstance) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) IdmIdentity(eu.bcvsolutions.idm.core.model.entity.IdmIdentity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with RoleRequestException

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

the class IdmRoleRequestController method delete.

@Override
@ResponseBody
@RequestMapping(value = "/{backendId}", method = RequestMethod.DELETE)
@PreAuthorize("hasAuthority('" + CoreGroupPermission.ROLE_REQUEST_DELETE + "')")
@ApiOperation(value = "Delete role request", nickname = "deleteRoleRequest", tags = { IdmRoleRequestController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = CoreGroupPermission.ROLE_REQUEST_DELETE, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = CoreGroupPermission.ROLE_REQUEST_DELETE, description = "") }) })
public ResponseEntity<?> delete(@ApiParam(value = "Role request's uuid identifier.", required = true) @PathVariable @NotNull String backendId) {
    IdmRoleRequestService service = ((IdmRoleRequestService) this.getService());
    IdmRoleRequestDto dto = service.get(backendId);
    // 
    checkAccess(dto, IdmBasePermission.DELETE);
    // Request in Executed state can not be delete or change
    if (RoleRequestState.EXECUTED == dto.getState()) {
        throw new RoleRequestException(CoreResultCode.ROLE_REQUEST_EXECUTED_CANNOT_DELETE, ImmutableMap.of("request", dto));
    }
    // Only request in Concept state, can be deleted. In others states, will be request set to Canceled state and save.
    if (RoleRequestState.CONCEPT == dto.getState()) {
        service.delete(dto);
    } else {
        service.cancel(dto);
    }
    return new ResponseEntity<Object>(HttpStatus.NO_CONTENT);
}
Also used : RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) ResponseEntity(org.springframework.http.ResponseEntity) IdmRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmRoleRequestService) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with RoleRequestException

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

the class DefaultIdmAutomaticRoleRequestService method startRequest.

@Override
@Transactional
public IdmAutomaticRoleRequestDto startRequest(UUID requestId, boolean checkRight) {
    IdmAutomaticRoleRequestDto request = get(requestId);
    Assert.notNull(request, "Request is required!");
    // Validation on exist some rule
    if (AutomaticRoleRequestType.ATTRIBUTE == request.getRequestType() && RequestOperationType.REMOVE != request.getOperation()) {
        IdmAutomaticRoleAttributeRuleRequestFilter ruleFilter = new IdmAutomaticRoleAttributeRuleRequestFilter();
        ruleFilter.setRoleRequestId(requestId);
        List<IdmAutomaticRoleAttributeRuleRequestDto> ruleConcepts = automaticRoleRuleRequestService.find(ruleFilter, null).getContent();
        if (ruleConcepts.isEmpty()) {
            throw new RoleRequestException(CoreResultCode.AUTOMATIC_ROLE_REQUEST_START_WITHOUT_RULE, ImmutableMap.of("request", request.getName()));
        }
    }
    try {
        IdmAutomaticRoleRequestService service = this.getIdmAutomaticRoleRequestService();
        if (!(service instanceof DefaultIdmAutomaticRoleRequestService)) {
            throw new CoreException("We expects instace of DefaultIdmAutomaticRoleRequestService!");
        }
        return ((DefaultIdmAutomaticRoleRequestService) service).startRequestNewTransactional(requestId, checkRight);
    } catch (Exception ex) {
        LOG.error(ex.getLocalizedMessage(), ex);
        request = get(requestId);
        Throwable exceptionToLog = resolveException(ex);
        // TODO: I set only cause of exception, not code and properties. If are
        // properties set, then request cannot be save!
        request.setResult(new OperationResultDto.Builder(OperationState.EXCEPTION).setCause(exceptionToLog).build());
        request.setState(RequestState.EXCEPTION);
        return save(request);
    }
}
Also used : IdmAutomaticRoleAttributeRuleRequestFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmAutomaticRoleAttributeRuleRequestFilter) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) IdmAutomaticRoleAttributeRuleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleAttributeRuleRequestDto) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) IdmAutomaticRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IdmAutomaticRoleRequestService(eu.bcvsolutions.idm.core.api.service.IdmAutomaticRoleRequestService) RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) AcceptedException(eu.bcvsolutions.idm.core.api.exception.AcceptedException) CoreException(eu.bcvsolutions.idm.core.api.exception.CoreException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with RoleRequestException

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

the class DefaultIdmRoleRequestService method startRequestInternal.

@Override
@Transactional
public IdmRoleRequestDto startRequestInternal(UUID requestId, boolean checkRight) {
    LOG.debug("Start role request [{}]", requestId);
    Assert.notNull(requestId, "Role request ID is required!");
    // Load request ... check right for read
    IdmRoleRequestDto request = get(requestId);
    Assert.notNull(request, "Role request DTO is required!");
    Assert.isTrue(RoleRequestState.CONCEPT == request.getState() || RoleRequestState.DUPLICATED == request.getState() || RoleRequestState.EXCEPTION == request.getState(), "Only role request with CONCEPT or EXCEPTION or DUPLICATED state can be started!");
    IdmRoleRequestDto duplicant = validateOnDuplicity(request);
    if (duplicant != null) {
        request.setState(RoleRequestState.DUPLICATED);
        request.setDuplicatedToRequest(duplicant.getId());
        this.addToLog(request, MessageFormat.format("This request [{0}] is duplicated to another change permissions request [{1}]", request.getId(), duplicant.getId()));
        return this.save(request);
    }
    // Duplicant is fill, but request is not duplicated (maybe in past)
    if (request.getDuplicatedToRequest() != null) {
        request.setDuplicatedToRequest(null);
    }
    // Check on same applicants in all role concepts
    boolean identityNotSame = this.get(request.getId()).getConceptRoles().stream().anyMatch(concept -> {
        // get contract dto from embedded map
        IdmIdentityContractDto contract = (IdmIdentityContractDto) concept.getEmbedded().get(IdmConceptRoleRequestService.IDENTITY_CONTRACT_FIELD);
        if (contract == null) {
            // If is contract from concept null, then contract via identity role must works
            contract = (IdmIdentityContractDto) identityRoleService.get(concept.getIdentityRole()).getEmbedded().get(IdmConceptRoleRequestService.IDENTITY_CONTRACT_FIELD);
        }
        return !request.getApplicant().equals(contract.getIdentity());
    });
    if (identityNotSame) {
        throw new RoleRequestException(CoreResultCode.ROLE_REQUEST_APPLICANTS_NOT_SAME, ImmutableMap.of("request", request, "applicant", request.getApplicant()));
    }
    // Convert whole request to JSON and persist (without logs and embedded data)
    try {
        IdmRoleRequestDto requestOriginal = get(requestId);
        trimRequest(requestOriginal);
        request.setOriginalRequest(objectMapper.writeValueAsString(requestOriginal));
    } catch (JsonProcessingException e) {
        throw new RoleRequestException(CoreResultCode.BAD_REQUEST, e);
    }
    // Request will be set on in progress state
    request.setState(RoleRequestState.IN_PROGRESS);
    IdmRoleRequestDto savedRequest = this.save(request);
    // Throw event
    Map<String, Serializable> variables = new HashMap<>();
    variables.put(RoleRequestApprovalProcessor.CHECK_RIGHT_PROPERTY, checkRight);
    return entityEventManager.process(new RoleRequestEvent(RoleRequestEventType.EXCECUTE, savedRequest, variables)).getContent();
}
Also used : RoleRequestException(eu.bcvsolutions.idm.core.api.exception.RoleRequestException) Serializable(java.io.Serializable) HashMap(java.util.HashMap) RoleRequestEvent(eu.bcvsolutions.idm.core.model.event.RoleRequestEvent) IdmRoleRequestDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto) IdmIdentityContractDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

RoleRequestException (eu.bcvsolutions.idm.core.api.exception.RoleRequestException)7 Transactional (org.springframework.transaction.annotation.Transactional)5 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)4 HashMap (java.util.HashMap)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IdmAutomaticRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmAutomaticRoleRequestDto)3 IdmIdentityContractDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityContractDto)3 IdmRoleDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleDto)3 CoreException (eu.bcvsolutions.idm.core.api.exception.CoreException)3 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)3 IdmRoleRequestService (eu.bcvsolutions.idm.core.api.service.IdmRoleRequestService)3 RoleRequestEvent (eu.bcvsolutions.idm.core.model.event.RoleRequestEvent)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Strings (com.google.common.base.Strings)2 Throwables (com.google.common.base.Throwables)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ConceptRoleRequestOperation (eu.bcvsolutions.idm.core.api.domain.ConceptRoleRequestOperation)2 CoreResultCode (eu.bcvsolutions.idm.core.api.domain.CoreResultCode)2 Loggable (eu.bcvsolutions.idm.core.api.domain.Loggable)2 RoleRequestState (eu.bcvsolutions.idm.core.api.domain.RoleRequestState)2