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