001    /**
002     * Copyright (c) 2000-present 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.util.StringPool;
018    import com.liferay.portal.security.permission.ResourceActionsUtil;
019    import com.liferay.taglib.util.IncludeTag;
020    import com.liferay.taglib.util.PortalIncludeUtil;
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     */
032    public class InputPermissionsTag extends IncludeTag {
033    
034            public static String doTag(
035                            String formName, String modelName, PageContext pageContext)
036                    throws Exception {
037    
038                    return doTag(_PAGE, formName, modelName, pageContext);
039            }
040    
041            public static String doTag(
042                            String page, String formName, String modelName,
043                            PageContext pageContext)
044                    throws Exception {
045    
046                    HttpServletRequest request =
047                            (HttpServletRequest)pageContext.getRequest();
048    
049                    request.setAttribute("liferay-ui:input-permissions:formName", formName);
050    
051                    if (modelName != null) {
052                            List<String> supportedActions =
053                                    ResourceActionsUtil.getModelResourceActions(modelName);
054                            List<String> groupDefaultActions =
055                                    ResourceActionsUtil.getModelResourceGroupDefaultActions(
056                                            modelName);
057                            List<String> guestDefaultActions =
058                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(
059                                            modelName);
060                            List<String> guestUnsupportedActions =
061                                    ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
062                                            modelName);
063    
064                            request.setAttribute(
065                                    "liferay-ui:input-permissions:modelName", modelName);
066                            request.setAttribute(
067                                    "liferay-ui:input-permissions:supportedActions",
068                                    supportedActions);
069                            request.setAttribute(
070                                    "liferay-ui:input-permissions:groupDefaultActions",
071                                    groupDefaultActions);
072                            request.setAttribute(
073                                    "liferay-ui:input-permissions:guestDefaultActions",
074                                    guestDefaultActions);
075                            request.setAttribute(
076                                    "liferay-ui:input-permissions:guestUnsupportedActions",
077                                    guestUnsupportedActions);
078                    }
079    
080                    PortalIncludeUtil.include(pageContext, page);
081    
082                    return StringPool.BLANK;
083            }
084    
085            @Override
086            public int doEndTag() throws JspException {
087                    try {
088                            doTag(getPage(), _formName, _modelName, pageContext);
089    
090                            return EVAL_PAGE;
091                    }
092                    catch (Exception e) {
093                            throw new JspException(e);
094                    }
095            }
096    
097            public void setFormName(String formName) {
098                    _formName = formName;
099            }
100    
101            public void setModelName(String modelName) {
102                    _modelName = modelName;
103            }
104    
105            @Override
106            protected String getPage() {
107                    return _PAGE;
108            }
109    
110            private static final String _PAGE =
111                    "/html/taglib/ui/input_permissions/page.jsp";
112    
113            private String _formName = "fm";
114            private String _modelName;
115    
116    }