001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
058                                    modelName);
059    
060                    if (layoutGroup.isControlPanel()) {
061                            Group group = themeDisplay.getScopeGroup();
062    
063                            if (!group.hasPrivateLayouts() &&
064                                    guestDefaultActions.contains(ActionKeys.VIEW)) {
065    
066                                    return RoleConstants.GUEST;
067                            }
068                    }
069                    else if (layout.isPublicLayout() &&
070                                     guestDefaultActions.contains(ActionKeys.VIEW)) {
071    
072                            return RoleConstants.GUEST;
073                    }
074    
075                    List<String> groupDefaultActions =
076                            ResourceActionsUtil.getModelResourceGroupDefaultActions(modelName);
077    
078                    if (groupDefaultActions.contains(ActionKeys.VIEW)) {
079                            Group parentGroup = GroupLocalServiceUtil.getGroup(
080                                    themeDisplay.getParentGroupId());
081    
082                            Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
083                                    parentGroup.getGroupId());
084    
085                            return defaultGroupRole.getName();
086                    }
087    
088                    return RoleConstants.OWNER;
089            }
090    
091            public static void doEndTag(String modelName, PageContext pageContext)
092                    throws JspException {
093    
094                    try {
095                            HttpServletRequest request =
096                                    (HttpServletRequest)pageContext.getRequest();
097    
098                            RenderResponse renderResponse =
099                                    (RenderResponse)request.getAttribute(
100                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
101    
102                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
103                                    WebKeys.THEME_DISPLAY);
104    
105                            Layout layout = themeDisplay.getLayout();
106    
107                            Group layoutGroup = layout.getGroup();
108    
109                            Group group = themeDisplay.getScopeGroup();
110    
111                            List<String> supportedActions =
112                                    ResourceActionsUtil.getModelResourceActions(modelName);
113                            List<String> groupDefaultActions =
114                                    ResourceActionsUtil.getModelResourceGroupDefaultActions(
115                                            modelName);
116                            List<String> guestDefaultActions =
117                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(
118                                            modelName);
119                            List<String> guestUnsupportedActions =
120                                    ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
121                                            modelName);
122    
123                            StringBundler sb = new StringBundler();
124    
125                            for (int i = 0; i < supportedActions.size(); i++) {
126                                    String action = supportedActions.get(i);
127    
128                                    boolean groupChecked = groupDefaultActions.contains(action);
129    
130                                    boolean guestChecked = false;
131    
132                                    if (layoutGroup.isControlPanel()) {
133                                            if (!group.hasPrivateLayouts() &&
134                                                    guestDefaultActions.contains(action)) {
135    
136                                                    guestChecked = true;
137                                            }
138                                    }
139                                    else if (layout.isPublicLayout() &&
140                                                     guestDefaultActions.contains(action)) {
141    
142                                            guestChecked = true;
143                                    }
144    
145                                    boolean guestDisabled = guestUnsupportedActions.contains(
146                                            action);
147    
148                                    if (guestDisabled) {
149                                            guestChecked = false;
150                                    }
151    
152                                    if (group.isOrganization() || group.isRegularSite()) {
153                                            if (groupChecked) {
154                                                    sb.append(StringPool.AMPERSAND);
155                                                    sb.append(renderResponse.getNamespace());
156                                                    sb.append("groupPermissions=");
157                                                    sb.append(HttpUtil.encodeURL(action));
158                                            }
159                                    }
160    
161                                    if (guestChecked) {
162                                            sb.append(StringPool.AMPERSAND);
163                                            sb.append(renderResponse.getNamespace());
164                                            sb.append("guestPermissions=");
165                                            sb.append(HttpUtil.encodeURL(action));
166                                    }
167                            }
168    
169                            String inputPermissionsViewRole = getDefaultViewRole(
170                                    modelName, themeDisplay);
171    
172                            sb.append(StringPool.AMPERSAND);
173                            sb.append(renderResponse.getNamespace());
174                            sb.append("inputPermissionsViewRole=");
175                            sb.append(HttpUtil.encodeURL(inputPermissionsViewRole));
176    
177                            pageContext.getOut().print(sb.toString());
178                    }
179                    catch (Exception e) {
180                            throw new JspException(e);
181                    }
182            }
183    
184    }