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