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, Object resourceGroupId,
047                            String resourcePrimKey, String windowState, String var,
048                            int[] roleTypes, 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                    if (Validator.isNull(resourceGroupId)) {
058                            resourceGroupId = String.valueOf(themeDisplay.getScopeGroupId());
059                    }
060    
061                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
062    
063                    Layout layout = themeDisplay.getLayout();
064    
065                    if (Validator.isNull(redirect) &&
066                            (Validator.isNull(windowState) ||
067                             !windowState.equals(LiferayWindowState.POP_UP.toString()))) {
068    
069                            redirect = PortalUtil.getCurrentURL(request);
070                    }
071    
072                    PortletURL portletURL = PortletURLFactoryUtil.create(
073                            request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
074                            PortletRequest.RENDER_PHASE);
075    
076                    if (Validator.isNotNull(windowState)) {
077                            portletURL.setWindowState(
078                                    WindowStateFactory.getWindowState(windowState));
079                    }
080                    else if (themeDisplay.isStatePopUp()) {
081                            portletURL.setWindowState(LiferayWindowState.POP_UP);
082                    }
083                    else {
084                            portletURL.setWindowState(WindowState.MAXIMIZED);
085                    }
086    
087                    portletURL.setParameter(
088                            "struts_action", "/portlet_configuration/edit_permissions");
089    
090                    if (Validator.isNotNull(redirect)) {
091                            portletURL.setParameter("redirect", redirect);
092    
093                            if (!themeDisplay.isStateMaximized()) {
094                                    portletURL.setParameter("returnToFullPageURL", redirect);
095                            }
096                    }
097    
098                    portletURL.setParameter("portletResource", portletDisplay.getId());
099                    portletURL.setParameter("modelResource", modelResource);
100                    portletURL.setParameter(
101                            "modelResourceDescription", modelResourceDescription);
102                    portletURL.setParameter(
103                            "resourceGroupId", String.valueOf(resourceGroupId));
104                    portletURL.setParameter("resourcePrimKey", resourcePrimKey);
105    
106                    if (roleTypes != null) {
107                            portletURL.setParameter("roleTypes", StringUtil.merge(roleTypes));
108                    }
109    
110                    String portletURLToString = portletURL.toString();
111    
112                    if (Validator.isNotNull(var)) {
113                            pageContext.setAttribute(var, portletURLToString);
114                    }
115                    else {
116                            JspWriter jspWriter = pageContext.getOut();
117    
118                            jspWriter.write(portletURLToString);
119                    }
120            }
121    
122            @Override
123            public int doEndTag() throws JspException {
124                    try {
125                            doTag(
126                                    _redirect, _modelResource, _modelResourceDescription,
127                                    _resourceGroupId, _resourcePrimKey, _windowState, _var,
128                                    _roleTypes, pageContext);
129                    }
130                    catch (Exception e) {
131                            throw new JspException(e);
132                    }
133    
134                    return EVAL_PAGE;
135            }
136    
137            public void setModelResource(String modelResource) {
138                    _modelResource = modelResource;
139            }
140    
141            public void setModelResourceDescription(String modelResourceDescription) {
142                    _modelResourceDescription = modelResourceDescription;
143            }
144    
145            public void setRedirect(String redirect) {
146                    _redirect = redirect;
147            }
148    
149            public void setResourceGroupId(Object resourceGroupId) {
150                    _resourceGroupId = resourceGroupId;
151            }
152    
153            public void setResourcePrimKey(String resourcePrimKey) {
154                    _resourcePrimKey = resourcePrimKey;
155            }
156    
157            public void setRoleTypes(int[] roleTypes) {
158                    _roleTypes = roleTypes;
159            }
160    
161            public void setVar(String var) {
162                    _var = var;
163            }
164    
165            public void setWindowState(String windowState) {
166                    _windowState = windowState;
167            }
168    
169            private String _modelResource;
170            private String _modelResourceDescription;
171            private String _redirect;
172            private Object _resourceGroupId;
173            private String _resourcePrimKey;
174            private int[] _roleTypes;
175            private String _var;
176            private String _windowState;
177    
178    }