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