Search in sources :

Example 96 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project hub-alert by blackducksoftware.

the class AbstractUploadAction method deleteFile.

public ActionResponse<Void> deleteFile() {
    if (isTargetUndefined()) {
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, META_DATA_MISSING);
    }
    if (!authorizationManager.hasUploadDeletePermission(target.getContext(), target.getDescriptorKey())) {
        return new ActionResponse<>(HttpStatus.FORBIDDEN, ActionResponse.FORBIDDEN_MESSAGE);
    }
    try {
        String targetFilename = target.getFilename();
        File fileToValidate = filePersistenceUtil.createUploadsFile(targetFilename);
        filePersistenceUtil.delete(fileToValidate);
    } catch (IOException ex) {
        logger.error("Error deleting file - file: {}, context: {}, descriptor: {} ", target.getFilename(), target.getContext(), target.getDescriptorKey().getUniversalKey());
        logger.error("Error deleting file caused by: ", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, "Error deleting uploaded file from server.");
    }
    return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
Also used : IOException(java.io.IOException) File(java.io.File) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 97 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project hub-alert by blackducksoftware.

the class AuthenticationActions method authenticateUser.

public ActionResponse<Void> authenticateUser(HttpServletRequest servletRequest, HttpServletResponse servletResponse, LoginConfig loginConfig) throws BadCredentialsException {
    ActionResponse<Void> response = new ActionResponse<>(HttpStatus.UNAUTHORIZED);
    try {
        Authentication pendingAuthentication = createUsernamePasswordAuthToken(loginConfig);
        Authentication authentication = authenticationProvider.authenticate(pendingAuthentication);
        boolean authenticated = authentication.isAuthenticated() && !authentication.getAuthorities().isEmpty();
        if (authenticated) {
            CsrfToken token = csrfTokenRepository.generateToken(servletRequest);
            csrfTokenRepository.saveToken(token, servletRequest, servletResponse);
            servletResponse.setHeader(token.getHeaderName(), token.getToken());
            response = new ActionResponse<>(HttpStatus.NO_CONTENT);
        } else {
            servletRequest.getSession().invalidate();
        }
    } catch (AuthenticationException ex) {
        logger.error("Error Authenticating user.", ex);
    }
    return response;
}
Also used : AuthenticationException(org.springframework.security.core.AuthenticationException) Authentication(org.springframework.security.core.Authentication) CsrfToken(org.springframework.security.web.csrf.CsrfToken) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 98 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project hub-alert by blackducksoftware.

the class CertificateActions method updateWithoutChecks.

@Override
protected ActionResponse<CertificateModel> updateWithoutChecks(Long id, CertificateModel resource) {
    try {
        Optional<CustomCertificateModel> existingCertificate = certificateAccessor.getCertificate(id);
        String logableId = escapeUtil.replaceWithUnderscore(resource.getId());
        String loggableAlias = escapeUtil.replaceWithUnderscore(resource.getAlias());
        logger.info("Updating certificate with id: {} and alias: {}", logableId, loggableAlias);
        if (existingCertificate.isPresent()) {
            CertificateModel certificateModel = importCertificate(resource);
            return new ActionResponse<>(HttpStatus.NO_CONTENT, certificateModel);
        }
        logger.error("Certificate with id: {} missing.", logableId);
        return new ActionResponse<>(HttpStatus.NOT_FOUND, "Certificate not found.");
    } catch (AlertException ex) {
        logger.error("Error occurred updating certificate", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
    }
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 99 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project hub-alert by blackducksoftware.

the class CertificateActions method deleteWithoutChecks.

@Override
protected ActionResponse<CertificateModel> deleteWithoutChecks(Long id) {
    try {
        Optional<CustomCertificateModel> certificate = certificateAccessor.getCertificate(id);
        if (certificate.isPresent()) {
            CustomCertificateModel certificateModel = certificate.get();
            logger.info("Delete certificate with id: {} and alias: {}", certificateModel.getNullableId(), certificateModel.getAlias());
            trustStoreService.removeCertificate(certificateModel);
            certificateAccessor.deleteCertificate(id);
        }
    } catch (AlertException ex) {
        logger.error("Error deleting certificate", ex);
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, String.format("Error deleting certificate: %s", ex.getMessage()));
    }
    return new ActionResponse<>(HttpStatus.NO_CONTENT);
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse)

Example 100 with ActionResponse

use of com.synopsys.integration.alert.common.action.ActionResponse in project hub-alert by blackducksoftware.

the class RoleActions method updateWithoutChecks.

@Override
protected ActionResponse<RolePermissionModel> updateWithoutChecks(Long id, RolePermissionModel resource) {
    try {
        String roleName = resource.getRoleName();
        Optional<UserRoleModel> existingRole = roleAccessor.getRoles(List.of(id)).stream().findFirst();
        if (existingRole.isPresent()) {
            logger.debug(actionMessageCreator.updateStartMessage("role", existingRole.get().getName()));
            if (!existingRole.get().getName().equals(roleName)) {
                authorizationManager.updateRoleName(id, roleName);
            }
            Set<PermissionModel> permissions = resource.getPermissions();
            PermissionMatrixModel permissionMatrixModel = PermissionModelUtil.convertToPermissionMatrixModel(permissions);
            authorizationManager.updatePermissionsForRole(roleName, permissionMatrixModel);
            logger.debug(actionMessageCreator.updateSuccessMessage("Role", roleName));
            return new ActionResponse<>(HttpStatus.NO_CONTENT);
        }
        logger.warn(actionMessageCreator.updateNotFoundMessage("Role", id));
        return new ActionResponse<>(HttpStatus.NOT_FOUND, "Role not found.");
    } catch (AlertException ex) {
        logger.error(actionMessageCreator.updateErrorMessage("role", resource.getRoleName()));
        return new ActionResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
    }
}
Also used : PermissionMatrixModel(com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel) UserRoleModel(com.synopsys.integration.alert.common.persistence.model.UserRoleModel) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Aggregations

ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)132 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)46 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)40 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)38 PermissionMatrixModel (com.synopsys.integration.alert.common.persistence.model.PermissionMatrixModel)34 PermissionKey (com.synopsys.integration.alert.common.persistence.model.PermissionKey)32 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)30 ChannelKey (com.synopsys.integration.alert.descriptor.api.model.ChannelKey)30 AuthenticationTestUtils (com.synopsys.integration.alert.test.common.AuthenticationTestUtils)30 Test (org.junit.jupiter.api.Test)30 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)24 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)16 JobFieldModel (com.synopsys.integration.alert.common.rest.model.JobFieldModel)16 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)12 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)10 Collection (java.util.Collection)10 Optional (java.util.Optional)10 Set (java.util.Set)10 Collectors (java.util.stream.Collectors)10 Autowired (org.springframework.beans.factory.annotation.Autowired)10