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