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.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  }