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