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.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.JavaConstants;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.model.Group;
23  import com.liferay.portal.model.Layout;
24  import com.liferay.portal.model.Role;
25  import com.liferay.portal.model.RoleConstants;
26  import com.liferay.portal.security.permission.ActionKeys;
27  import com.liferay.portal.security.permission.ResourceActionsUtil;
28  import com.liferay.portal.service.GroupLocalServiceUtil;
29  import com.liferay.portal.service.RoleLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  
33  import java.util.List;
34  
35  import javax.portlet.RenderResponse;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.jsp.JspException;
39  import javax.servlet.jsp.PageContext;
40  
41  /**
42   * <a href="InputPermissionsParamsTagUtil.java.html"><b><i>View Source</i></b>
43   * </a>
44   *
45   * @author Brian Chan
46   * @author Jorge Ferrer
47   */
48  public class InputPermissionsParamsTagUtil {
49  
50      public static String getDefaultViewRole(
51              String modelName, ThemeDisplay themeDisplay)
52          throws PortalException, SystemException {
53  
54          Layout layout = themeDisplay.getLayout();
55  
56          Group layoutGroup = layout.getGroup();
57  
58          List<String> guestDefaultActions =
59              ResourceActionsUtil.getModelResourceGuestDefaultActions(
60                  modelName);
61  
62          if (layoutGroup.isControlPanel()) {
63              Group group = themeDisplay.getScopeGroup();
64  
65              if (!group.hasPrivateLayouts() &&
66                  guestDefaultActions.contains(ActionKeys.VIEW)) {
67  
68                  return RoleConstants.GUEST;
69              }
70          }
71          else if (layout.isPublicLayout() &&
72                   guestDefaultActions.contains(ActionKeys.VIEW)) {
73  
74              return RoleConstants.GUEST;
75          }
76          else {
77              Group parentGroup = GroupLocalServiceUtil.getGroup(
78                  themeDisplay.getParentGroupId());
79  
80              Role defaultGroupRole = RoleLocalServiceUtil.getDefaultGroupRole(
81                  parentGroup.getGroupId());
82  
83              return defaultGroupRole.getName();
84          }
85  
86          return StringPool.BLANK;
87      }
88  
89      public static void doEndTag(String modelName, PageContext pageContext)
90          throws JspException {
91  
92          try {
93              HttpServletRequest request =
94                  (HttpServletRequest)pageContext.getRequest();
95  
96              RenderResponse renderResponse =
97                  (RenderResponse)request.getAttribute(
98                      JavaConstants.JAVAX_PORTLET_RESPONSE);
99  
100             ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
101                 WebKeys.THEME_DISPLAY);
102 
103             Group group = themeDisplay.getScopeGroup();
104 
105             List<String> supportedActions =
106                 ResourceActionsUtil.getModelResourceActions(modelName);
107             List<String> communityDefaultActions =
108                 ResourceActionsUtil.getModelResourceCommunityDefaultActions(
109                     modelName);
110             List<String> guestDefaultActions =
111                 ResourceActionsUtil.getModelResourceGuestDefaultActions(
112                     modelName);
113             List<String> guestUnsupportedActions =
114                 ResourceActionsUtil.getModelResourceGuestUnsupportedActions(
115                     modelName);
116 
117             StringBundler sb = new StringBundler();
118 
119             for (int i = 0; i < supportedActions.size(); i++) {
120                 String action = supportedActions.get(i);
121 
122                 boolean communityChecked = communityDefaultActions.contains(
123                     action);
124                 boolean guestChecked = guestDefaultActions.contains(action);
125                 boolean guestDisabled = guestUnsupportedActions.contains(
126                     action);
127 
128                 if (guestDisabled) {
129                     guestChecked = false;
130                 }
131 
132                 if (group.isCommunity() || group.isOrganization()) {
133                     if (communityChecked) {
134                         sb.append(StringPool.AMPERSAND);
135                         sb.append(renderResponse.getNamespace());
136                         sb.append("communityPermissions=");
137                         sb.append(action);
138                     }
139                 }
140 
141                 if (guestChecked) {
142                     sb.append(StringPool.AMPERSAND);
143                     sb.append(renderResponse.getNamespace());
144                     sb.append("guestPermissions=");
145                     sb.append(action);
146                 }
147             }
148 
149             String inputPermissionsViewRole = getDefaultViewRole(
150                 modelName, themeDisplay);
151 
152             sb.append(StringPool.AMPERSAND);
153             sb.append(renderResponse.getNamespace());
154             sb.append("inputPermissionsViewRole=");
155             sb.append(inputPermissionsViewRole);
156 
157             pageContext.getOut().print(sb.toString());
158         }
159         catch (Exception e) {
160             throw new JspException(e);
161         }
162     }
163 
164 }