1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.servlet.taglib.ui;
16  
17  import com.liferay.portal.kernel.util.JavaConstants;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.GroupConstants;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.security.permission.ResourceActionsUtil;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.WebKeys;
26  
27  import java.util.List;
28  
29  import javax.portlet.RenderResponse;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.PageContext;
34  
35  /**
36   * <a href="InputPermissionsParamsTagUtil.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Chan
40   * @author Jorge Ferrer
41   */
42  public class InputPermissionsParamsTagUtil {
43  
44      public static void doEndTag(String modelName, PageContext pageContext)
45          throws JspException {
46  
47          try {
48              HttpServletRequest request =
49                  (HttpServletRequest)pageContext.getRequest();
50  
51              RenderResponse renderResponse =
52                  (RenderResponse)request.getAttribute(
53                      JavaConstants.JAVAX_PORTLET_RESPONSE);
54  
55              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
56                  WebKeys.THEME_DISPLAY);
57  
58              Layout layout = themeDisplay.getLayout();
59  
60              Group group = themeDisplay.getScopeGroup();
61              Group layoutGroup = layout.getGroup();
62  
63              List<String> supportedActions =
64                  ResourceActionsUtil.getModelResourceActions(modelName);
65              List<String> communityDefaultActions =
66                  ResourceActionsUtil.getModelResourceCommunityDefaultActions(
67                      modelName);
68              List<String> guestDefaultActions =
69                  ResourceActionsUtil.getModelResourceGuestDefaultActions(
70                      modelName);
71              List<String> guestUnsupportedActions =
72                  ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
73                      modelName);
74  
75              StringBundler sb = new StringBundler();
76  
77              for (int i = 0; i < supportedActions.size(); i++) {
78                  String action = supportedActions.get(i);
79  
80                  boolean communityChecked = communityDefaultActions.contains(
81                      action);
82                  boolean guestChecked = guestDefaultActions.contains(action);
83                  boolean guestDisabled = guestUnsupportedActions.contains(
84                      action);
85  
86                  if (guestDisabled) {
87                      guestChecked = false;
88                  }
89  
90                  if (group.isCommunity() || group.isOrganization()) {
91                      if (communityChecked) {
92                          sb.append(StringPool.AMPERSAND);
93                          sb.append(renderResponse.getNamespace());
94                          sb.append("communityPermissions=");
95                          sb.append(action);
96                      }
97                  }
98  
99                  if (guestChecked) {
100                     sb.append(StringPool.AMPERSAND);
101                     sb.append(renderResponse.getNamespace());
102                     sb.append("guestPermissions=");
103                     sb.append(action);
104                 }
105             }
106 
107             boolean inputPermissionsPublic = false;
108 
109             if (layoutGroup.getName().equals(GroupConstants.CONTROL_PANEL)) {
110                 if (!group.hasPrivateLayouts()) {
111                     inputPermissionsPublic = true;
112                 }
113             }
114             else if (layout.isPublicLayout()) {
115                 inputPermissionsPublic = true;
116             }
117 
118             if (inputPermissionsPublic) {
119                 sb.append(StringPool.AMPERSAND);
120                 sb.append(renderResponse.getNamespace());
121                 sb.append("inputPermissionsPublic=1");
122             }
123 
124             pageContext.getOut().print(sb.toString());
125         }
126         catch (Exception e) {
127             throw new JspException(e);
128         }
129     }
130 
131 }