001    /**
002     * Copyright (c) 2000-2010 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.enterpriseadmin.action;
016    
017    import com.liferay.portal.NoSuchUserGroupException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.UserServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    
027    import javax.portlet.ActionRequest;
028    import javax.portlet.ActionResponse;
029    import javax.portlet.PortletConfig;
030    import javax.portlet.RenderRequest;
031    import javax.portlet.RenderResponse;
032    
033    import org.apache.struts.action.ActionForm;
034    import org.apache.struts.action.ActionForward;
035    import org.apache.struts.action.ActionMapping;
036    
037    /**
038     * @author Charles May
039     */
040    public class EditUserGroupAssignmentsAction extends PortletAction {
041    
042            public void processAction(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
048    
049                    try {
050                            if (cmd.equals("user_group_users")) {
051                                    updateUserGroupUsers(actionRequest);
052                            }
053    
054                            if (Validator.isNotNull(cmd)) {
055                                    String redirect = ParamUtil.getString(
056                                            actionRequest, "assignmentsRedirect");
057    
058                                    sendRedirect(actionRequest, actionResponse, redirect);
059                            }
060                    }
061                    catch (Exception e) {
062                            if (e instanceof NoSuchUserGroupException ||
063                                    e instanceof PrincipalException) {
064    
065                                    SessionErrors.add(actionRequest, e.getClass().getName());
066    
067                                    setForward(actionRequest, "portlet.enterprise_admin.error");
068                            }
069                            else {
070                                    throw e;
071                            }
072                    }
073            }
074    
075            public ActionForward render(
076                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
077                            RenderRequest renderRequest, RenderResponse renderResponse)
078                    throws Exception {
079    
080                    try {
081                            ActionUtil.getUserGroup(renderRequest);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof NoSuchUserGroupException ||
085                                    e instanceof PrincipalException) {
086    
087                                    SessionErrors.add(renderRequest, e.getClass().getName());
088    
089                                    return mapping.findForward("portlet.enterprise_admin.error");
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095    
096                    return mapping.findForward(getForward(
097                            renderRequest,
098                            "portlet.enterprise_admin.edit_user_group_assignments"));
099            }
100    
101            protected void updateUserGroupUsers(ActionRequest actionRequest)
102                    throws Exception {
103    
104                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
105    
106                    long[] addUserIds = StringUtil.split(
107                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
108                    long[] removeUserIds = StringUtil.split(
109                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
110    
111                    UserServiceUtil.addUserGroupUsers(userGroupId, addUserIds);
112                    UserServiceUtil.unsetUserGroupUsers(userGroupId, removeUserIds);
113            }
114    
115    }