Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TRUNK-6203: Global properties access should be privileged #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions api/src/main/java/org/openmrs/ui/framework/FormatterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.openmrs.ui.framework.formatter.FormatterService;
import org.springframework.context.MessageSource;

import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;
import static org.openmrs.util.TimeZoneUtil.toTimezone;

/**
Expand Down Expand Up @@ -117,18 +118,25 @@ private boolean wholeNumber(Number n) {

private String format(Date d, Locale locale) {
DateFormat df;
boolean convertTimezones = BooleanUtils
.toBoolean(administrationService.getGlobalProperty(UiFrameworkConstants.GP_TIMEZONE_CONVERSIONS));
if (convertTimezones) {
String clientTimezone = getAuthenticatedUser()
.getUserProperty(administrationService.getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE));
return (toTimezone(d, administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT),
clientTimezone));
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
boolean convertTimezones = BooleanUtils
.toBoolean(administrationService.getGlobalProperty(UiFrameworkConstants.GP_TIMEZONE_CONVERSIONS));
if (convertTimezones) {
String clientTimezone = getAuthenticatedUser()
.getUserProperty(administrationService.getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE));
return (toTimezone(d,
administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT),
clientTimezone));
}
if (hasTimeComponent(d)) {
df = UiFrameworkUtil.getDateTimeFormat(administrationService, locale);
} else {
df = UiFrameworkUtil.getDateFormat(administrationService, locale);
}
}
if (hasTimeComponent(d)) {
df = UiFrameworkUtil.getDateTimeFormat(administrationService, locale);
} else {
df = UiFrameworkUtil.getDateFormat(administrationService, locale);
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return df.format(d);
}
Expand Down Expand Up @@ -276,12 +284,19 @@ private String format(PersonAddress personAddress, Locale locale) {
Object templates = MethodUtils.invokeExactMethod(addressSupport, "getAddressTemplate", null);
addressTemplate = ((List<?>) templates).get(0);
} else {
String templateName = administrationService.getGlobalProperty(ADDRESS_LAYOUT_TEMPLATE_NAME_GP);
if (templateName != null) {
addressTemplate = MethodUtils.invokeExactMethod(addressSupport, "getLayoutTemplateByName", templateName);
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
String templateName = administrationService.getGlobalProperty(ADDRESS_LAYOUT_TEMPLATE_NAME_GP);
if (templateName != null) {
addressTemplate = MethodUtils.invokeExactMethod(addressSupport, "getLayoutTemplateByName",
templateName);
}
if (addressTemplate == null) {
addressTemplate = MethodUtils.invokeExactMethod(addressSupport, "getDefaultLayoutTemplate", null);
}
}
if (addressTemplate == null) {
addressTemplate = MethodUtils.invokeExactMethod(addressSupport, "getDefaultLayoutTemplate", null);
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ public class UiFrameworkConstants {
//The name of the user property that save the client timezone.
public static final String UP_CLIENT_TIMEZONE = "uiframework.client.timezone";

public static final String GET_GLOBAL_PROPERTIES = "Get Global Properties";

}
40 changes: 32 additions & 8 deletions api/src/main/java/org/openmrs/ui/framework/UiFrameworkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;

public class UiFrameworkUtil {

private static Log log = LogFactory.getLog(UiFrameworkUtil.class);
Expand Down Expand Up @@ -667,9 +669,16 @@ private static String getRuntimeOrSystemProperty(String key) {
public static DateFormat getDateFormat(AdministrationService administrationService, Locale locale) {
String defaultFormat = "dd.MMM.yyyy";
if (administrationService != null) {
return new SimpleDateFormat(
administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATE_FORMAT, defaultFormat),
locale);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATE_FORMAT,
defaultFormat);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return new SimpleDateFormat(globalProperty, locale);
} else {
return new SimpleDateFormat(defaultFormat, locale);
}
Expand All @@ -678,8 +687,16 @@ public static DateFormat getDateFormat(AdministrationService administrationServi
public static DateFormat getDateTimeFormat(AdministrationService administrationService, Locale locale) {
String defaultFormat = "dd.MMM.yyyy, HH:mm:ss";
if (administrationService != null) {
return new SimpleDateFormat(administrationService
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT, defaultFormat), locale);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT,
defaultFormat);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return new SimpleDateFormat(globalProperty, locale);
} else {
return new SimpleDateFormat(defaultFormat, locale);
}
Expand All @@ -688,9 +705,16 @@ public static DateFormat getDateTimeFormat(AdministrationService administrationS
public static DateFormat getTimeFormat(AdministrationService administrationService, Locale locale) {
String defaultFormat = "hh:mm a";
if (administrationService != null) {
return new SimpleDateFormat(
administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_TIME_FORMAT, defaultFormat),
locale);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = administrationService.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_TIME_FORMAT,
defaultFormat);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return new SimpleDateFormat(globalProperty, locale);
} else {
return new SimpleDateFormat(defaultFormat, locale);
}
Expand Down
81 changes: 72 additions & 9 deletions api/src/main/java/org/openmrs/ui/framework/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Random;

import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;
import static org.openmrs.util.TimeZoneUtil.toTimezone;

/**
Expand Down Expand Up @@ -643,36 +644,96 @@ public void setLocale(Locale locale) {
* @return the value of the Global Property GP_TIMEZONE_CONVERSIONS
*/
public boolean convertTimezones() {
return BooleanUtils.toBoolean(
Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_TIMEZONE_CONVERSIONS));
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_TIMEZONE_CONVERSIONS);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return BooleanUtils.toBoolean(globalProperty);
}

