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