Search in sources :

Example 76 with Security

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;
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) Security(org.apache.ofbiz.security.Security)

Example 77 with Security

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);
}
Also used : SecurityConfigurationException(org.apache.ofbiz.security.SecurityConfigurationException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) Security(org.apache.ofbiz.security.Security) AssertionFailedError(junit.framework.AssertionFailedError) Map(java.util.Map) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 78 with Security

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";
}
Also used : Locale(java.util.Locale) Security(org.apache.ofbiz.security.Security)

Example 79 with Security

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";
}
Also used : Locale(java.util.Locale) Security(org.apache.ofbiz.security.Security)

Aggregations

Security (org.apache.ofbiz.security.Security)79 GenericValue (org.apache.ofbiz.entity.GenericValue)69 Delegator (org.apache.ofbiz.entity.Delegator)60 Locale (java.util.Locale)56 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)54 HashMap (java.util.HashMap)36 Timestamp (java.sql.Timestamp)27 LinkedList (java.util.LinkedList)27 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)20 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)18 Map (java.util.Map)12 HttpSession (javax.servlet.http.HttpSession)7 GeneralException (org.apache.ofbiz.base.util.GeneralException)7 BigDecimal (java.math.BigDecimal)6 List (java.util.List)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)4 File (java.io.File)3