001    /**
002     * Copyright (c) 2000-2012 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.security;
016    
017    import com.liferay.portal.kernel.portlet.LiferayWindowState;
018    import com.liferay.portal.kernel.portlet.WindowStateFactory;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.theme.PortletDisplay;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.PortletKeys;
027    import com.liferay.portlet.PortletURLFactoryUtil;
028    
029    import javax.portlet.PortletRequest;
030    import javax.portlet.PortletURL;
031    import javax.portlet.WindowState;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.jsp.JspException;
035    import javax.servlet.jsp.JspWriter;
036    import javax.servlet.jsp.PageContext;
037    import javax.servlet.jsp.tagext.TagSupport;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class PermissionsURLTag extends TagSupport {
043    
044            public static void doTag(
045                            String redirect, String modelResource,
046                            String modelResourceDescription, String resourcePrimKey,
047                            String windowState, String var, int[] roleTypes,
048                            PageContext pageContext)
049                    throws Exception {
050    
051                    HttpServletRequest request =
052                            (HttpServletRequest)pageContext.getRequest();
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
058    
059                    Layout layout = themeDisplay.getLayout();
060    
061                    if (Validator.isNull(redirect) &&
062                            (Validator.isNull(windowState) ||
063                             !windowState.equals(LiferayWindowState.POP_UP.toString()))) {
064    
065                            redirect = PortalUtil.getCurrentURL(request);
066                    }
067    
068                    PortletURL portletURL = PortletURLFactoryUtil.create(
069                            request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
070                            PortletRequest.RENDER_PHASE);
071    
072                    if (Validator.isNotNull(windowState)) {
073                            portletURL.setWindowState(
074                                    WindowStateFactory.getWindowState(windowState));
075                    }
076                    else if (themeDisplay.isStatePopUp()) {
077                            portletURL.setWindowState(LiferayWindowState.POP_UP);
078                    }
079                    else {
080                            portletURL.setWindowState(WindowState.MAXIMIZED);
081                    }
082    
083                    portletURL.setParameter(
084                            "struts_action", "/portlet_configuration/edit_permissions");
085    
086                    if (Validator.isNotNull(redirect)) {
087                            portletURL.setParameter("redirect", redirect);
088    
089                            if (!themeDisplay.isStateMaximized()) {
090                                    portletURL.setParameter("returnToFullPageURL", redirect);
091                            }
092                    }
093    
094                    portletURL.setParameter("portletResource", portletDisplay.getId());
095                    portletURL.setParameter("modelResource", modelResource);
096                    portletURL.setParameter(
097                            "modelResourceDescription", modelResourceDescription);
098                    portletURL.setParameter("resourcePrimKey", resourcePrimKey);
099    
100                    if (roleTypes != null) {
101                            portletURL.setParameter("roleTypes", StringUtil.merge(roleTypes));
102                    }
103    
104                    String portletURLToString = portletURL.toString();
105    
106                    if (Validator.isNotNull(var)) {
107                            pageContext.setAttribute(var, portletURLToString);
108                    }
109                    else {
110                            JspWriter jspWriter = pageContext.getOut();
111    
112                            jspWriter.write(portletURLToString);
113                    }
114            }
115    
116            @Override
117            public int doEndTag() throws JspException {
118                    try {
119                            doTag(
120                                    _redirect, _modelResource, _modelResourceDescription,
121                                    _resourcePrimKey, _windowState, _var, _roleTypes, pageContext);
122                    }
123                    catch (Exception e) {
124                            throw new JspException(e);
125                    }
126    
127                    return EVAL_PAGE;
128            }
129    
130            public void setModelResource(String modelResource) {
131                    _modelResource = modelResource;
132            }
133    
134            public void setModelResourceDescription(String modelResourceDescription) {
135                    _modelResourceDescription = modelResourceDescription;
136            }
137    
138            public void setRedirect(String redirect) {
139                    _redirect = redirect;
140            }
141    
142            public void setResourcePrimKey(String resourcePrimKey) {
143                    _resourcePrimKey = resourcePrimKey;
144            }
145    
146            public void setRoleTypes(int[] roleTypes) {
147                    _roleTypes = roleTypes;
148            }
149    
150            public void setVar(String var) {
151                    _var = var;
152            }
153    
154            public void setWindowState(String windowState) {
155                    _windowState = windowState;
156            }
157    
158            private String _modelResource;
159            private String _modelResourceDescription;
160            private String _redirect;
161            private String _resourcePrimKey;
162            private int[] _roleTypes;
163            private String _var;
164            private String _windowState;
165    
166    }