From a2bf9162c1f630f00bd89657c3373fd11639e1b4 Mon Sep 17 00:00:00 2001 From: Ray Lee Date: Fri, 5 Jun 2020 23:00:03 -0400 Subject: [PATCH] DRYD-861: Fix getAccountRole sometimes including null role. --- .../services/account/AccountResource.java | 2 +- .../nuxeo/BatchDocumentModelHandler.java | 78 ++++++------- .../common/storage/jpa/JpaStorageUtils.java | 22 ++-- .../nuxeo/ReportDocumentModelHandler.java | 109 +++++++++--------- 4 files changed, 105 insertions(+), 106 deletions(-) diff --git a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java index cd06e8bd4..970212295 100644 --- a/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java +++ b/services/account/service/src/main/java/org/collectionspace/services/account/AccountResource.java @@ -195,7 +195,7 @@ public class AccountResource extends SecurityResourceBase getAccountRoles(String userId, String tenantId) { + public List getAccountRoleDisplayNames(String userId, String tenantId) { List result = null; String accountCsid = getAccountCsid(userId); diff --git a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java index 096e4033b..20fb1a55e 100644 --- a/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java +++ b/services/batch/service/src/main/java/org/collectionspace/services/batch/nuxeo/BatchDocumentModelHandler.java @@ -67,17 +67,17 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler 0; } catch (NullPointerException e) { // ignore exception, we're just testing to see if we have any list elements } - + return result; } - + /** * Returns true if we found any required roles. - * + * * @param batchCommon * @return */ private boolean hasRequiredRoles(BatchCommon batchCommon) { boolean result = false; - + try { result = batchCommon.getForRoles().getRoleDisplayName().size() > 0; } catch (NullPointerException e) { // ignore exception, we're just testing to see if we have any list elements } - + return result; } @@ -137,29 +137,29 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler roleDisplayNameList = accountResource.getAccountRoles(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId()); + List roleDisplayNameList = accountResource.getAccountRoleDisplayNames(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId()); for (String target : forRolesList.getRoleDisplayName()) { if (Tools.listContainsIgnoreCase(roleDisplayNameList, target)) { result = true; @@ -167,10 +167,10 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler toLowerCase(List listOfStrings) { List result = null; - + if (listOfStrings != null) { Set stringSet = new HashSet(); for (String s : listOfStrings) { @@ -214,7 +214,7 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler(stringSet); } - + return result; } @@ -234,19 +234,19 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler forDocTypeList = toLowerCase(forDocTypes.getForDocType()); // convert all strings to lowercase. if (forDocTypeList == null || !forDocTypeList.contains(invocationCtx.getDocType().toLowerCase())) { - String msg = String.format("BatchResource: The batch job '%s' CSID='%s' does not support the invocation document type '%s'.", + String msg = String.format("BatchResource: The batch job '%s' CSID='%s' does not support the invocation document type '%s'.", batchCommon.getName(), csid, invocationCtx.getDocType()); throw new BadRequestException(msg); } @@ -265,7 +265,7 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler c = tccl.loadClass(className); @@ -273,7 +273,7 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler modes = batchInstance.getSupportedInvocationModes(); if (!modes.contains(invocationCtx.getMode().toLowerCase())) { @@ -281,10 +281,10 @@ public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler roleValues = new ArrayList(); - while (resultList.hasNext()) { - AccountRoleRel accountRolRel = (AccountRoleRel)resultList.next(); - roleValues.add(AuthorizationRoleRel.buildRoleValue(accountRolRel)); + List roleValues = new ArrayList(); + + while (resultList.hasNext()) { + AccountRoleRel accountRolRel = (AccountRoleRel) resultList.next(); + RoleValue roleValue = AuthorizationRoleRel.buildRoleValue(accountRolRel); + + if (roleValue != null) { + roleValues.add(roleValue); } + } + + if (roleValues.size() > 0) { result.setRole(roleValues); } } catch (NoResultException nre) { diff --git a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java index 9b43a3662..64923f024 100644 --- a/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java +++ b/services/report/service/src/main/java/org/collectionspace/services/report/nuxeo/ReportDocumentModelHandler.java @@ -114,12 +114,12 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler resultMIMETypeItemList = resultMIMEType.getMIMEType(); - + // // Read the MIME type values from the service bindings and put into our response payload // @@ -142,7 +142,7 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler bindingsMIMETypeList = ServiceBindingUtils.getPropertyValueList(reportServiceBinding, ServiceBindingUtils.OUTPUT_MIME_PROP); - + if (bindingsMIMETypeList != null) { for (PropertyItemType bindingItemMimeType : bindingsMIMETypeList) { MIMETypeItemType resultMimeTypeItem = new MIMETypeItemType(); @@ -159,7 +159,7 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler params) { String outputMIME = invContext.getOutputMIME(); String mode = invContext.getMode(); @@ -190,10 +190,10 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler params = new HashMap(); params.put(REPORTS_STD_TENANTID_PARAM, ctx.getTenantId()); boolean checkDocType = true; - + // Note we set before we put in the default ones, so they cannot override tenant or CSID. setParamsFromContext(params, invContext); - + if (Invocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) { modeProperty = InvocableJAXBSchema.SUPPORTS_SINGLE_DOC; params.put(REPORTS_STD_CSID_PARAM, invContext.getSingleCSID()); @@ -241,11 +241,11 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler forDocTypeList = + List forDocTypeList = (List) NuxeoUtils.getProperyValue(docModel, InvocableJAXBSchema.FOR_DOC_TYPES); //docModel.getPropertyValue(InvocableJAXBSchema.FOR_DOC_TYPES); if (forDocTypeList==null || !forDocTypeList.contains(invContext.getDocType())) { throw new BadRequestException( @@ -312,10 +312,10 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler params, InvocationContext invContext) { InvocationContext.Params icParams = invContext.getParams(); if(icParams!= null) { @@ -330,31 +330,31 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler params, String reportFileName, String outputMimeType, StringBuffer outReportFileName) throws Exception { Connection conn = null; InputStream result = null; - + try { String fileNameBase = Tools.getFilenameBase(reportFileName); String compiledReportFilename = fileNameBase+ReportClient.COMPILED_REPORT_EXTENSION; String reportDescriptionFilename = fileNameBase+ReportClient.REPORT_DECSRIPTION_EXTENSION; - + String basePath = ServiceMain.getInstance().getServerRootDir() + - File.separator + JEEServerDeployment.CSPACE_DIR_NAME + + File.separator + JEEServerDeployment.CSPACE_DIR_NAME + File.separator + REPORTS_FOLDER + // File.separator + tenantName + File.separator; // + reportFileName; - - String compiledFilePath = basePath+compiledReportFilename; + + String compiledFilePath = basePath+compiledReportFilename; File f = new File(compiledFilePath); if(!f.exists()) { // Need to compile the file // First verify that there is a source file. - String sourceFilePath = basePath+reportDescriptionFilename; + String sourceFilePath = basePath+reportDescriptionFilename; File f2 = new File(sourceFilePath); if(!f2.exists()) { // Missing source file - error! logger.error("Report for csid={} is missing the specified source file: {}", @@ -364,28 +364,28 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler0) outputFilename = outputFilename.substring(0, idx); // Strip any sub-dir from report filename. - idx = outputFilename.lastIndexOf(File.separator); + idx = outputFilename.lastIndexOf(File.separator); if(idx>0) outputFilename = outputFilename.substring(idx+1); if(outputMimeType.equals(MediaType.APPLICATION_XML)) { @@ -433,15 +433,15 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler ctx = this.getServiceContext(); try { String repositoryName = ctx.getRepositoryName(); @@ -513,7 +513,7 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler 0; } catch (NullPointerException e) { // ignore exception, we're just testing to see if we have any list elements } - + return result; } - + /** * Returns true if we found any required roles. - * + * * @param reportCommon * @return */ private boolean hasRequiredRoles(ReportsCommon reportCommon) { boolean result = false; - + try { result = reportCommon.getForRoles().getRoleDisplayName().size() > 0; } catch (NullPointerException e) { // ignore exception, we're just testing to see if we have any list elements } - + return result; } - + /** * The current user is authorized to run the report if: * 1. No permissions or roles are specified in the report * 2. No roles are specified, but permissions are specified and the current user has those permissions * 3. Roles are specified and the current user is a member of at least one of the roles. - * + * * @param reportsCommon * @return */ protected boolean isAuthoritzed(ReportsCommon reportsCommon) { boolean result = true; - - if (hasRequiredRoles(reportsCommon)) { + + if (hasRequiredRoles(reportsCommon)) { result = isAuthorizedWithRoles(reportsCommon); } else if (hasRequiredPermissions(reportsCommon)) { result = isAuthoritzedWithPermissions(reportsCommon); } - + return result; } - + protected boolean isAuthorizedWithRoles(ReportsCommon reportCommon) { boolean result = false; - + ForRoles forRolesList = reportCommon.getForRoles(); if (forRolesList != null) { AccountResource accountResource = new AccountResource(); - List roleDisplayNameList = accountResource.getAccountRoles(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId()); + List roleDisplayNameList = accountResource.getAccountRoleDisplayNames(AuthN.get().getUserId(), AuthN.get().getCurrentTenantId()); for (String target : forRolesList.getRoleDisplayName()) { if (Tools.listContainsIgnoreCase(roleDisplayNameList, target)) { result = true; @@ -615,9 +615,8 @@ public class ReportDocumentModelHandler extends NuxeoDocumentModelHandler