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