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   */
58  public class CommunitiesUtil {
59  
60      public static void deleteLayout(
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          HttpServletRequest request = PortalUtil.getHttpServletRequest(
65              actionRequest);
66          HttpServletResponse response = PortalUtil.getHttpServletResponse(
67              actionResponse);
68  
69          deleteLayout(request, response);
70      }
71  
72      public static void deleteLayout(
73              RenderRequest renderRequest, RenderResponse renderResponse)
74          throws Exception {
75  
76          HttpServletRequest request = PortalUtil.getHttpServletRequest(
77              renderRequest);
78          HttpServletResponse response = PortalUtil.getHttpServletResponse(
79              renderResponse);
80  
81          deleteLayout(request, response);
82      }
83  
84      public static void deleteLayout(
85              HttpServletRequest request, HttpServletResponse response)
86          throws Exception {
87  
88          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
89              WebKeys.THEME_DISPLAY);
90  
91          PermissionChecker permissionChecker =
92              themeDisplay.getPermissionChecker();
93  
94          long plid = ParamUtil.getLong(request, "plid");
95  
96          long groupId = ParamUtil.getLong(request, "groupId");
97          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
98          long layoutId = ParamUtil.getLong(request, "layoutId");
99  
100         Layout layout = null;
101 
102         if (plid <= 0) {
103             layout = LayoutLocalServiceUtil.getLayout(
104                 groupId, privateLayout, layoutId);
105         }
106         else {
107             layout = LayoutLocalServiceUtil.getLayout(plid);
108 
109             groupId = layout.getGroupId();
110             privateLayout = layout.isPrivateLayout();
111             layoutId = layout.getLayoutId();
112         }
113 
114         Group group = layout.getGroup();
115 
116         if (group.isStagingGroup() &&
117             !GroupPermissionUtil.contains(
118                 permissionChecker, groupId, ActionKeys.MANAGE_STAGING) &&
119             !GroupPermissionUtil.contains(
120                 permissionChecker, groupId, ActionKeys.PUBLISH_STAGING)) {
121 
122             throw new PrincipalException();
123         }
124 
125         if (LayoutPermissionUtil.contains(
126                 permissionChecker, groupId, privateLayout, layoutId,
127                 ActionKeys.DELETE)) {
128 
129             String[] eventClasses = StringUtil.split(
130                 PropsUtil.get(
131                     PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE,
132                     new Filter(layout.getType())));
133 
134             EventsProcessorUtil.process(
135                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_DELETE, eventClasses,
136                 request, response);
137         }
138 
139         LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
140     }
141 
142 }