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.portal.servlet.taglib.ui;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Group;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.Role;
026    import com.liferay.portal.model.RoleConstants;
027    import com.liferay.portal.security.permission.ActionKeys;
028    import com.liferay.portal.security.permission.ResourceActionsUtil;
029    import com.liferay.portal.service.GroupLocalServiceUtil;
030    import com.liferay.portal.service.RoleLocalServiceUtil;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.WebKeys;
033    
034    import java.util.List;
035    
036    import javax.portlet.RenderResponse;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.jsp.JspException;
040    import javax.servlet.jsp.PageContext;
041    
042    /**
043     * @author Brian Chan
044     * @author Jorge Ferrer
045     */
046    public class InputPermissionsParamsTagUtil {
047    
048            public static void doEndTag(String modelName, PageContext pageContext)
049                    throws JspException {
050    
051                    try {
052                            HttpServletRequest request =
053                                    (HttpServletRequest)pageContext.getRequest();
054    
055                            RenderResponse renderResponse =
056                                    (RenderResponse)request.getAttribute(
057                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
058    
059                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
060                                    WebKeys.THEME_DISPLAY);
061    
062                            Layout layout = themeDisplay.getLayout();
063    
064                            Group layoutGroup = layout.getGroup();
065    
066                            Group group = themeDisplay.getScopeGroup();
067    
068                            List<String> supportedActions =
069                                    ResourceActionsUtil.getModelResourceActions(modelName);
070                            List<String> groupDefaultActions =
071                                    ResourceActionsUtil.getModelResourceGroupDefaultActions(
072                                            modelName);
073                            List<String> guestDefaultActions =
074                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(
075                                            modelName);
076                            List<String> guestUnsupportedActions =
077                                    ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
078                                            modelName);
079    
080                            StringBundler sb = new StringBundler();
081    
082                            for (int i = 0; i < supportedActions.size(); i++) {
083                                    String action = supportedActions.get(i);
084    
085                                    boolean groupChecked = groupDefaultActions.contains(action);
086    
087                                    boolean guestChecked = false;
088    
089                                    if (layoutGroup.isControlPanel()) {
090                                            if (!group.hasPrivateLayouts() &&
091                                                    guestDefaultActions.contains(action)) {
092    
093                                                    guestChecked = true;
094                                            }
095                                    }
096                                    else if (layout.isPublicLayout() &&
097                                                     guestDefaultActions.contains(action)) {
098    
099                                            guestChecked = true;
100                                    }
101    
102                                    boolean guestDisabled = guestUnsupportedActions.contains(
103                                            action);
104    
105                                    if (guestDisabled) {
106                                            guestChecked = false;
107                                    }
108    
109                                    if (group.isOrganization() || group.isRegularSite()) {
110                                            if (groupChecked) {
111                                                    sb.append(StringPool.AMPERSAND);
112                                                    sb.append(renderResponse.getNamespace());
113                                                    sb.append("groupPermissions=");
114                                                    sb.append(HttpUtil.encodeURL(action));
115                                            }
116                                    }
117    
118                                    if (guestChecked) {
119                                            sb.append(StringPool.AMPERSAND);
120                                            sb.append(renderResponse.getNamespace());
121                                            sb.append("guestPermissions=");
122                                            sb.append(HttpUtil.encodeURL(action));
123                                    }
124                            }
125    
126                            String inputPermissionsViewRole = getDefaultViewRole(
127                                    modelName, themeDisplay);
128    
129                            sb.append(StringPool.AMPERSAND);
130                            sb.append(renderResponse.getNamespace());
131                            sb.append("inputPermissionsViewRole=");
132                            sb.append(HttpUtil.encodeURL(inputPermissionsViewRole));
133    
134                            pageContext.getOut().print(sb.toString());
135                    }
136                    catch (Exception e) {
137                            throw new JspException(e);
138                    }
139            }
140    
141            public static String getDefaultViewRole(
142                            String modelName, ThemeDisplay themeDisplay)
143                    throws PortalException, SystemException {
144    
145                    Layout layout = themeDisplay.getLayout();
146    
147                    Group layoutGroup = layout.getGroup();
148    
149                    List<String> guestDefaultActions =
150                            ResourceActionsUtil.getModelResourceGuestDefaultActions(modelName);
151    
152                    if (layoutGroup.isControlPanel()) {
153                            Group group = themeDisplay.getScopeGroup();
154    
155                            if (!group.hasPrivateLayouts() &&
156                                    guestDefaultActions.contains(ActionKeys.VIEW)) {
157    
158                                    return RoleConstants.GUEST;
159                            }
160                    }
161                    else if (layout.isPublicLayout() &&
162                                     guestDefaultActions.contains(ActionKeys.VIEW)) {
163    
164                            return RoleConstants.GUEST;
165                    }
166    
167                    List<String> groupDefaultActions =
168                            ResourceActionsUtil.getModelResourceGroupDefaultActions(modelName);
169    
170                    if (groupDefaultActions.contains(ActionKeys.VIEW)) {
171                            Group parentGroup = GroupLocalServiceUtil.getGroup(
172                                    themeDisplay.getParentGroupId());
173    
174                            Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
175                                    parentGroup.getGroupId());
176    
177                            return defaultGroupRole.getName();
178                    }
179    
180                    return RoleConstants.OWNER;
181            }
182    
183    }