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