Search in sources :

Example 16 with EntityNotFoundException

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

the class WorkflowTaskInstanceController method getPermissions.

@RequestMapping(method = RequestMethod.GET, value = "/{backendId}/permissions")
@ApiOperation(value = "Historic task instance detail", nickname = "getHistoricTaskInstance", response = WorkflowTaskInstanceDto.class, tags = { WorkflowTaskInstanceController.TAG })
@Override
public Set<String> getPermissions(@ApiParam(value = "Task instance id.", required = true) @PathVariable String backendId) {
    WorkflowFilterDto context = new WorkflowFilterDto();
    context.setOnlyInvolved(Boolean.FALSE);
    WorkflowTaskInstanceDto taskInstanceDto = workflowTaskInstanceService.get(backendId, context);
    if (taskInstanceDto == null) {
        throw new EntityNotFoundException(getService().getEntityClass(), backendId);
    }
    return workflowTaskInstanceService.getPermissions(taskInstanceDto);
}
Also used : WorkflowFilterDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowFilterDto) WorkflowTaskInstanceDto(eu.bcvsolutions.idm.core.workflow.model.dto.WorkflowTaskInstanceDto) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) ApiOperation(io.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 17 with EntityNotFoundException

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

the class VsRequestController method get.

@Override
@ResponseBody
@RequestMapping(value = "/{backendId}", method = RequestMethod.GET)
@PreAuthorize("hasAuthority('" + VirtualSystemGroupPermission.VS_REQUEST_READ + "')")
@ApiOperation(value = "Request detail", nickname = "getRequest", response = VsRequestDto.class, tags = { VsRequestController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = VirtualSystemGroupPermission.VS_REQUEST_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = VirtualSystemGroupPermission.VS_REQUEST_READ, description = "") }) })
public ResponseEntity<?> get(@ApiParam(value = "Request's uuid identifier.", required = true) @PathVariable @NotNull String backendId) {
    VsRequestDto request = this.getDto(backendId);
    if (request == null) {
        throw new EntityNotFoundException(getService().getEntityClass(), backendId);
    }
    UUID roleRequestId = request.getRoleRequestId();
    if (roleRequestId != null) {
        IdmRoleRequestDto roleRequestDto = rolerequestService.get(roleRequestId);
        if (roleRequestDto != null) {
            UUID roleRequestCreatorId = roleRequestDto.getCreatorId();
            if (roleRequestCreatorId != null) {
                IdmIdentityDto roleRequestCreator = identityService.get(roleRequestCreatorId);
                roleRequestDto.getEmbedded().put(Auditable.PROPERTY_CREATOR, roleRequestCreator);
            }
            request.getEmbedded().put(IdmConceptRoleRequestService.ROLE_REQUEST_FIELD, roleRequestDto);
        }
    }
    ResourceSupport resource = toResource(request);
    if (resource == null) {
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
    // 
    return new ResponseEntity<>(resource, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) VsRequestDto(eu.bcvsolutions.idm.vs.dto.VsRequestDto) UUID(java.util.UUID) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) ResourceSupport(org.springframework.hateoas.ResourceSupport) 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 18 with EntityNotFoundException

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

the class IdentityUsernameExportBulkActionTest method checkPermission.

@Test
public void checkPermission() {
    IdmBulkActionDto exampleAction = findBulkAction(IdmIdentity.class, IdentityUsernameExportBulkAction.BULK_ACTION_NAME);
    assertNotNull(exampleAction);
    IdmIdentityDto identity = getHelper().createIdentity();
    exampleAction.setIdentifiers(Sets.newHashSet(identity.getId()));
    IdmBulkActionDto processAction = bulkActionManager.processAction(exampleAction);
    assertNotNull(processAction.getLongRunningTaskId());
    IdmLongRunningTaskDto longRunningTask = longRunningTaskService.get(processAction.getLongRunningTaskId());
    IdmIdentityDto adminIdentity = this.createUserWithAuthorities(CoreGroupPermission.IDENTITY, CoreGroupPermission.SCHEDULER);
    loginAsNoAdmin(adminIdentity.getUsername());
    processAction = bulkActionManager.processAction(exampleAction);
    assertNotNull(processAction.getLongRunningTaskId());
    IdmLongRunningTaskDto longRunningTask2 = longRunningTaskService.get(processAction.getLongRunningTaskId());
    assertFalse(longRunningTask.isRunning());
    assertFalse(longRunningTask2.isRunning());
    Assert.notNull(longRunningTask, "Task is required.");
    Assert.notNull(longRunningTask2, "Task is required.");
    UUID attachmentOneId = attachmentManager.getAttachments(longRunningTask, null).getContent().get(0).getId();
    try {
        longRunningTaskManager.getAttachment(longRunningTask.getId(), attachmentOneId, IdmBasePermission.READ);
        fail();
    } catch (ForbiddenEntityException e) {
    // Correct behavior
    } catch (Exception e) {
        fail();
    }
    try {
        longRunningTaskManager.getAttachment(UUID.randomUUID(), attachmentOneId, IdmBasePermission.READ);
        fail();
    } catch (EntityNotFoundException e) {
    // Correct behavior
    } catch (Exception e) {
        fail();
    }
    UUID attachmentTwoId = attachmentManager.getAttachments(longRunningTask2, null).getContent().get(0).getId();
    IdmAttachmentDto attachmentDto = longRunningTaskManager.getAttachment(longRunningTask2.getId(), attachmentTwoId);
    assertNotNull(attachmentDto);
}
Also used : IdmAttachmentDto(eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto) IdmLongRunningTaskDto(eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto) IdmBulkActionDto(eu.bcvsolutions.idm.core.api.bulk.action.dto.IdmBulkActionDto) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) UUID(java.util.UUID) ForbiddenEntityException(eu.bcvsolutions.idm.core.api.exception.ForbiddenEntityException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException) ForbiddenEntityException(eu.bcvsolutions.idm.core.api.exception.ForbiddenEntityException) AbstractBulkActionTest(eu.bcvsolutions.idm.test.api.AbstractBulkActionTest) Test(org.junit.Test)

