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