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