Example 19 with EntityNotFoundException

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

the class DefaultIdmIncompatibleRoleService method resolveIncompatibleRoles.

@Override
public Set<ResolvedIncompatibleRoleDto> resolveIncompatibleRoles(List<Serializable> rolesOrIdentifiers) {
    // search all defined incompatible roles for given roles - business roles can be given
    Set<ResolvedIncompatibleRoleDto> incompatibleRoles = new HashSet<>();
    if (CollectionUtils.isEmpty(rolesOrIdentifiers)) {
        return incompatibleRoles;
    }
    LOG.debug("Start resolving incompabible roles [{}]", rolesOrIdentifiers);
    // 
    Set<UUID> allRoleIds = new HashSet<>();
    Set<IdmRoleDto> roles = new HashSet<>();
    // search all sub roles
    for (Serializable roleOrIdentifier : rolesOrIdentifiers) {
        if (roleOrIdentifier == null) {
            continue;
        }
        roles.clear();
        // 
        IdmRoleDto directRole = null;
        if (roleOrIdentifier instanceof IdmRoleDto) {
            directRole = (IdmRoleDto) roleOrIdentifier;
        } else {
            directRole = (IdmRoleDto) lookupService.lookupDto(IdmRoleDto.class, roleOrIdentifier);
        }
        if (directRole == null) {
            throw new EntityNotFoundException(IdmRole.class, roleOrIdentifier);
        }
        // 
        roles.add(directRole);
        if (directRole.getChildrenCount() > 0) {
            roles.addAll(roleCompositionService.resolveDistinctRoles(roleCompositionService.findAllSubRoles(directRole.getId())));
        }
        // 
        // resolve incompatible roles
        List<UUID> roleIds = roles.stream().map(IdmRoleDto::getId).collect(Collectors.toList());
        // 
        for (IdmIncompatibleRoleDto incompatibleRole : findAllByRoles(roleIds)) {
            // find incompatible roles - we need to know, which from the given role is incompatible => ResolvedIncompatibleRoleDto
            incompatibleRoles.add(new ResolvedIncompatibleRoleDto(directRole, incompatibleRole));
        }
        allRoleIds.addAll(roleIds);
    }
    // 
    // both sides of incompatible roles should be in the allRoleIds and superior vs. sub role has to be different.
    Set<ResolvedIncompatibleRoleDto> resolvedRoles = incompatibleRoles.stream().filter(ir -> {
        // superior vs. sub role has to be different.
        return !ir.getIncompatibleRole().getSuperior().equals(ir.getIncompatibleRole().getSub());
    }).filter(ir -> {
        // superior and sub role has to be in all roles.
        return allRoleIds.contains(ir.getIncompatibleRole().getSuperior()) && allRoleIds.contains(ir.getIncompatibleRole().getSub());
    }).collect(Collectors.toSet());
    // 
    LOG.debug("Resolved incompabible roles [{}]", resolvedRoles.size());
    return resolvedRoles;
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) IdmIncompatibleRole(eu.bcvsolutions.idm.core.model.entity.IdmIncompatibleRole) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CoreGroupPermission(eu.bcvsolutions.idm.core.model.domain.CoreGroupPermission) LookupService(eu.bcvsolutions.idm.core.api.service.LookupService) Predicate(javax.persistence.criteria.Predicate) CollectionUtils(org.apache.commons.collections.CollectionUtils) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IdmIncompatibleRole_(eu.bcvsolutions.idm.core.model.entity.IdmIncompatibleRole_) IdmExportImportDto(eu.bcvsolutions.idm.core.api.dto.IdmExportImportDto) Root(javax.persistence.criteria.Root) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IdmRole_(eu.bcvsolutions.idm.core.model.entity.IdmRole_) AbstractEventableDtoService(eu.bcvsolutions.idm.core.api.service.AbstractEventableDtoService) Set(java.util.Set) PageRequest(org.springframework.data.domain.PageRequest) IdmRoleCompositionService(eu.bcvsolutions.idm.core.api.service.IdmRoleCompositionService) UUID(java.util.UUID) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) IdmIncompatibleRoleService(eu.bcvsolutions.idm.core.api.service.IdmIncompatibleRoleService) List(java.util.List) ExportDescriptorDto(eu.bcvsolutions.idm.core.api.dto.ExportDescriptorDto) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) ResolvedIncompatibleRoleDto(eu.bcvsolutions.idm.core.api.dto.ResolvedIncompatibleRoleDto) IdmIncompatibleRoleFilter(eu.bcvsolutions.idm.core.api.dto.filter.IdmIncompatibleRoleFilter) BaseDto(eu.bcvsolutions.idm.core.api.dto.BaseDto) IdmIncompatibleRoleRepository(eu.bcvsolutions.idm.core.model.repository.IdmIncompatibleRoleRepository) IdmIncompatibleRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIncompatibleRoleDto) PageImpl(org.springframework.data.domain.PageImpl) EntityEventManager(eu.bcvsolutions.idm.core.api.service.EntityEventManager) AuthorizableType(eu.bcvsolutions.idm.core.security.api.dto.AuthorizableType) Assert(org.springframework.util.Assert) IdmRole(eu.bcvsolutions.idm.core.model.entity.IdmRole) IdmRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmRoleDto) Serializable(java.io.Serializable) ResolvedIncompatibleRoleDto(eu.bcvsolutions.idm.core.api.dto.ResolvedIncompatibleRoleDto) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) UUID(java.util.UUID) IdmIncompatibleRoleDto(eu.bcvsolutions.idm.core.api.dto.IdmIncompatibleRoleDto) HashSet(java.util.HashSet)

