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.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.PrincipalException;
024    import com.liferay.portal.security.membershippolicy.MembershipPolicyException;
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 actionMapping, ActionForm actionForm,
046                            PortletConfig portletConfig, ActionRequest actionRequest,
047                            ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals("user_group_users")) {
054                                    updateUserGroupUsers(actionRequest);
055                            }
056    
057                            if (Validator.isNotNull(cmd)) {
058                                    String redirect = ParamUtil.getString(
059                                            actionRequest, "assignmentsRedirect");
060    
061                                    sendRedirect(actionRequest, actionResponse, redirect);
062                            }
063                    }
064                    catch (Exception e) {
065                            if (e instanceof MembershipPolicyException) {
066                                    SessionErrors.add(actionRequest, e.getClass(), e);
067                            }
068                            else if (e instanceof NoSuchUserGroupException ||
069                                             e instanceof PrincipalException) {
070    
071                                    SessionErrors.add(actionRequest, e.getClass());
072    
073                                    setForward(actionRequest, "portlet.user_groups_admin.error");
074                            }
075                            else {
076                                    throw e;
077                            }
078                    }
079            }
080    
081            @Override
082            public ActionForward render(
083                            ActionMapping actionMapping, ActionForm actionForm,
084                            PortletConfig portletConfig, RenderRequest renderRequest,
085                            RenderResponse renderResponse)
086                    throws Exception {
087    
088                    try {
089                            ActionUtil.getUserGroup(renderRequest);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof NoSuchUserGroupException ||
093                                    e instanceof PrincipalException) {
094    
095                                    SessionErrors.add(renderRequest, e.getClass());
096    
097                                    return actionMapping.findForward(
098                                            "portlet.user_groups_admin.error");
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104    
105                    return actionMapping.findForward(
106                            getForward(
107                                    renderRequest,
108                                    "portlet.user_groups_admin.edit_user_group_assignments"));
109            }
110    
111            protected void updateUserGroupUsers(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
115    
116                    long[] addUserIds = StringUtil.split(
117                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
118                    long[] removeUserIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
120    
121                    UserServiceUtil.addUserGroupUsers(userGroupId, addUserIds);
122                    UserServiceUtil.unsetUserGroupUsers(userGroupId, removeUserIds);
123            }
124    
125    }