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.messageboards.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.struts.PortletAction;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.messageboards.service.MBBanServiceUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionMapping;
32  
33  /**
34   * <a href="BanUserAction.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Michael Young
37   */
38  public class BanUserAction extends PortletAction {
39  
40      public void processAction(
41              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
42              ActionRequest actionRequest, ActionResponse actionResponse)
43          throws Exception {
44  
45          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
46  
47          try {
48              if (cmd.equals("ban")) {
49                  banUser(actionRequest);
50              }
51              else if (cmd.equals("unban")) {
52                  unbanUser(actionRequest);
53              }
54  
55              sendRedirect(actionRequest, actionResponse);
56          }
57          catch (Exception e) {
58              if (e instanceof PrincipalException) {
59                  SessionErrors.add(actionRequest, e.getClass().getName());
60  
61                  setForward(actionRequest, "portlet.message_boards.error");
62              }
63              else {
64                  throw e;
65              }
66          }
67      }
68  
69      protected void banUser(ActionRequest actionRequest) throws Exception {
70          Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
71  
72          long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
73  
74          MBBanServiceUtil.addBan(layout.getPlid(), banUserId);
75      }
76  
77      protected void unbanUser(ActionRequest actionRequest) throws Exception {
78          Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
79  
80          long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
81  
82          MBBanServiceUtil.deleteBan(layout.getPlid(), banUserId);
83      }
84  
85  }