Example 20 with EntityNotFoundException

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

the class DefaultTwoFactorAuthenticationManager method authenticate.

@Override
@Transactional
public LoginDto authenticate(LoginDto loginTwoFactorRequestDto) {
    Assert.notNull(loginTwoFactorRequestDto, "Login request is required.");
    // 
    IdmJwtAuthenticationDto claims = null;
    String loggedAction = siemLogger.buildAction(SiemLoggerManager.LOGIN_LEVEL_KEY);
    String targetName = loginTwoFactorRequestDto.getUsername();
    String targetUuid = null;
    try {
        Optional<Jwt> jwt = HttpFilterUtils.parseToken(loginTwoFactorRequestDto.getToken());
        if (!jwt.isPresent()) {
            throw new ResultCodeException(CoreResultCode.AUTH_FAILED, "Verification code must be filled");
        }
        HttpFilterUtils.verifyToken(jwt.get(), jwtAuthenticationMapper.getVerifier());
        // authentication dto from request
        try {
            claims = jwtAuthenticationMapper.getClaims(jwt.get());
            targetName = claims.getCurrentUsername();
            targetUuid = Objects.toString(claims.getCurrentIdentityId(), "");
        } catch (IOException ex) {
            throw new ResultCodeException(CoreResultCode.TOKEN_READ_FAILED, ex);
        }
        // we need to check expiration, before current (automatically prolonged) token is used by mapper
        if (claims.getExpiration() != null && claims.getExpiration().isBefore(ZonedDateTime.now())) {
            throw new ResultCodeException(CoreResultCode.AUTH_EXPIRED);
        }
        UUID identityId = claims.getCurrentIdentityId();
        IdmIdentityDto identity = identityService.get(identityId);
        if (identity == null) {
            throw new EntityNotFoundException(IdmIdentityDto.class, identityId);
        }
        IdmPasswordDto password = passwordService.findOneByIdentity(identityId);
        if (password == null) {
            throw new EntityNotFoundException(IdmPasswordDto.class, identityId);
        }
        if (!verifyCode(password, loginTwoFactorRequestDto.getPassword())) {
            throw new ResultCodeException(CoreResultCode.TWO_FACTOR_VERIFICATION_CODE_FAILED);
        }
        // 
        if (password.isMustChange() && !loginTwoFactorRequestDto.isSkipMustChange()) {
            throw new MustChangePasswordException(claims.getCurrentUsername());
        }
        // set token verified
        IdmTokenDto token = tokenManager.getToken(claims.getId());
        token.setSecretVerified(true);
        // and login - new login dto new to be constructed to preserve original login metadata
        LoginDto loginDto = new LoginDto();
        loginDto.setUsername(claims.getCurrentUsername());
        loginDto.setAuthenticationModule(claims.getFromModule());
        // 
        LoginDto resultLoginDto = jwtAuthenticationService.createJwtAuthenticationAndAuthenticate(loginDto, identity, token);
        siemLogger.log(loggedAction, SiemLoggerManager.SUCCESS_ACTION_STATUS, targetName, targetUuid, null, null, null, null);
        return resultLoginDto;
    } catch (Exception e) {
        siemLogger.log(loggedAction, SiemLoggerManager.FAILED_ACTION_STATUS, targetName, targetUuid, null, null, null, e.getMessage());
        throw e;
    }
}
Also used : IdmTokenDto(eu.bcvsolutions.idm.core.api.dto.IdmTokenDto) Jwt(org.springframework.security.jwt.Jwt) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IOException(java.io.IOException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) CodeGenerationException(dev.samstevens.totp.exceptions.CodeGenerationException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException) MustChangePasswordException(eu.bcvsolutions.idm.core.security.api.exception.MustChangePasswordException) MustChangePasswordException(eu.bcvsolutions.idm.core.security.api.exception.MustChangePasswordException) IdmPasswordDto(eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto) IdmJwtAuthenticationDto(eu.bcvsolutions.idm.core.security.api.dto.IdmJwtAuthenticationDto) UUID(java.util.UUID) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) LoginDto(eu.bcvsolutions.idm.core.security.api.dto.LoginDto) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)30 ApiOperation (io.swagger.annotations.ApiOperation)15 ResponseEntity (org.springframework.http.ResponseEntity)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)10 UUID (java.util.UUID)10 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)9 IdmLongRunningTaskDto (eu.bcvsolutions.idm.core.scheduler.api.dto.IdmLongRunningTaskDto)7 ResourceSupport (org.springframework.hateoas.ResourceSupport)7 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)6 Transactional (org.springframework.transaction.annotation.Transactional)5 IdmAttachmentDto (eu.bcvsolutions.idm.core.ecm.api.dto.IdmAttachmentDto)4 IOException (java.io.IOException)4 IdmPasswordDto (eu.bcvsolutions.idm.core.api.dto.IdmPasswordDto)3 IdmProfileDto (eu.bcvsolutions.idm.core.api.dto.IdmProfileDto)3 IdmRoleRequestDto (eu.bcvsolutions.idm.core.api.dto.IdmRoleRequestDto)3 IdmTokenDto (eu.bcvsolutions.idm.core.api.dto.IdmTokenDto)3 AcceptedException (eu.bcvsolutions.idm.core.api.exception.AcceptedException)3 IdmLongRunningTaskFilter (eu.bcvsolutions.idm.core.scheduler.api.dto.filter.IdmLongRunningTaskFilter)3