use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class WebToolsServices method entityMaintPermCheck.
/**
* Performs an entity maintenance security check. Returns hasPermission=true
* if the user has the ENTITY_MAINT permission.
* @param dctx the dispatch context
* @param context the context
* @return return the result of the service execution
*/
public static Map<String, Object> entityMaintPermCheck(DispatchContext dctx, Map<String, ? extends Object> context) {
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
Security security = dctx.getSecurity();
Map<String, Object> resultMap = null;
if (security.hasPermission("ENTITY_MAINT", userLogin)) {
resultMap = ServiceUtil.returnSuccess();
resultMap.put("hasPermission", true);
} else {
resultMap = ServiceUtil.returnFailure(UtilProperties.getMessage(resource, "WebtoolsPermissionError", locale));
resultMap.put("hasPermission", false);
}
return resultMap;
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class SimpleMethodTest method run.
@Override
public void run(TestResult result) {
result.startTest(this);
try {
// define request
Security security = SecurityFactory.getInstance(delegator);
MockServletContext servletContext = new MockServletContext();
request.setAttribute("security", security);
request.setAttribute("servletContext", servletContext);
request.setAttribute("delegator", delegator);
request.setAttribute("dispatcher", dispatcher);
Map<String, Object> serviceResult = SimpleMethod.runSimpleService(methodLocation, methodName, dispatcher.getDispatchContext(), UtilMisc.toMap("test", this, "testResult", result, "locale", Locale.getDefault(), "request", request, "response", response));
// do something with the errorMessage
String errorMessage = (String) serviceResult.get(ModelService.ERROR_MESSAGE);
if (UtilValidate.isNotEmpty(errorMessage)) {
result.addFailure(this, new AssertionFailedError(errorMessage));
}
// do something with the errorMessageList
List<Object> errorMessageList = UtilGenerics.cast(serviceResult.get(ModelService.ERROR_MESSAGE_LIST));
if (UtilValidate.isNotEmpty(errorMessageList)) {
for (Object message : errorMessageList) {
result.addFailure(this, new AssertionFailedError(message.toString()));
}
}
// do something with the errorMessageMap
Map<String, Object> errorMessageMap = UtilGenerics.cast(serviceResult.get(ModelService.ERROR_MESSAGE_MAP));
if (!UtilValidate.isEmpty(errorMessageMap)) {
for (Map.Entry<String, Object> entry : errorMessageMap.entrySet()) {
result.addFailure(this, new AssertionFailedError(entry.getKey() + ": " + entry.getValue()));
}
}
} catch (MiniLangException e) {
result.addError(this, e);
} catch (SecurityConfigurationException e) {
result.addError(this, e);
}
result.endTest(this);
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class UtilCacheEvents method removeElementEvent.
/**
* An HTTP WebEvent handler the specified element from the specified cache
* @param request The HTTP request object for the current JSP or Servlet request.
* @param response The HTTP response object for the current JSP or Servlet request.
* @return return an HTTP WebEvent handler the specified element from the specified cache
*/
public static String removeElementEvent(HttpServletRequest request, HttpServletResponse response) {
String errMsg = "";
Locale locale = UtilHttp.getLocale(request);
Security security = (Security) request.getAttribute("security");
if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
errMsg = UtilProperties.getMessage(err_resource, "utilCacheEvents.permissionEdit", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String name = request.getParameter("UTIL_CACHE_NAME");
if (name == null) {
errMsg = UtilProperties.getMessage(err_resource, "utilCacheEvents.noCacheNameSpecified", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String numString = request.getParameter("UTIL_CACHE_ELEMENT_NUMBER");
if (numString == null) {
errMsg = UtilProperties.getMessage(err_resource, "utilCacheEvents.noElementNumberSpecified", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", "");
return "error";
}
int number;
try {
number = Integer.parseInt(numString);
} catch (Exception e) {
return "error";
}
UtilCache<?, ?> utilCache = UtilCache.findCache(name);
if (utilCache != null) {
Object key = null;
Iterator<?> ksIter = utilCache.getCacheLineKeys().iterator();
int curNum = 0;
while (ksIter.hasNext()) {
if (number == curNum) {
key = ksIter.next();
break;
} else {
ksIter.next();
}
curNum++;
}
if (key != null) {
utilCache.remove(key);
errMsg = UtilProperties.getMessage(err_resource, "utilCache.removeElementWithKey", UtilMisc.toMap("key", key.toString()), locale) + ".";
request.setAttribute("_EVENT_MESSAGE_", errMsg);
} else {
errMsg = UtilProperties.getMessage(err_resource, "utilCache.couldNotRemoveElementNumber", UtilMisc.toMap("name", name, "numString", numString), locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
} else {
errMsg = UtilProperties.getMessage(err_resource, "utilCache.couldNotRemoveElement", UtilMisc.toMap("name", name), locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
return "success";
}
use of org.apache.ofbiz.security.Security in project ofbiz-framework by apache.
the class UtilCacheEvents method clearEvent.
/**
* An HTTP WebEvent handler that clears the named cache
* @param request The HTTP request object for the current JSP or Servlet request.
* @param response The HTTP response object for the current JSP or Servlet request.
* @return return an HTTP WebEvent handler that clears the named cache
*/
public static String clearEvent(HttpServletRequest request, HttpServletResponse response) {
String errMsg = "";
Locale locale = UtilHttp.getLocale(request);
Security security = (Security) request.getAttribute("security");
if (!security.hasPermission("UTIL_CACHE_EDIT", request.getSession())) {
errMsg = UtilProperties.getMessage(err_resource, "utilCacheEvents.permissionEdit", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
String name = request.getParameter("UTIL_CACHE_NAME");
if (name == null) {
errMsg = UtilProperties.getMessage(err_resource, "utilCache.couldNotClearCache", locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
UtilCache<?, ?> utilCache = UtilCache.findCache(name);
if (utilCache != null) {
utilCache.clear();
errMsg = UtilProperties.getMessage(err_resource, "utilCache.clearCache", UtilMisc.toMap("name", name), locale) + ".";
request.setAttribute("_EVENT_MESSAGE_", errMsg);
} else {
errMsg = UtilProperties.getMessage(err_resource, "utilCache.couldNotClearCacheNotFoundName", UtilMisc.toMap("name", name), locale) + ".";
request.setAttribute("_ERROR_MESSAGE_", errMsg);
return "error";
}
return "success";
}
Aggregations