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.Layout;
29  import com.liferay.portal.security.permission.ResourceActionsUtil;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.util.List;
33  
34  import javax.portlet.RenderResponse;
35  
36  import javax.servlet.http.HttpServletRequest;
37  import javax.servlet.jsp.JspException;
38  import javax.servlet.jsp.PageContext;
39  
40  /**
41   * <a href="InputPermissionsParamsTagUtil.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Chan
45   * @author Jorge Ferrer
46   */
47  public class InputPermissionsParamsTagUtil {
48  
49      public static void doEndTag(String modelName, PageContext pageContext)
50          throws JspException {
51  
52          try {
53              HttpServletRequest request =
54                  (HttpServletRequest)pageContext.getRequest();
55  
56              RenderResponse renderResponse =
57                  (RenderResponse)request.getAttribute(
58                      JavaConstants.JAVAX_PORTLET_RESPONSE);
59  
60              Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
61  
62              Group group = layout.getGroup();
63  
64              List<String> supportedActions =
65                  ResourceActionsUtil.getModelResourceActions(modelName);
66              List<String> communityDefaultActions =
67                  ResourceActionsUtil.getModelResourceCommunityDefaultActions(
68                      modelName);
69              List<String> guestDefaultActions =
70                  ResourceActionsUtil.getModelResourceGuestDefaultActions(
71                      modelName);
72              List<String> guestUnsupportedActions =
73                  ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
74                      modelName);
75  
76              StringBuilder sb = new StringBuilder();
77  
78              for (int i = 0; i < supportedActions.size(); i++) {
79                  String action = supportedActions.get(i);
80  
81                  boolean communityChecked = communityDefaultActions.contains(
82                      action);
83                  boolean guestChecked = guestDefaultActions.contains(action);
84                  boolean guestDisabled = guestUnsupportedActions.contains(
85                      action);
86  
87                  if (guestDisabled) {
88                      guestChecked = false;
89                  }
90  
91                  if (group.isCommunity() || group.isOrganization()) {
92                      if (communityChecked) {
93                          sb.append(StringPool.AMPERSAND);
94                          sb.append(renderResponse.getNamespace());
95                          sb.append("communityPermissions=");
96                          sb.append(action);
97                      }
98                  }
99  
100                 if (guestChecked) {
101                     sb.append(StringPool.AMPERSAND);
102                     sb.append(renderResponse.getNamespace());
103                     sb.append("guestPermissions=");
104                     sb.append(action);
105                 }
106             }
107 
108             boolean inputPermissionsPublic = false;
109 
110             if (layout.isPublicLayout()) {
111                 inputPermissionsPublic = true;
112             }
113 
114             if (inputPermissionsPublic) {
115                 sb.append(StringPool.AMPERSAND);
116                 sb.append(renderResponse.getNamespace());
117                 sb.append("inputPermissionsPublic=1");
118             }
119 
120             pageContext.getOut().print(sb.toString());
121         }
122         catch (Exception e) {
123             throw new JspException(e);
124         }
125     }
126 
127 }