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