1   /**
2    * Copyright (c) 2000-2008 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.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.tagext.TagSupport;
39  
40  /**
41   * <a href="InputPermissionsParamsTag.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class InputPermissionsParamsTag extends TagSupport {
47  
48      public int doEndTag() throws JspException {
49          try {
50              HttpServletRequest request =
51                  (HttpServletRequest)pageContext.getRequest();
52  
53              RenderResponse renderResponse =
54                  (RenderResponse)request.getAttribute(
55                      JavaConstants.JAVAX_PORTLET_RESPONSE);
56  
57              Layout layout = (Layout)request.getAttribute(WebKeys.LAYOUT);
58  
59              Group group = layout.getGroup();
60  
61              List<String> supportedActions =
62                  ResourceActionsUtil.getModelResourceActions(_modelName);
63              List<String> communityDefaultActions =
64                  ResourceActionsUtil.getModelResourceCommunityDefaultActions(
65                      _modelName);
66              List<String> guestDefaultActions =
67                  ResourceActionsUtil.getModelResourceGuestDefaultActions(
68                      _modelName);
69              List<String> guestUnsupportedActions =
70                  ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
71                      _modelName);
72  
73              StringBuilder sb = new StringBuilder();
74  
75              for (int i = 0; i < supportedActions.size(); i++) {
76                  String action = supportedActions.get(i);
77  
78                  boolean communityChecked = communityDefaultActions.contains(
79                      action);
80                  boolean guestChecked = guestDefaultActions.contains(action);
81                  boolean guestDisabled = guestUnsupportedActions.contains(
82                      action);
83  
84                  if (guestDisabled) {
85                      guestChecked = false;
86                  }
87  
88                  if (group.isCommunity() || group.isOrganization()) {
89                      if (communityChecked) {
90                          sb.append(StringPool.AMPERSAND);
91                          sb.append(renderResponse.getNamespace());
92                          sb.append("communityPermissions=");
93                          sb.append(action);
94                      }
95                  }
96  
97                  if (guestChecked) {
98                      sb.append(StringPool.AMPERSAND);
99                      sb.append(renderResponse.getNamespace());
100                     sb.append("guestPermissions=");
101                     sb.append(action);
102                 }
103             }
104 
105             pageContext.getOut().print(sb.toString());
106 
107             return EVAL_PAGE;
108         }
109         catch (Exception e) {
110             throw new JspException(e);
111         }
112     }
113 
114     public void setModelName(String modelName) {
115         _modelName = modelName;
116     }
117 
118     private String _modelName;
119 
120 }