From 1d49014466aa72538710a11d02ef9d64c0e8fd87 Mon Sep 17 00:00:00 2001 From: Ian Date: Tue, 4 Jun 2024 13:48:30 -0400 Subject: [PATCH] RESTWS-941: Any authenticated user should be able to read available modules --- .../resource/openmrs1_8/ModuleActionResource1_8.java | 8 ++------ .../v1_0/resource/openmrs1_8/ModuleResource1_8.java | 10 +++++----- .../webservices/helper/ModuleFactoryWrapper.java | 6 ++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleActionResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleActionResource1_8.java index 0eadbfe2b..d67ab5ff4 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleActionResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleActionResource1_8.java @@ -88,7 +88,7 @@ public Object create(SimpleObject post, RequestContext context) throws ResponseE throw new IllegalRequestException("Cannot execute action " + action.getAction() + " on empty set of modules."); } else { if (action.getAction() == Action.INSTALL) { - if (installUri == null || !ResourceUtils.isUrl(installUri)) { + if (!ResourceUtils.isUrl(installUri)) { throw new IllegalRequestException("The installUri needs to be a URL for this action to be performed"); } } @@ -171,11 +171,7 @@ private Module installModule(Collection modules, String installUri, Serv } catch (MalformedURLException e) { throw new APIException(e.getMessage(), e); - } - catch (IOException e) { - throw new APIException(e.getMessage(), e); - } - finally { + } finally { if (tempModule == null && moduleFile != null) { FileUtils.deleteQuietly(moduleFile); } diff --git a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleResource1_8.java b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleResource1_8.java index 6336d907a..e4d5ecc25 100644 --- a/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleResource1_8.java +++ b/omod-1.8/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/resource/openmrs1_8/ModuleResource1_8.java @@ -46,7 +46,7 @@ public class ModuleResource1_8 extends BaseDelegatingReadableResource im private ModuleFactoryWrapper moduleFactoryWrapper = new ModuleFactoryWrapper(); - private String moduleActionLink = ModuleActionResource1_8.class.getAnnotation(Resource.class).name(); + private final String moduleActionLink = ModuleActionResource1_8.class.getAnnotation(Resource.class).name(); public void setModuleFactoryWrapper(ModuleFactoryWrapper moduleFactoryWrapper) { this.moduleFactoryWrapper = moduleFactoryWrapper; @@ -54,7 +54,7 @@ public void setModuleFactoryWrapper(ModuleFactoryWrapper moduleFactoryWrapper) { @Override public Module getByUniqueId(String uniqueId) { - moduleFactoryWrapper.checkPrivilege(); + moduleFactoryWrapper.checkReadonlyPrivilege(); return moduleFactoryWrapper.getModuleById(uniqueId); } @@ -136,11 +136,11 @@ public Model getGETModel(Representation rep) { } /** - * @see org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource#doGetAll(org.openmrs.module.webservices.rest.web.RequestContext) + * @see org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingReadableResource#doGetAll(RequestContext) */ @Override public NeedsPaging doGetAll(RequestContext context) throws ResponseException { - moduleFactoryWrapper.checkPrivilege(); + moduleFactoryWrapper.checkReadonlyPrivilege(); return new NeedsPaging(new ArrayList(moduleFactoryWrapper.getLoadedModules()), context); } @@ -185,7 +185,7 @@ public Object upload(MultipartFile file, RequestContext context) throws Response module = moduleFactoryWrapper.loadModule(moduleFile); moduleFactoryWrapper.startModule(module, servletContext); - if (existingModule != null && dependentModulesStopped.size() > 0 + if (existingModule != null && !dependentModulesStopped.isEmpty() && moduleFactoryWrapper.isModuleStarted(module)) { startModules(dependentModulesStopped, existingModule, servletContext); } diff --git a/omod-common/src/main/java/org/openmrs/module/webservices/helper/ModuleFactoryWrapper.java b/omod-common/src/main/java/org/openmrs/module/webservices/helper/ModuleFactoryWrapper.java index 2dbdd22cd..9180b1722 100644 --- a/omod-common/src/main/java/org/openmrs/module/webservices/helper/ModuleFactoryWrapper.java +++ b/omod-common/src/main/java/org/openmrs/module/webservices/helper/ModuleFactoryWrapper.java @@ -161,4 +161,10 @@ public void checkPrivilege() throws APIAuthenticationException { throw new APIAuthenticationException("Privilege required: " + PrivilegeConstants.MANAGE_MODULES); } } + + public void checkReadonlyPrivilege() throws APIAuthenticationException { + if (!Context.isAuthenticated()) { + throw new APIAuthenticationException("User must be authenticated"); + } + } }