private void validateRequiredParametersInConsent(ReceiptInput receiptInput) throws ConsentManagementClientException { if (isBlank(receiptInput.getPiiPrincipalId())) { throw handleClientException(ERROR_CODE_PII_PRINCIPAL_ID_REQUIRED, null); } if (isBlank(receiptInput.getCollectionMethod())) { throw handleClientException(ERROR_CODE_PII_COLLECTION_METHOD_REQUIRED, null); } if (isEmpty(receiptInput.getServices())) { throw handleClientException(ERROR_CODE_AT_LEAST_ONE_SERVICE_REQUIRED, null); } }
protected void addReceiptInfo(ReceiptInput receiptInput) throws ConsentManagementServerException { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); try { jdbcTemplate.withTransaction(template -> { template.executeInsert(INSERT_RECEIPT_SQL, (preparedStatement -> { preparedStatement.setString(1, receiptInput.getConsentReceiptId()); preparedStatement.setString(2, receiptInput.getVersion()); preparedStatement.setString(3, receiptInput.getJurisdiction()); preparedStatement.setTimestamp(4, new java.sql.Timestamp(new Date().getTime()), Calendar.getInstance(TimeZone.getTimeZone(UTC))); preparedStatement.setString(5, receiptInput.getCollectionMethod()); preparedStatement.setString(6, receiptInput.getLanguage()); preparedStatement.setString(7, receiptInput.getPiiPrincipalId()); preparedStatement.setInt(8, receiptInput.getTenantId()); preparedStatement.setString(9, receiptInput.getPolicyUrl()); preparedStatement.setString(10, ACTIVE_STATE); preparedStatement.setString(11, receiptInput.getPiiControllerInfo()); }), receiptInput, false); return null; }); } catch (TransactionException e) { throw ConsentUtils.handleServerException(ErrorMessages.ERROR_CODE_ADD_RECEIPT, receiptInput.getPiiPrincipalId(), e); } }
private void revokeActiveReceipts(ReceiptInput receiptInput) throws ConsentManagementServerException { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); try { jdbcTemplate.withTransaction(template -> { receiptInput.getServices().forEach(rethrowConsumer(receiptServiceInput -> { List<String> ids = template.executeQuery(GET_ACTIVE_RECEIPTS_SQL, (resultSet, rowNumber) -> resultSet .getString(1), preparedStatement -> { preparedStatement.setString(1, receiptInput.getPiiPrincipalId()); preparedStatement.setString(2, receiptServiceInput.getService()); preparedStatement.setInt(3, receiptInput.getTenantId()); preparedStatement.setInt(4, receiptServiceInput.getTenantId()); }); if (isNotEmpty(ids)) { ids.forEach(rethrowConsumer(id -> { revokeReceipt(id); if (log.isDebugEnabled()) { log.debug("Revoked active receipt: " + id + " of the user: " + receiptInput .getPiiPrincipalId()); } })); } })); return null; }); } catch (TransactionException e) { throw ConsentUtils.handleServerException(ErrorMessages.ERROR_CODE_REVOKE_ACTIVE_RECEIPT, receiptInput.getPiiPrincipalId(), e); } }
private void validateInputParameters(ReceiptInput receiptInput) throws ConsentManagementException { //Set authenticated user. if (isBlank(receiptInput.getPiiPrincipalId())) { receiptInput.setPiiPrincipalId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); } // Set authenticated user's tenant id if it is not set. if (isBlank(receiptInput.getTenantDomain())) { receiptInput.setTenantId(getTenantIdFromCarbonContext()); receiptInput.setTenantDomain(getTenantDomainFromCarbonContext()); } else { receiptInput.setTenantId(getTenantId(realmService, receiptInput.getTenantDomain())); } validateRequiredParametersInConsent(receiptInput); receiptInput.getServices().forEach(rethrowConsumer(receiptServiceInput -> { validateRequiredParametersInService(receiptServiceInput); receiptServiceInput.getPurposes().forEach(rethrowConsumer(receiptPurposeInput -> validateRequiredParametersInPurpose(receiptServiceInput, receiptPurposeInput))); })); if (log.isDebugEnabled()) { log.debug("Consent adding request validation success"); } }
/** * This API is used to verify and store consent input. * * @param receiptInput consent input. * @throws ConsentManagementException Consent Management Exception. */ public AddReceiptResponse addConsent(ReceiptInput receiptInput) throws ConsentManagementException { validateInputParameters(receiptInput); receiptInput.setConsentReceiptId(generateConsentReceiptId()); setAPIVersion(receiptInput); setPIIControllerInfo(receiptInput); getReceiptsDAO(receiptDAOs).addReceipt(receiptInput); if (log.isDebugEnabled()) { log.debug("Consent stored successfully with the Id: " + receiptInput.getConsentReceiptId()); } return new AddReceiptResponse(receiptInput.getConsentReceiptId(), receiptInput.getCollectionMethod(), receiptInput.getLanguage(), receiptInput.getPiiPrincipalId(), receiptInput.getTenantDomain()); }
@Override public void addReceipt(ReceiptInput receiptInput) throws ConsentManagementException { JdbcTemplate jdbcTemplate = JdbcUtils.getNewTemplate(); try { jdbcTemplate.withTransaction(template -> { revokeActiveReceipts(receiptInput); addReceiptInfo(receiptInput); receiptInput.getServices().forEach(rethrowConsumer(receiptServiceInput -> { int receiptToSPAssocId = addReceiptSPAssociation(receiptInput.getConsentReceiptId(), receiptServiceInput); receiptServiceInput.getPurposes().forEach(rethrowConsumer(receiptPurposeInput -> { int spToPurposeAssocId = addSpToPurposeAssociation(receiptToSPAssocId, receiptPurposeInput); receiptPurposeInput.getPurposeCategoryId().forEach(rethrowConsumer(id -> addSpPurposeToPurposeCategoryAssociation(spToPurposeAssocId, id))); receiptPurposeInput.getPiiCategory().forEach(rethrowConsumer(piiCategoryValidity -> addSpPurposeToPiiCategoryAssociation(spToPurposeAssocId, piiCategoryValidity.getId(), piiCategoryValidity.getValidity()))); })); })); if (receiptInput.getProperties() != null) { addReceiptProperties(receiptInput.getConsentReceiptId(), receiptInput.getProperties()); } return null; }); } catch (TransactionException e) { throw ConsentUtils.handleServerException(ErrorMessages.ERROR_CODE_ADD_CONSENT_RECEIPT, receiptInput.getPiiPrincipalId(), e); } }