001    /**
002     * Copyright (c) 2000-2010 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(
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                    else {
075                            Group parentGroup = GroupLocalServiceUtil.getGroup(
076                                    themeDisplay.getParentGroupId());
077    
078                            Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
079                                    parentGroup.getGroupId());
080    
081                            return defaultGroupRole.getName();
082                    }
083    
084                    return StringPool.BLANK;
085            }
086    
087            public static void doEndTag(String modelName, PageContext pageContext)
088                    throws JspException {
089    
090                    try {
091                            HttpServletRequest request =
092                                    (HttpServletRequest)pageContext.getRequest();
093    
094                            RenderResponse renderResponse =
095                                    (RenderResponse)request.getAttribute(
096                                            JavaConstants.JAVAX_PORTLET_RESPONSE);
097    
098                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
099                                    WebKeys.THEME_DISPLAY);
100    
101                            Group group = themeDisplay.getScopeGroup();
102    
103                            List<String> supportedActions =
104                                    ResourceActionsUtil.getModelResourceActions(modelName);
105                            List<String> communityDefaultActions =
106                                    ResourceActionsUtil.getModelResourceCommunityDefaultActions(
107                                            modelName);
108                            List<String> guestDefaultActions =
109                                    ResourceActionsUtil.getModelResourceGuestDefaultActions(
110                                            modelName);
111                            List<String> guestUnsupportedActions =
112                                    ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
113                                            modelName);
114    
115                            StringBundler sb = new StringBundler();
116    
117                            for (int i = 0; i < supportedActions.size(); i++) {
118                                    String action = supportedActions.get(i);
119    
120                                    boolean communityChecked = communityDefaultActions.contains(
121                                            action);
122                                    boolean guestChecked = guestDefaultActions.contains(action);
123                                    boolean guestDisabled = guestUnsupportedActions.contains(
124                                            action);
125    
126                                    if (guestDisabled) {
127                                            guestChecked = false;
128                                    }
129    
130                                    if (group.isCommunity() || group.isOrganization()) {
131                                            if (communityChecked) {
132                                                    sb.append(StringPool.AMPERSAND);
133                                                    sb.append(renderResponse.getNamespace());
134                                                    sb.append("communityPermissions=");
135                                                    sb.append(HttpUtil.encodeURL(action));
136                                            }
137                                    }
138    
139                                    if (guestChecked) {
140                                            sb.append(StringPool.AMPERSAND);
141                                            sb.append(renderResponse.getNamespace());
142                                            sb.append("guestPermissions=");
143                                            sb.append(HttpUtil.encodeURL(action));
144                                    }
145                            }
146    
147                            String inputPermissionsViewRole = getDefaultViewRole(
148                                    modelName, themeDisplay);
149    
150                            sb.append(StringPool.AMPERSAND);
151                            sb.append(renderResponse.getNamespace());
152                            sb.append("inputPermissionsViewRole=");
153                            sb.append(HttpUtil.encodeURL(inputPermissionsViewRole));
154    
155                            pageContext.getOut().print(sb.toString());
156                    }
157                    catch (Exception e) {
158                            throw new JspException(e);
159                    }
160            }
161    
162    }