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