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