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.usersadmin.action;
016    
017    import com.liferay.portal.NoSuchOrganizationException;
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.model.Organization;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.security.membershippolicy.MembershipPolicyException;
026    import com.liferay.portal.service.OrganizationLocalServiceUtil;
027    import com.liferay.portal.service.UserGroupServiceUtil;
028    import com.liferay.portal.service.UserServiceUtil;
029    import com.liferay.portal.struts.PortletAction;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class EditOrganizationAssignmentsAction extends PortletAction {
045    
046            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
054    
055                    try {
056                            if (cmd.equals("organization_user_groups")) {
057                                    updateOrganizationUserGroups(actionRequest);
058                            }
059                            else if (cmd.equals("organization_users")) {
060                                    updateOrganizationUsers(actionRequest);
061                            }
062    
063                            if (Validator.isNotNull(cmd)) {
064                                    String redirect = ParamUtil.getString(
065                                            actionRequest, "assignmentsRedirect");
066    
067                                    sendRedirect(actionRequest, actionResponse, redirect);
068                            }
069                    }
070                    catch (Exception e) {
071                            if (e instanceof MembershipPolicyException) {
072                                    SessionErrors.add(actionRequest, e.getClass(), e);
073                            }
074                            else if (e instanceof NoSuchOrganizationException ||
075                                             e instanceof PrincipalException) {
076    
077                                    SessionErrors.add(actionRequest, e.getClass());
078    
079                                    setForward(actionRequest, "portlet.users_admin.error");
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085            }
086    
087            @Override
088            public ActionForward render(
089                            ActionMapping actionMapping, ActionForm actionForm,
090                            PortletConfig portletConfig, RenderRequest renderRequest,
091                            RenderResponse renderResponse)
092                    throws Exception {
093    
094                    try {
095                            ActionUtil.getOrganization(renderRequest);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof NoSuchOrganizationException ||
099                                    e instanceof PrincipalException) {
100    
101                                    SessionErrors.add(renderRequest, e.getClass());
102    
103                                    return actionMapping.findForward("portlet.users_admin.error");
104                            }
105                            else {
106                                    throw e;
107                            }
108                    }
109    
110                    return actionMapping.findForward(
111                            getForward(
112                                    renderRequest,
113                                    "portlet.users_admin.edit_organization_assignments"));
114            }
115    
116            protected void updateOrganizationUserGroups(ActionRequest actionRequest)
117                    throws Exception {
118    
119                    long organizationId = ParamUtil.getLong(
120                            actionRequest, "organizationId");
121    
122                    Organization organization =
123                            OrganizationLocalServiceUtil.getOrganization(organizationId);
124    
125                    long groupId = organization.getGroupId();
126    
127                    long[] addUserGroupIds = StringUtil.split(
128                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
129                    long[] removeUserGroupIds = StringUtil.split(
130                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
131    
132                    UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
133                    UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
134            }
135    
136            protected void updateOrganizationUsers(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    long organizationId = ParamUtil.getLong(
140                            actionRequest, "organizationId");
141    
142                    long[] addUserIds = StringUtil.split(
143                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
144                    long[] removeUserIds = StringUtil.split(
145                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
146    
147                    UserServiceUtil.addOrganizationUsers(organizationId, addUserIds);
148                    UserServiceUtil.unsetOrganizationUsers(organizationId, removeUserIds);
149            }
150    
151    }