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.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.kernel.util.WebKeys;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.Role;
027    import com.liferay.portal.model.RoleConstants;
028    import com.liferay.portal.security.permission.ActionKeys;
029    import com.liferay.portal.security.permission.ResourceActionsUtil;
030    import com.liferay.portal.service.GroupLocalServiceUtil;
031    import com.liferay.portal.service.RoleLocalServiceUtil;
032    import com.liferay.portal.theme.ThemeDisplay;
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    import javax.servlet.jsp.tagext.TagSupport;
042    
043    /**
044     * @author Brian Wing Shun Chan
045     * @author Jorge Ferrer
046     * @see    com.liferay.portal.servlet.taglib.ui.InputPermissionsParamsTagUtil
047     */
048    public class InputPermissionsParamsTag extends TagSupport {
049    
050            public static String doTag(String modelName, PageContext pageContext)
051                    throws Exception {
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                    return StringPool.BLANK;
143            }
144    
145            public static String getDefaultViewRole(
146                            String modelName, ThemeDisplay themeDisplay)
147                    throws PortalException, SystemException {
148    
149                    Layout layout = themeDisplay.getLayout();
150    
151                    Group layoutGroup = layout.getGroup();
152    
153                    List<String> guestDefaultActions =
154                            ResourceActionsUtil.getModelResourceGuestDefaultActions(modelName);
155    
156                    if (layoutGroup.isControlPanel()) {
157                            Group group = themeDisplay.getScopeGroup();
158    
159                            if (!group.hasPrivateLayouts() &&
160                                    guestDefaultActions.contains(ActionKeys.VIEW)) {
161    
162                                    return RoleConstants.GUEST;
163                            }
164                    }
165                    else if (layout.isPublicLayout() &&
166                                     guestDefaultActions.contains(ActionKeys.VIEW)) {
167    
168                            return RoleConstants.GUEST;
169                    }
170    
171                    List<String> groupDefaultActions =
172                            ResourceActionsUtil.getModelResourceGroupDefaultActions(modelName);
173    
174                    if (groupDefaultActions.contains(ActionKeys.VIEW)) {
175                            Group parentGroup = GroupLocalServiceUtil.getGroup(
176                                    themeDisplay.getParentGroupId());
177    
178                            Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
179                                    parentGroup.getGroupId());
180    
181                            return defaultGroupRole.getName();
182                    }
183    
184                    return RoleConstants.OWNER;
185            }
186    
187            @Override
188            public int doEndTag() throws JspException {
189                    try {
190                            doTag(_modelName, pageContext);
191    
192                            return EVAL_PAGE;
193                    }
194                    catch (Exception e) {
195                            throw new JspException(e);
196                    }
197            }
198    
199            public void setModelName(String modelName) {
200                    _modelName = modelName;
201            }
202    
203            private String _modelName;
204    
205    }