1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.servlet.taglib.ui;
24  
25  import com.liferay.portal.kernel.util.JavaConstants;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.model.Group;
28  import com.liferay.portal.model.GroupConstants;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.security.permission.ResourceActionsUtil;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portal.util.WebKeys;
33  
34  import java.util.List;
35  
36  import javax.portlet.RenderResponse;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.servlet.jsp.JspException;
40  import javax.servlet.jsp.PageContext;
41  
42  /**
43   * <a href="InputPermissionsParamsTagUtil.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Brian Chan
47   * @author Jorge Ferrer
48   */
49  public class InputPermissionsParamsTagUtil {
50  
51      public static void doEndTag(String modelName, PageContext pageContext)
52          throws JspException {
53  
54          try {
55              HttpServletRequest request =
56                  (HttpServletRequest)pageContext.getRequest();
57  
58              RenderResponse renderResponse =
59                  (RenderResponse)request.getAttribute(
60                      JavaConstants.JAVAX_PORTLET_RESPONSE);
61  
62              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
63                  WebKeys.THEME_DISPLAY);
64  
65              Layout layout = themeDisplay.getLayout();
66  
67              Group group = themeDisplay.getScopeGroup();
68              Group layoutGroup = layout.getGroup();
69  
70              List<String> supportedActions =
71                  ResourceActionsUtil.getModelResourceActions(modelName);
72              List<String> communityDefaultActions =
73                  ResourceActionsUtil.getModelResourceCommunityDefaultActions(
74                      modelName);
75              List<String> guestDefaultActions =
76                  ResourceActionsUtil.getModelResourceGuestDefaultActions(
77                      modelName);
78              List<String> guestUnsupportedActions =
79                  ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
80                      modelName);
81  
82              StringBuilder sb = new StringBuilder();
83  
84              for (int i = 0; i < supportedActions.size(); i++) {
85                  String action = supportedActions.get(i);
86  
87                  boolean communityChecked = communityDefaultActions.contains(
88                      action);
89                  boolean guestChecked = guestDefaultActions.contains(action);
90                  boolean guestDisabled = guestUnsupportedActions.contains(
91                      action);
92  
93                  if (guestDisabled) {
94                      guestChecked = false;
95                  }
96  
97                  if (group.isCommunity() || group.isOrganization()) {
98                      if (communityChecked) {
99                          sb.append(StringPool.AMPERSAND);
100                         sb.append(renderResponse.getNamespace());
101                         sb.append("communityPermissions=");
102                         sb.append(action);
103                     }
104                 }
105 
106                 if (guestChecked) {
107                     sb.append(StringPool.AMPERSAND);
108                     sb.append(renderResponse.getNamespace());
109                     sb.append("guestPermissions=");
110                     sb.append(action);
111                 }
112             }
113 
114             boolean inputPermissionsPublic = false;
115 
116             if (layoutGroup.getName().equals(GroupConstants.CONTROL_PANEL)) {
117                 if (!group.hasPrivateLayouts()) {
118                     inputPermissionsPublic = true;
119                 }
120             }
121             else if (layout.isPublicLayout()) {
122                 inputPermissionsPublic = true;
123             }
124 
125             if (inputPermissionsPublic) {
126                 sb.append(StringPool.AMPERSAND);
127                 sb.append(renderResponse.getNamespace());
128                 sb.append("inputPermissionsPublic=1");
129             }
130 
131             pageContext.getOut().print(sb.toString());
132         }
133         catch (Exception e) {
134             throw new JspException(e);
135         }
136     }
137 
138 }