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.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.tagext.TagSupport;
037    
038    /**
039     * @author Brian Wing Shun Chan
040     */
041    public class PermissionsURLTag extends TagSupport {
042    
043            /**
044             * Returns the URL for opening the resource's permissions configuration
045             * dialog and for configuring the resource's permissions.
046             *
047             * @param  redirect the redirect. If the redirect is <code>null</code> or
048             *         the dialog does not open as a pop-up, the current URL is obtained
049             *         via {@link PortalUtil#getCurrentURL(HttpServletRequest)} and
050             *         used.
051             * @param  modelResource the resource's class for which to configure
052             *         permissions
053             * @param  modelResourceDescription the human-friendly description of the
054             *         resource
055             * @param  resourceGroupId the group ID to which the resource belongs. The
056             *         ID can be a number, string containing a number, or substitution
057             *         string. If the resource group ID is <code>null</code>, it is
058             *         obtained via {@link ThemeDisplay#getScopeGroupId()}.
059             * @param  resourcePrimKey the primary key of the resource
060             * @param  windowState the window state to use when opening the permissions
061             *         configuration dialog. For more information, see {@link
062             *         LiferayWindowState}.
063             * @param  roleTypes the role types
064             * @param  request the current request
065             * @return the URL for opening the resource's permissions configuration
066             *         dialog and for configuring the resource's permissions
067             * @throws Exception if an exception occurred
068             */
069            public static String doTag(
070                            String redirect, String modelResource,
071                            String modelResourceDescription, Object resourceGroupId,
072                            String resourcePrimKey, String windowState, int[] roleTypes,
073                            HttpServletRequest request)
074                    throws Exception {
075    
076                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
077                            WebKeys.THEME_DISPLAY);
078    
079                    if (resourceGroupId instanceof Number) {
080                            Number resourceGroupIdNumber = (Number)resourceGroupId;
081    
082                            if (resourceGroupIdNumber.longValue() < 0) {
083                                    resourceGroupId = null;
084                            }
085                    }
086                    else if (resourceGroupId instanceof String) {
087                            String esourceGroupIdString = (String)resourceGroupId;
088    
089                            if (esourceGroupIdString.length() == 0) {
090                                    resourceGroupId = null;
091                            }
092                    }
093    
094                    if (resourceGroupId == null) {
095                            resourceGroupId = String.valueOf(themeDisplay.getScopeGroupId());
096                    }
097    
098                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
099    
100                    Layout layout = themeDisplay.getLayout();
101    
102                    if (Validator.isNull(redirect) &&
103                            (Validator.isNull(windowState) ||
104                             !windowState.equals(LiferayWindowState.POP_UP.toString()))) {
105    
106                            redirect = PortalUtil.getCurrentURL(request);
107                    }
108    
109                    PortletURL portletURL = PortletURLFactoryUtil.create(
110                            request, PortletKeys.PORTLET_CONFIGURATION, layout.getPlid(),
111                            PortletRequest.RENDER_PHASE);
112    
113                    if (Validator.isNotNull(windowState)) {
114                            portletURL.setWindowState(
115                                    WindowStateFactory.getWindowState(windowState));
116                    }
117                    else if (themeDisplay.isStatePopUp()) {
118                            portletURL.setWindowState(LiferayWindowState.POP_UP);
119                    }
120                    else {
121                            portletURL.setWindowState(WindowState.MAXIMIZED);
122                    }
123    
124                    portletURL.setParameter(
125                            "mvcPath",
126                            "/html/portlet/portlet_configuration/edit_permissions.jsp");
127    
128                    if (Validator.isNotNull(redirect)) {
129                            portletURL.setParameter("redirect", redirect);
130    
131                            if (!themeDisplay.isStateMaximized()) {
132                                    portletURL.setParameter("returnToFullPageURL", redirect);
133                            }
134                    }
135    
136                    portletURL.setParameter("portletResource", portletDisplay.getId());
137                    portletURL.setParameter("modelResource", modelResource);
138                    portletURL.setParameter(
139                            "modelResourceDescription", modelResourceDescription);
140                    portletURL.setParameter(
141                            "resourceGroupId", String.valueOf(resourceGroupId));
142                    portletURL.setParameter("resourcePrimKey", resourcePrimKey);
143    
144                    if (roleTypes != null) {
145                            portletURL.setParameter("roleTypes", StringUtil.merge(roleTypes));
146                    }
147    
148                    return portletURL.toString();
149            }
150    
151            @Override
152            public int doEndTag() throws JspException {
153                    try {
154                            String portletURLToString = doTag(
155                                    _redirect, _modelResource, _modelResourceDescription,
156                                    _resourceGroupId, _resourcePrimKey, _windowState, _roleTypes,
157                                    (HttpServletRequest)pageContext.getRequest());
158    
159                            if (Validator.isNotNull(_var)) {
160                                    pageContext.setAttribute(_var, portletURLToString);
161                            }
162                            else {
163                                    JspWriter jspWriter = pageContext.getOut();
164    
165                                    jspWriter.write(portletURLToString);
166                            }
167                    }
168                    catch (Exception e) {
169                            throw new JspException(e);
170                    }
171    
172                    return EVAL_PAGE;
173            }
174    
175            public void setModelResource(String modelResource) {
176                    _modelResource = modelResource;
177            }
178    
179            public void setModelResourceDescription(String modelResourceDescription) {
180                    _modelResourceDescription = modelResourceDescription;
181            }
182    
183            public void setRedirect(String redirect) {
184                    _redirect = redirect;
185            }
186    
187            public void setResourceGroupId(Object resourceGroupId) {
188                    _resourceGroupId = resourceGroupId;
189            }
190    
191            public void setResourcePrimKey(String resourcePrimKey) {
192                    _resourcePrimKey = resourcePrimKey;
193            }
194    
195            public void setRoleTypes(int[] roleTypes) {
196                    _roleTypes = roleTypes;
197            }
198    
199            public void setVar(String var) {
200                    _var = var;
201            }
202    
203            public void setWindowState(String windowState) {
204                    _windowState = windowState;
205            }
206    
207            private String _modelResource;
208            private String _modelResourceDescription;
209            private String _redirect;
210            private Object _resourceGroupId;
211            private String _resourcePrimKey;
212            private int[] _roleTypes;
213            private String _var;
214            private String _windowState;
215    
216    }