001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
042                            PortletConfig portletConfig, ActionRequest actionRequest,
043                            ActionResponse actionResponse)
044                    throws Exception {
045    
046                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
047    
048                    try {
049                            if (cmd.equals("ban")) {
050                                    banUser(actionRequest);
051                            }
052                            else if (cmd.equals("unban")) {
053                                    unbanUser(actionRequest);
054                            }
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof PrincipalException) {
060                                    SessionErrors.add(actionRequest, e.getClass());
061    
062                                    setForward(actionRequest, "portlet.message_boards.error");
063                            }
064                            else {
065                                    throw e;
066                            }
067                    }
068            }
069    
070            protected void banUser(ActionRequest actionRequest) throws Exception {
071                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
072    
073                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
074                            MBBan.class.getName(), actionRequest);
075    
076                    MBBanServiceUtil.addBan(banUserId, serviceContext);
077            }
078    
079            protected void unbanUser(ActionRequest actionRequest) throws Exception {
080                    long banUserId = ParamUtil.getLong(actionRequest, "banUserId");
081    
082                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
083                            MBBan.class.getName(), actionRequest);
084    
085                    MBBanServiceUtil.deleteBan(banUserId, serviceContext);
086            }
087    
088    }