1   /**
2    * Copyright (c) 2000-2009 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.portlet.communities.util;
24  
25  import com.liferay.portal.events.EventsProcessorUtil;
26  import com.liferay.portal.kernel.configuration.Filter;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.model.Layout;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.security.permission.ActionKeys;
33  import com.liferay.portal.security.permission.PermissionChecker;
34  import com.liferay.portal.service.LayoutLocalServiceUtil;
35  import com.liferay.portal.service.LayoutServiceUtil;
36  import com.liferay.portal.service.permission.GroupPermissionUtil;
37  import com.liferay.portal.service.permission.LayoutPermissionUtil;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.PropsKeys;
41  import com.liferay.portal.util.PropsUtil;
42  import com.liferay.portal.util.WebKeys;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  /**
53   * <a href="CommunitiesUtil.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Raymond Augé
56   */
57  public class CommunitiesUtil {
58  
59      public static void deleteLayout(
60              ActionRequest actionRequest, ActionResponse actionResponse)
61          throws Exception {
62  
63          HttpServletRequest request = PortalUtil.getHttpServletRequest(
64              actionRequest);
65          HttpServletResponse response = PortalUtil.getHttpServletResponse(
66              actionResponse);
67  
68          deleteLayout(request, response);
69      }
70  
71      public static void deleteLayout(
72              RenderRequest renderRequest, RenderResponse renderResponse)
73          throws Exception {
74  
75          HttpServletRequest request = PortalUtil.getHttpServletRequest(
76              renderRequest);
77          HttpServletResponse response = PortalUtil.getHttpServletResponse(
78              renderResponse);
79  
80          deleteLayout(request, response);
81      }
82  
83      public static void deleteLayout(
84              HttpServletRequest request, HttpServletResponse response)
85          throws Exception {
86  
87          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
88              WebKeys.THEME_DISPLAY);
89  
90          PermissionChecker permissionChecker =
91              themeDisplay.getPermissionChecker();
92  
93          long plid = ParamUtil.getLong(request, "plid");
94  
95          long groupId = ParamUtil.getLong(request, "groupId");
96          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
97          long layoutId = ParamUtil.getLong(request, "layoutId");
98  
99          Layout layout = null;
100 
101         if (plid <= 0) {
102             layout = LayoutLocalServiceUtil.getLayout(
103                 groupId, privateLayout, layoutId);
104         }
105         else {
106             layout = LayoutLocalServiceUtil.getLayout(plid);
107 
108             groupId = layout.getGroupId();
109             privateLayout = layout.isPrivateLayout();
110             layoutId = layout.getLayoutId();
111         }
112 
113         Group group = layout.getGroup();
114 
115         if (group.isStagingGroup() &&
116             !GroupPermissionUtil.contains(
117                 permissionChecker, groupId, ActionKeys.MANAGE_STAGING) &&
118             !GroupPermissionUtil.contains(
119                 permissionChecker, groupId, ActionKeys.PUBLISH_STAGING)) {
120 
121             throw new PrincipalException();
122         }
123 
124         if (LayoutPermissionUtil.contains(
125                 permissionChecker, groupId, privateLayout, layoutId,
126                 ActionKeys.DELETE)) {
127 
128             String[] eventClasses = StringUtil.split(
129                 PropsUtil.get(
130                     PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE,
131                     new Filter(layout.getType())));
132 
133             EventsProcessorUtil.process(
134                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE, eventClasses,
135                 request, response);
136         }
137 
138         LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
139     }
140 
141 }