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.sites.action;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.ArrayUtil;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.liveusers.LiveUsers;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.OrganizationServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
030    import com.liferay.portal.service.UserGroupGroupRoleServiceUtil;
031    import com.liferay.portal.service.UserGroupRoleServiceUtil;
032    import com.liferay.portal.service.UserGroupServiceUtil;
033    import com.liferay.portal.service.UserLocalServiceUtil;
034    import com.liferay.portal.service.UserServiceUtil;
035    import com.liferay.portal.struts.PortletAction;
036    import com.liferay.portal.theme.ThemeDisplay;
037    import com.liferay.portal.util.PortalUtil;
038    import com.liferay.portal.util.WebKeys;
039    
040    import java.util.HashSet;
041    import java.util.Set;
042    
043    import javax.portlet.ActionRequest;
044    import javax.portlet.ActionResponse;
045    import javax.portlet.PortletConfig;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Brian Wing Shun Chan
055     */
056    public class EditGroupAssignmentsAction extends PortletAction {
057    
058            @Override
059            public void processAction(
060                            ActionMapping actionMapping, ActionForm actionForm,
061                            PortletConfig portletConfig, ActionRequest actionRequest,
062                            ActionResponse actionResponse)
063                    throws Exception {
064    
065                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
066    
067                    try {
068                            if (cmd.equals("group_organizations")) {
069                                    updateGroupOrganizations(actionRequest);
070                            }
071                            else if (cmd.equals("group_user_groups")) {
072                                    updateGroupUserGroups(actionRequest);
073                            }
074                            else if (cmd.equals("group_users")) {
075                                    updateGroupUsers(actionRequest);
076                            }
077                            else if (cmd.equals("user_group_group_role")) {
078                                    updateUserGroupGroupRole(actionRequest);
079                            }
080                            else if (cmd.equals("user_group_role")) {
081                                    updateUserGroupRole(actionRequest);
082                            }
083    
084                            if (Validator.isNotNull(cmd)) {
085                                    String redirect = ParamUtil.getString(
086                                            actionRequest, "assignmentsRedirect");
087    
088                                    if (Validator.isNull(redirect)) {
089                                            redirect = ParamUtil.getString(actionRequest, "redirect");
090                                    }
091    
092                                    sendRedirect(actionRequest, actionResponse, redirect);
093                            }
094                    }
095                    catch (Exception e) {
096                            if (e instanceof NoSuchGroupException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass());
100    
101                                    setForward(actionRequest, "portlet.sites_admin.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107            }
108    
109            @Override
110            public ActionForward render(
111                            ActionMapping actionMapping, ActionForm actionForm,
112                            PortletConfig portletConfig, RenderRequest renderRequest,
113                            RenderResponse renderResponse)
114                    throws Exception {
115    
116                    try {
117                            ActionUtil.getGroup(renderRequest);
118                    }
119                    catch (Exception e) {
120                            if (e instanceof NoSuchGroupException ||
121                                    e instanceof PrincipalException) {
122    
123                                    SessionErrors.add(renderRequest, e.getClass());
124    
125                                    return actionMapping.findForward("portlet.sites_admin.error");
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131    
132                    return actionMapping.findForward(
133                            getForward(
134                                    renderRequest, "portlet.sites_admin.edit_site_assignments"));
135            }
136    
137            protected long[] filterAddUserIds(long groupId, long[] userIds)
138                    throws Exception {
139    
140                    Set<Long> filteredUserIds = new HashSet<Long>(userIds.length);
141    
142                    for (long userId : userIds) {
143                            if (!UserLocalServiceUtil.hasGroupUser(groupId, userId)) {
144                                    filteredUserIds.add(userId);
145                            }
146                    }
147    
148                    return ArrayUtil.toArray(
149                            filteredUserIds.toArray(new Long[filteredUserIds.size()]));
150            }
151    
152            protected long[] filterRemoveUserIds(long groupId, long[] userIds)
153                    throws Exception {
154    
155                    Set<Long> filteredUserIds = new HashSet<Long>(userIds.length);
156    
157                    for (long userId : userIds) {
158                            if (UserLocalServiceUtil.hasGroupUser(groupId, userId)) {
159                                    filteredUserIds.add(userId);
160                            }
161                    }
162    
163                    return ArrayUtil.toArray(
164                            filteredUserIds.toArray(new Long[filteredUserIds.size()]));
165            }
166    
167            protected void updateGroupOrganizations(ActionRequest actionRequest)
168                    throws Exception {
169    
170                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
171    
172                    long[] addOrganizationIds = StringUtil.split(
173                            ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
174                    long[] removeOrganizationIds = StringUtil.split(
175                            ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
176    
177                    OrganizationServiceUtil.addGroupOrganizations(
178                            groupId, addOrganizationIds);
179                    OrganizationServiceUtil.unsetGroupOrganizations(
180                            groupId, removeOrganizationIds);
181            }
182    
183            protected void updateGroupUserGroups(ActionRequest actionRequest)
184                    throws Exception {
185    
186                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
187    
188                    long[] addUserGroupIds = StringUtil.split(
189                            ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
190                    long[] removeUserGroupIds = StringUtil.split(
191                            ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
192    
193                    UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
194                    UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
195            }
196    
197            protected void updateGroupUsers(ActionRequest actionRequest)
198                    throws Exception {
199    
200                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
201                            WebKeys.THEME_DISPLAY);
202    
203                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
204    
205                    long[] addUserIds = StringUtil.split(
206                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
207    
208                    addUserIds = filterAddUserIds(groupId, addUserIds);
209    
210                    long[] removeUserIds = StringUtil.split(
211                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
212    
213                    removeUserIds = filterRemoveUserIds(groupId, removeUserIds);
214    
215                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
216                            actionRequest);
217    
218                    UserServiceUtil.addGroupUsers(groupId, addUserIds, serviceContext);
219                    UserServiceUtil.unsetGroupUsers(groupId, removeUserIds, serviceContext);
220    
221                    LiveUsers.joinGroup(themeDisplay.getCompanyId(), groupId, addUserIds);
222                    LiveUsers.leaveGroup(
223                            themeDisplay.getCompanyId(), groupId, removeUserIds);
224            }
225    
226            protected void updateUserGroupGroupRole(ActionRequest actionRequest)
227                    throws Exception {
228    
229                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
230    
231                    long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
232    
233                    long[] addRoleIds = StringUtil.split(
234                            ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
235                    long[] removeRoleIds = StringUtil.split(
236                            ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
237    
238                    UserGroupGroupRoleServiceUtil.addUserGroupGroupRoles(
239                            userGroupId, groupId, addRoleIds);
240                    UserGroupGroupRoleServiceUtil.deleteUserGroupGroupRoles(
241                            userGroupId, groupId, removeRoleIds);
242            }
243    
244            protected void updateUserGroupRole(ActionRequest actionRequest)
245                    throws Exception {
246    
247                    User user = PortalUtil.getSelectedUser(actionRequest, false);
248    
249                    if (user == null) {
250                            return;
251                    }
252    
253                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
254    
255                    long[] addRoleIds = StringUtil.split(
256                            ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
257                    long[] removeRoleIds = StringUtil.split(
258                            ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
259    
260                    UserGroupRoleServiceUtil.addUserGroupRoles(
261                            user.getUserId(), groupId, addRoleIds);
262                    UserGroupRoleServiceUtil.deleteUserGroupRoles(
263                            user.getUserId(), groupId, removeRoleIds);
264            }
265    
266    }