1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.communities.util;
16  
17  import com.liferay.portal.events.EventsProcessorUtil;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.PropsKeys;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.service.LayoutLocalServiceUtil;
26  import com.liferay.portal.service.LayoutServiceUtil;
27  import com.liferay.portal.service.permission.GroupPermissionUtil;
28  import com.liferay.portal.service.permission.LayoutPermissionUtil;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.LayoutSettings;
31  import com.liferay.portal.util.PortalUtil;
32  import com.liferay.portal.util.WebKeys;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import javax.servlet.http.HttpServletRequest;
40  import javax.servlet.http.HttpServletResponse;
41  
42  /**
43   * <a href="CommunitiesUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Raymond Augé
46   */
47  public class CommunitiesUtil {
48  
49      public static void deleteLayout(
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          HttpServletRequest request = PortalUtil.getHttpServletRequest(
54              actionRequest);
55          HttpServletResponse response = PortalUtil.getHttpServletResponse(
56              actionResponse);
57  
58          deleteLayout(request, response);
59      }
60  
61      public static void deleteLayout(
62              RenderRequest renderRequest, RenderResponse renderResponse)
63          throws Exception {
64  
65          HttpServletRequest request = PortalUtil.getHttpServletRequest(
66              renderRequest);
67          HttpServletResponse response = PortalUtil.getHttpServletResponse(
68              renderResponse);
69  
70          deleteLayout(request, response);
71      }
72  
73      public static void deleteLayout(
74              HttpServletRequest request, HttpServletResponse response)
75          throws Exception {
76  
77          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
78              WebKeys.THEME_DISPLAY);
79  
80          PermissionChecker permissionChecker =
81              themeDisplay.getPermissionChecker();
82  
83          long plid = ParamUtil.getLong(request, "plid");
84  
85          long groupId = ParamUtil.getLong(request, "groupId");
86          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
87          long layoutId = ParamUtil.getLong(request, "layoutId");
88  
89          Layout layout = null;
90  
91          if (plid <= 0) {
92              layout = LayoutLocalServiceUtil.getLayout(
93                  groupId, privateLayout, layoutId);
94          }
95          else {
96              layout = LayoutLocalServiceUtil.getLayout(plid);
97  
98              groupId = layout.getGroupId();
99              privateLayout = layout.isPrivateLayout();
100             layoutId = layout.getLayoutId();
101         }
102 
103         Group group = layout.getGroup();
104 
105         if (group.isStagingGroup() &&
106             !GroupPermissionUtil.contains(
107                 permissionChecker, groupId, ActionKeys.MANAGE_STAGING) &&
108             !GroupPermissionUtil.contains(
109                 permissionChecker, groupId, ActionKeys.PUBLISH_STAGING)) {
110 
111             throw new PrincipalException();
112         }
113 
114         if (LayoutPermissionUtil.contains(
115                 permissionChecker, groupId, privateLayout, layoutId,
116                 ActionKeys.DELETE)) {
117 
118             LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
119 
120             EventsProcessorUtil.process(
121                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE,
122                 layoutSettings.getConfigurationActionDelete(), request,
123                 response);
124         }
125 
126         LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
127     }
128 
129 }