Search in sources :

Example 1 with PermissionEvaluator

use of org.alfresco.web.ui.repo.component.evaluator.PermissionEvaluator in project acs-community-packaging by Alfresco.

the class UIActions method buildActionGroup.

/**
 * Build an action group as reusable UIActionLink components.
 *
 * @param context FacesContext
 * @param config ActionsConfigElement
 * @param actionGroup ActionGroup
 * @param contextId String
 * @throws IOException
 */
@SuppressWarnings("unchecked")
private void buildActionGroup(FacesContext context, ActionsConfigElement config, ActionGroup actionGroup, String contextId) throws IOException {
    javax.faces.application.Application facesApp = context.getApplication();
    ResourceBundle messages = Application.getBundle(context);
    // get overriding display attributes
    String style = (String) getAttributes().get(ATTR_STYLE);
    String styleClass = (String) getAttributes().get(ATTR_STYLECLASS);
    Boolean showLink = null;
    if (getAttributes().get(ATTR_SHOWLINK) != null) {
        showLink = (Boolean) getAttributes().get(ATTR_SHOWLINK);
    }
    // build parent wrapper component
    HtmlPanelGroup wrapper = (HtmlPanelGroup) facesApp.createComponent(ComponentConstants.JAVAX_FACES_PANELGROUP);
    wrapper.setId(createUniqueId());
    wrapper.getAttributes().put("contextId", contextId);
    this.getChildren().add(wrapper);
    if (logger.isDebugEnabled())
        logger.debug("UIActions id: " + this.getId() + " Children() structure size: " + this.getChildren().size());
    this.groups.add(contextId);
    // process each ActionDefinition in the order they were defined
    for (String actionId : actionGroup) {
        if (logger.isDebugEnabled())
            logger.debug("---processing ActionDefinition: " + actionId);
        ActionDefinition actionDef = config.getActionDefinition(actionId);
        if (actionDef == null) {
            throw new AlfrescoRuntimeException("Unable to find configured ActionDefinition Id: " + actionId);
        }
        UIComponent currentParent = wrapper;
        // build a permissions evaluator component to wrap the actionlink
        PermissionEvaluator permEval = null;
        List<String> allow = actionDef.getAllowPermissions();
        if (allow != null && allow.size() != 0) {
            // found some permissions to test
            permEval = (PermissionEvaluator) facesApp.createComponent(COMPONENT_PERMISSIONEVAL);
            String condition = allow.get(0);
            if (allow.size() != 1) {
                for (int i = 1; i < allow.size(); i++) {
                    condition += "," + allow.get(i);
                }
            }
            permEval.setAllow(condition);
        }
        List<String> deny = actionDef.getDenyPermissions();
        if (deny != null && deny.size() != 0) {
            if (permEval == null) {
                permEval = (PermissionEvaluator) facesApp.createComponent(COMPONENT_PERMISSIONEVAL);
            }
            String condition = deny.get(0);
            if (deny.size() != 1) {
                for (int i = 1; i < deny.size(); i++) {
                    condition += "," + deny.get(i);
                }
            }
            permEval.setDeny(condition);
        }
        if (permEval != null) {
            // add the permission evaluator component and walk down the hierarchy
            permEval.setId(createUniqueId());
            permEval.setValueBinding(ATTR_VALUE, facesApp.createValueBinding("#{" + ACTION_CONTEXT + "}"));
            if (logger.isDebugEnabled())
                logger.debug("-----adding PermissionEvaluator to action");
            currentParent.getChildren().add(permEval);
            currentParent = permEval;
        }
        // now prepare any code based evaluators that may be present
        if (actionDef.Evaluator != null) {
            ActionInstanceEvaluator evaluator = (ActionInstanceEvaluator) facesApp.createComponent(COMPONENT_ACTIONEVAL);
            evaluator.setId(createUniqueId());
            evaluator.setEvaluator(actionDef.Evaluator);
            evaluator.setValueBinding(ATTR_VALUE, facesApp.createValueBinding("#{" + ACTION_CONTEXT + "}"));
            // add the action evaluator component and walk down the hiearchy
            if (logger.isDebugEnabled())
                logger.debug("-----adding ActionEvaluator to action");
            currentParent.getChildren().add(evaluator);
            currentParent = evaluator;
        }
        // now build the UIActionLink component for this action
        UIActionLink control = (UIActionLink) facesApp.createComponent(COMPONENT_ACTIONLINK);
        control.setRendererType(RENDERER_ACTIONLINK);
        control.setId(actionDef.getId() + createUniqueId());
        if (actionDef.Action != null) {
            if (UIComponentTagUtils.isValueReference(actionDef.Action)) {
                control.setAction(facesApp.createMethodBinding(actionDef.Action, null));
            } else {
                control.setAction(new ConstantMethodBinding(actionDef.Action));
            }
        }
        if (actionDef.ActionListener != null) {
            control.setActionListener(facesApp.createMethodBinding(actionDef.ActionListener, ACTION_CLASS_ARGS));
        }
        if (style != null) {
            control.getAttributes().put(ATTR_STYLE, style);
        } else if (actionDef.Style != null) {
            control.getAttributes().put(ATTR_STYLE, actionDef.Style);
        }
        if (styleClass != null) {
            control.getAttributes().put(ATTR_STYLECLASS, styleClass);
        } else if (actionDef.StyleClass != null) {
            control.getAttributes().put(ATTR_STYLECLASS, actionDef.StyleClass);
        }
        if (showLink != null) {
            control.setShowLink(showLink.booleanValue());
        } else {
            control.setShowLink(actionDef.ShowLink);
        }
        if (actionDef.Onclick != null) {
            if (UIComponentTagUtils.isValueReference(actionDef.Onclick)) {
                control.setValueBinding("onclick", facesApp.createValueBinding(actionDef.Onclick));
            } else {
                control.setOnclick(actionDef.Onclick);
            }
        }
        if (actionDef.Href != null) {
            if (UIComponentTagUtils.isValueReference(actionDef.Href)) {
                control.setValueBinding("href", facesApp.createValueBinding(actionDef.Href));
            } else {
                control.setHref(actionDef.Href);
            }
        } else if (actionDef.Script != null && actionDef.Script.length() != 0) {
            // found a script reference - may be a Path or a NodeRef
            StringBuilder scriptHref = new StringBuilder(100);
            scriptHref.append("/command/script/execute");
            if (actionDef.Script.charAt(0) == '/') {
                // found a Path - encode it as a URL argument
                scriptHref.append("?scriptPath=");
                scriptHref.append(URLEncoder.encode(actionDef.Script));
            } else {
                // found a NodeRef string, encode as URL elements
                NodeRef ref = new NodeRef(actionDef.Script);
                scriptHref.append('/').append(ref.getStoreRef().getProtocol()).append('/').append(ref.getStoreRef().getIdentifier()).append('/').append(ref.getId());
            }
            // set the full script execution URL as the href for the control
            control.setHref(scriptHref.toString());
        }
        control.setTarget(actionDef.Target);
        control.setImage(actionDef.Image);
        if (actionDef.TooltipMsg != null) {
            control.setTooltip(messages.getString(actionDef.TooltipMsg));
        } else if (actionDef.Tooltip != null) {
            if (UIComponentTagUtils.isValueReference(actionDef.Tooltip)) {
                control.setValueBinding("tooltip", facesApp.createValueBinding(actionDef.Tooltip));
            } else {
                control.setValue(actionDef.Tooltip);
            }
        }
        if (actionDef.LabelMsg != null) {
            control.setValue(messages.getString(actionDef.LabelMsg));
        } else if (actionDef.Label != null) {
            if (UIComponentTagUtils.isValueReference(actionDef.Label)) {
                control.setValueBinding("value", facesApp.createValueBinding(actionDef.Label));
            } else {
                control.setValue(actionDef.Label);
            }
        }
        // build any child params <f:param> components that are needed.
        Map<String, String> params = actionDef.getParams();
        if (params != null) {
            for (String name : params.keySet()) {
                UIParameter param = (UIParameter) facesApp.createComponent(ComponentConstants.JAVAX_FACES_PARAMETER);
                param.setId(createUniqueId());
                param.setName(name);
                String value = params.get(name);
                if (UIComponentTagUtils.isValueReference(value)) {
                    param.setValueBinding(ATTR_VALUE, facesApp.createValueBinding(value));
                } else {
                    param.setValue(value);
                }
                control.getChildren().add(param);
            }
        }
        if (logger.isDebugEnabled())
            logger.debug("-----adding UIActionLink component for: " + actionId);
        currentParent.getChildren().add(control);
    }
}
Also used : ActionInstanceEvaluator(org.alfresco.web.ui.repo.component.evaluator.ActionInstanceEvaluator) PermissionEvaluator(org.alfresco.web.ui.repo.component.evaluator.PermissionEvaluator) UIParameter(javax.faces.component.UIParameter) UIActionLink(org.alfresco.web.ui.common.component.UIActionLink) UIComponent(javax.faces.component.UIComponent) ActionDefinition(org.alfresco.web.config.ActionsConfigElement.ActionDefinition) NodeRef(org.alfresco.service.cmr.repository.NodeRef) ConstantMethodBinding(org.springframework.extensions.webscripts.ui.common.ConstantMethodBinding) HtmlPanelGroup(javax.faces.component.html.HtmlPanelGroup) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ResourceBundle(java.util.ResourceBundle)

Aggregations

ResourceBundle (java.util.ResourceBundle)1 UIComponent (javax.faces.component.UIComponent)1 UIParameter (javax.faces.component.UIParameter)1 HtmlPanelGroup (javax.faces.component.html.HtmlPanelGroup)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 ActionDefinition (org.alfresco.web.config.ActionsConfigElement.ActionDefinition)1 UIActionLink (org.alfresco.web.ui.common.component.UIActionLink)1 ActionInstanceEvaluator (org.alfresco.web.ui.repo.component.evaluator.ActionInstanceEvaluator)1 PermissionEvaluator (org.alfresco.web.ui.repo.component.evaluator.PermissionEvaluator)1 ConstantMethodBinding (org.springframework.extensions.webscripts.ui.common.ConstantMethodBinding)1