public String getJSDatetimeFormat() {
return Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_JS_DATETIME_FORMAT);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_JS_DATETIME_FORMAT);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalProperty;
}

public String getJSDateFormat() {
return Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_JS_DATE_FORMAT);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_JS_DATE_FORMAT);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalProperty;
}

public String getDatetimeFormat() {
return Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATETIME_FORMAT);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalProperty;
}

public String getDateFormat() {
return Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATE_FORMAT);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_DATE_FORMAT);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalProperty;
}

public String getTimeFormat() {
return Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_TIME_FORMAT);
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService()
.getGlobalProperty(UiFrameworkConstants.GP_FORMATTER_TIME_FORMAT);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalProperty;
}

/**
* @return the value of the User Property clientTimezone, that indicates the client timezone
*/
public String getClientTimezone() {
return Context.getAuthenticatedUser().getUserProperty(
Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE));
String globalProperty;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalProperty = Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return Context.getAuthenticatedUser().getUserProperty(globalProperty);
}

/**
Expand All @@ -683,12 +744,14 @@ public String getClientTimezone() {
public void setClientTimezone(String clientTimezone) {
try {
Context.addProxyPrivilege(PrivilegeConstants.EDIT_USERS);
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
Context.getUserService().setUserProperty(Context.getAuthenticatedUser(),
Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE),
clientTimezone);
}
finally {
Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USERS);
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.openmrs.api.context.Context;
import org.springframework.core.convert.converter.Converter;

import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;

/**
* Note that this converts based on property name, not on id
*/
Expand All @@ -15,6 +17,14 @@ public GlobalProperty convert(String propertyName) {
if (StringUtils.isBlank(propertyName)) {
return null;
}
return Context.getAdministrationService().getGlobalPropertyObject(propertyName);
GlobalProperty globalPropertyObject;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
globalPropertyObject = Context.getAdministrationService().getGlobalPropertyObject(propertyName);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return globalPropertyObject;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;

/**
* Manager for extension points
*/
Expand Down Expand Up @@ -112,7 +114,14 @@ public <T extends Extension> Map<String, T> getExtensionsByClass(Class<T> clazz)
* the point has not been configured, this returns null.
*/
public List<String> getExtensionPointConfiguration(String pointId) {
String gp = Context.getAdministrationService().getGlobalProperty("ui2.extensionConfig." + pointId);
String gp;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
gp = Context.getAdministrationService().getGlobalProperty("ui2.extensionConfig." + pointId);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return gp == null ? null : Arrays.asList(gp.split(","));
}

Expand All @@ -125,20 +134,26 @@ public List<String> getExtensionPointConfiguration(String pointId) {
* @param uniqueIds
*/
public void saveExtensionPointConfiguration(String pointId, String... uniqueIds) {
AdministrationService service = Context.getAdministrationService();
GlobalProperty gp = service.getGlobalPropertyObject("ui2.extensionConfig." + pointId);
if (uniqueIds.length == 0) {
if (gp != null)
service.purgeGlobalProperty(gp);
} else {
for (String id : uniqueIds) {
if (activeExtensions().get(id) == null)
throw new IllegalArgumentException("No extension found for id: " + id);
try {
AdministrationService service = Context.getAdministrationService();
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
GlobalProperty gp = service.getGlobalPropertyObject("ui2.extensionConfig." + pointId);
if (uniqueIds.length == 0) {
if (gp != null)
service.purgeGlobalProperty(gp);
} else {
for (String id : uniqueIds) {
if (activeExtensions().get(id) == null)
throw new IllegalArgumentException("No extension found for id: " + id);
}
if (gp == null)
gp = new GlobalProperty("ui2.extensionConfig." + pointId);
gp.setPropertyValue(OpenmrsUtil.join(Arrays.asList(uniqueIds), ","));
service.saveGlobalProperty(gp);
}
if (gp == null)
gp = new GlobalProperty("ui2.extensionConfig." + pointId);
gp.setPropertyValue(OpenmrsUtil.join(Arrays.asList(uniqueIds), ","));
service.saveGlobalProperty(gp);
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
}

Expand Down
12 changes: 10 additions & 2 deletions api/src/main/java/org/openmrs/util/TimeZoneUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.TimeZone;

import static org.joda.time.DateTimeZone.UTC;
import static org.openmrs.ui.framework.UiFrameworkConstants.GET_GLOBAL_PROPERTIES;

/**
* Helps provide tools to support recommended OpenMRS time zones conventions.
Expand All @@ -40,8 +41,15 @@ public class TimeZoneUtil {
* @return string with the date in the client timezone, formatted and ready to be displayed.
*/
public static String toTimezone(Date date, String format) {
String clientTimezone = Context.getAuthenticatedUser().getUserProperty(
Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE));
String clientTimezone;
try {
Context.addProxyPrivilege(GET_GLOBAL_PROPERTIES);
clientTimezone = Context.getAuthenticatedUser().getUserProperty(
Context.getAdministrationService().getGlobalProperty(UiFrameworkConstants.UP_CLIENT_TIMEZONE));
}
finally {
Context.removeProxyPrivilege(GET_GLOBAL_PROPERTIES);
}
return toTimezone(date, format, clientTimezone);
}

Expand Down
Loading
Loading