001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.ServiceContextFactory;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portlet.messageboards.model.MBBan;
025    import com.liferay.portlet.messageboards.service.MBBanServiceUtil;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionMapping;
033    
034    /**
035     * @author Michael Young
036     */
037    public class BanUserAction extends PortletAction {
038    
039            @Override
040            public void processAction(
041                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
042                            ActionRequest actionRequest, ActionResponse actionResponse)
043                    throws Exception {
044    
045                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
046    
047                    try {
048                            if (cmd.equals("ban")) {
049                                    banUser(actionRequest);
050                            }
051                            else if (cmd.equals("unban")) {
052                                    unbanUser(actionRequest);
053                            }
054    
055                            sendRedirect(actionRequest, actionResponse);
056                    }
057                    catch (Exception e) {
058                            if (e instanceof PrincipalException) {
059                                    SessionErrors.add(actionRequest, e.getClass().getName());
060    
061                                    setForward(actionRequest, "portlet.message_boards.error");
062                            }
063                            else {
064                                    throw e;
065                            }
066                    }
067            }
068    
069            protected void banUser(ActionRequest actionRequest) throws Exception {
070                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
071    
072                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
073                            MBBan.class.getName(), actionRequest);
074    
075                    MBBanServiceUtil.addBan(banUserId, serviceContext);
076            }
077    
078            protected void unbanUser(ActionRequest actionRequest) throws Exception {
079                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
080    
081                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
082                            MBBan.class.getName(), actionRequest);
083    
084                    MBBanServiceUtil.deleteBan(banUserId, serviceContext);
085            }
086    
087    }