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   */
50  public class InputPermissionsParamsTagUtil {
51  
52      public static void doEndTag(String modelName, PageContext pageContext)
53          throws JspException {
54  
55          try {
56              HttpServletRequest request =
57                  (HttpServletRequest)pageContext.getRequest();
58  
59              RenderResponse renderResponse =
60                  (RenderResponse)request.getAttribute(
61                      JavaConstants.JAVAX_PORTLET_RESPONSE);
62  
63              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
64                  WebKeys.THEME_DISPLAY);
65  
66              Layout layout = themeDisplay.getLayout();
67  
68              Group group = themeDisplay.getScopeGroup();
69              Group layoutGroup = layout.getGroup();
70  
71              List<String> supportedActions =
72                  ResourceActionsUtil.getModelResourceActions(modelName);
73              List<String> communityDefaultActions =
74                  ResourceActionsUtil.getModelResourceCommunityDefaultActions(
75                      modelName);
76              List<String> guestDefaultActions =
77                  ResourceActionsUtil.getModelResourceGuestDefaultActions(
78                      modelName);
79              List<String> guestUnsupportedActions =
80                  ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
81                      modelName);
82  
83              StringBuilder sb = new StringBuilder();
84  
85              for (int i = 0; i < supportedActions.size(); i++) {
86                  String action = supportedActions.get(i);
87  
88                  boolean communityChecked = communityDefaultActions.contains(
89                      action);
90                  boolean guestChecked = guestDefaultActions.contains(action);
91                  boolean guestDisabled = guestUnsupportedActions.contains(
92                      action);
93  
94                  if (guestDisabled) {
95                      guestChecked = false;
96                  }
97  
98                  if (group.isCommunity() || group.isOrganization()) {
99                      if (communityChecked) {
100                         sb.append(StringPool.AMPERSAND);
101                         sb.append(renderResponse.getNamespace());
102                         sb.append("communityPermissions=");
103                         sb.append(action);
104                     }
105                 }
106 
107                 if (guestChecked) {
108                     sb.append(StringPool.AMPERSAND);
109                     sb.append(renderResponse.getNamespace());
110                     sb.append("guestPermissions=");
111                     sb.append(action);
112                 }
113             }
114 
115             boolean inputPermissionsPublic = false;
116 
117             if (layoutGroup.getName().equals(GroupConstants.CONTROL_PANEL)) {
118                 if (!group.hasPrivateLayouts()) {
119                     inputPermissionsPublic = true;
120                 }
121             }
122             else if (layout.isPublicLayout()) {
123                 inputPermissionsPublic = true;
124             }
125 
126             if (inputPermissionsPublic) {
127                 sb.append(StringPool.AMPERSAND);
128                 sb.append(renderResponse.getNamespace());
129                 sb.append("inputPermissionsPublic=1");
130             }
131 
132             pageContext.getOut().print(sb.toString());
133         }
134         catch (Exception e) {
135             throw new JspException(e);
136         }
137     }
138 
139 }