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.PortletProvider;
019    import com.liferay.portal.kernel.portlet.PortletProviderUtil;
020    import com.liferay.portal.kernel.portlet.WindowStateFactory;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.theme.PortletDisplay;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portlet.portletconfiguration.util.PortletConfigurationApplicationType;
028    
029    import javax.portlet.PortletURL;
030    import javax.portlet.WindowState;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.jsp.JspException;
034    import javax.servlet.jsp.JspWriter;
035    import javax.servlet.jsp.tagext.TagSupport;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class PermissionsURLTag extends TagSupport {
041    
042            /**
043             * Returns the URL for opening the resource's permissions configuration
044             * dialog and for configuring the resource's permissions.
045             *
046             * @param  redirect the redirect. If the redirect is <code>null</code> or
047             *         the dialog does not open as a pop-up, the current URL is obtained
048             *         via {@link PortalUtil#getCurrentURL(HttpServletRequest)} and
049             *         used.
050             * @param  modelResource the resource's class for which to configure
051             *         permissions
052             * @param  modelResourceDescription the human-friendly description of the
053             *         resource
054             * @param  resourceGroupId the group ID to which the resource belongs. The
055             *         ID can be a number, string containing a number, or substitution
056             *         string. If the resource group ID is <code>null</code>, it is
057             *         obtained via {@link ThemeDisplay#getScopeGroupId()}.
058             * @param  resourcePrimKey the primary key of the resource
059             * @param  windowState the window state to use when opening the permissions
060             *         configuration dialog. For more information, see {@link
061             *         LiferayWindowState}.
062             * @param  roleTypes the role types
063             * @param  request the current request
064             * @return the URL for opening the resource's permissions configuration
065             *         dialog and for configuring the resource's permissions
066             * @throws Exception if an exception occurred
067             */
068            public static String doTag(
069                            String redirect, String modelResource,
070                            String modelResourceDescription, Object resourceGroupId,
071                            String resourcePrimKey, String windowState, int[] roleTypes,
072                            HttpServletRequest request)
073                    throws Exception {
074    
075                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
076                            WebKeys.THEME_DISPLAY);
077    
078                    if (resourceGroupId instanceof Number) {
079                            Number resourceGroupIdNumber = (Number)resourceGroupId;
080    
081                            if (resourceGroupIdNumber.longValue() < 0) {
082                                    resourceGroupId = null;
083                            }
084                    }
085                    else if (resourceGroupId instanceof String) {
086                            String esourceGroupIdString = (String)resourceGroupId;
087    
088                            if (esourceGroupIdString.length() == 0) {
089                                    resourceGroupId = null;
090                            }
091                    }
092    
093                    if (resourceGroupId == null) {
094                            resourceGroupId = String.valueOf(themeDisplay.getScopeGroupId());
095                    }
096    
097                    if (Validator.isNull(redirect) &&
098                            (Validator.isNull(windowState) ||
099                             !windowState.equals(LiferayWindowState.POP_UP.toString()))) {
100    
101                            redirect = PortalUtil.getCurrentURL(request);
102                    }
103    
104                    PortletURL portletURL = PortletProviderUtil.getPortletURL(
105                            request,
106                            PortletConfigurationApplicationType.PortletConfiguration.CLASS_NAME,
107                            PortletProvider.Action.VIEW);
108    
109                    if (Validator.isNotNull(windowState)) {
110                            portletURL.setWindowState(
111                                    WindowStateFactory.getWindowState(windowState));
112                    }
113                    else if (themeDisplay.isStatePopUp()) {
114                            portletURL.setWindowState(LiferayWindowState.POP_UP);
115                    }
116                    else {
117                            portletURL.setWindowState(WindowState.MAXIMIZED);
118                    }
119    
120                    portletURL.setParameter("mvcPath", "/edit_permissions.jsp");
121    
122                    if (Validator.isNotNull(redirect)) {
123                            portletURL.setParameter("redirect", redirect);
124    
125                            if (!themeDisplay.isStateMaximized()) {
126                                    portletURL.setParameter("returnToFullPageURL", redirect);
127                            }
128                    }
129    
130                    portletURL.setParameter(
131                            "portletConfiguration", Boolean.TRUE.toString());
132    
133                    PortletDisplay portletDisplay = themeDisplay.getPortletDisplay();
134    
135                    portletURL.setParameter("portletResource", portletDisplay.getId());
136    
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    }