001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.security.permission.ResourceActionsUtil;
020    import com.liferay.taglib.util.IncludeTag;
021    
022    import java.util.List;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.PageContext;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Wilson S. Man
031     * @see    com.liferay.portal.servlet.taglib.ui.InputPermissionsTagUtil
032     */
033    public class InputPermissionsTag extends IncludeTag {
034    
035            public static String doTag(
036                            String formName, String modelName, PageContext pageContext)
037                    throws Exception {
038    
039                    return doTag(_PAGE, formName, modelName, pageContext);
040            }
041    
042            public static String doTag(
043                            String page, String formName, String modelName,
044                            PageContext pageContext)
045                    throws Exception {
046    
047                    HttpServletRequest request =
048                            (HttpServletRequest)pageContext.getRequest();
049    
050                    request.setAttribute("liferay-ui:input-permissions:formName", formName);
051    
052                    if (modelName != null) {
053                            List<String> supportedActions =
054                                    ResourceActionsUtil.getModelResourceActions(modelName);
055                            List<String> groupDefaultActions =
056                                    ResourceActionsUtil.getModelResourceGroupDefaultActions(
057                                            modelName);
058                            List<String> guestDefaultActions =
059                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(
060                                            modelName);
061                            List<String> guestUnsupportedActions =
062                                    ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
063                                            modelName);
064    
065                            request.setAttribute(
066                                    "liferay-ui:input-permissions:modelName", modelName);
067                            request.setAttribute(
068                                    "liferay-ui:input-permissions:supportedActions",
069                                    supportedActions);
070                            request.setAttribute(
071                                    "liferay-ui:input-permissions:groupDefaultActions",
072                                    groupDefaultActions);
073                            request.setAttribute(
074                                    "liferay-ui:input-permissions:guestDefaultActions",
075                                    guestDefaultActions);
076                            request.setAttribute(
077                                    "liferay-ui:input-permissions:guestUnsupportedActions",
078                                    guestUnsupportedActions);
079                    }
080    
081                    PortalIncludeUtil.include(pageContext, page);
082    
083                    return StringPool.BLANK;
084            }
085    
086            @Override
087            public int doEndTag() throws JspException {
088                    try {
089                            doTag(getPage(), _formName, _modelName, pageContext);
090    
091                            return EVAL_PAGE;
092                    }
093                    catch (Exception e) {
094                            throw new JspException(e);
095                    }
096            }
097    
098            public void setFormName(String formName) {
099                    _formName = formName;
100            }
101    
102            public void setModelName(String modelName) {
103                    _modelName = modelName;
104            }
105    
106            @Override
107            protected String getPage() {
108                    return _PAGE;
109            }
110    
111            private static final String _PAGE =
112                    "/html/taglib/ui/input_permissions/page.jsp";
113    
114            private String _formName = "fm";
115            private String _modelName;
116    
117    }