com.liferay.portlet.sites.action.EditGroupAssignmentsAction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of portal-impl Show documentation
Show all versions of portal-impl Show documentation
Contains implementation for the portal services.
/**
* Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.portlet.sites.action;
import com.liferay.portal.NoSuchGroupException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ArrayUtil;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.liveusers.LiveUsers;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.security.membershippolicy.MembershipPolicyException;
import com.liferay.portal.service.OrganizationServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.ServiceContextFactory;
import com.liferay.portal.service.UserGroupGroupRoleServiceUtil;
import com.liferay.portal.service.UserGroupRoleServiceUtil;
import com.liferay.portal.service.UserGroupServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.UserServiceUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import java.util.HashSet;
import java.util.Set;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* @author Brian Wing Shun Chan
*/
public class EditGroupAssignmentsAction extends PortletAction {
@Override
public void processAction(
ActionMapping actionMapping, ActionForm actionForm,
PortletConfig portletConfig, ActionRequest actionRequest,
ActionResponse actionResponse)
throws Exception {
String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
try {
if (cmd.equals("group_organizations")) {
updateGroupOrganizations(actionRequest);
}
else if (cmd.equals("group_user_groups")) {
updateGroupUserGroups(actionRequest);
}
else if (cmd.equals("group_users")) {
updateGroupUsers(actionRequest);
}
else if (cmd.equals("user_group_group_role")) {
updateUserGroupGroupRole(actionRequest);
}
else if (cmd.equals("user_group_role")) {
updateUserGroupRole(actionRequest);
}
if (Validator.isNotNull(cmd)) {
String redirect = ParamUtil.getString(
actionRequest, "assignmentsRedirect");
if (Validator.isNull(redirect)) {
redirect = ParamUtil.getString(actionRequest, "redirect");
}
sendRedirect(actionRequest, actionResponse, redirect);
}
}
catch (Exception e) {
if (e instanceof MembershipPolicyException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else if (e instanceof NoSuchGroupException ||
e instanceof PrincipalException) {
SessionErrors.add(actionRequest, e.getClass());
setForward(actionRequest, "portlet.sites_admin.error");
}
else {
throw e;
}
}
}
@Override
public ActionForward render(
ActionMapping actionMapping, ActionForm actionForm,
PortletConfig portletConfig, RenderRequest renderRequest,
RenderResponse renderResponse)
throws Exception {
try {
ActionUtil.getGroup(renderRequest);
}
catch (Exception e) {
if (e instanceof NoSuchGroupException ||
e instanceof PrincipalException) {
SessionErrors.add(renderRequest, e.getClass());
return actionMapping.findForward("portlet.sites_admin.error");
}
else {
throw e;
}
}
return actionMapping.findForward(
getForward(
renderRequest, "portlet.sites_admin.edit_site_assignments"));
}
protected long[] filterAddUserIds(long groupId, long[] userIds)
throws Exception {
Set filteredUserIds = new HashSet(userIds.length);
for (long userId : userIds) {
if (!UserLocalServiceUtil.hasGroupUser(groupId, userId)) {
filteredUserIds.add(userId);
}
}
return ArrayUtil.toArray(
filteredUserIds.toArray(new Long[filteredUserIds.size()]));
}
protected long[] filterRemoveUserIds(long groupId, long[] userIds)
throws Exception {
Set filteredUserIds = new HashSet(userIds.length);
for (long userId : userIds) {
if (UserLocalServiceUtil.hasGroupUser(groupId, userId)) {
filteredUserIds.add(userId);
}
}
return ArrayUtil.toArray(
filteredUserIds.toArray(new Long[filteredUserIds.size()]));
}
protected void updateGroupOrganizations(ActionRequest actionRequest)
throws Exception {
long groupId = ParamUtil.getLong(actionRequest, "groupId");
long[] addOrganizationIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addOrganizationIds"), 0L);
long[] removeOrganizationIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeOrganizationIds"), 0L);
OrganizationServiceUtil.addGroupOrganizations(
groupId, addOrganizationIds);
OrganizationServiceUtil.unsetGroupOrganizations(
groupId, removeOrganizationIds);
}
protected void updateGroupUserGroups(ActionRequest actionRequest)
throws Exception {
long groupId = ParamUtil.getLong(actionRequest, "groupId");
long[] addUserGroupIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
long[] removeUserGroupIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
UserGroupServiceUtil.addGroupUserGroups(groupId, addUserGroupIds);
UserGroupServiceUtil.unsetGroupUserGroups(groupId, removeUserGroupIds);
}
protected void updateGroupUsers(ActionRequest actionRequest)
throws Exception {
ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
WebKeys.THEME_DISPLAY);
long groupId = ParamUtil.getLong(actionRequest, "groupId");
long[] addUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addUserIds"), 0L);
addUserIds = filterAddUserIds(groupId, addUserIds);
long[] removeUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
removeUserIds = filterRemoveUserIds(groupId, removeUserIds);
ServiceContext serviceContext = ServiceContextFactory.getInstance(
actionRequest);
UserServiceUtil.addGroupUsers(groupId, addUserIds, serviceContext);
UserServiceUtil.unsetGroupUsers(groupId, removeUserIds, serviceContext);
LiveUsers.joinGroup(themeDisplay.getCompanyId(), groupId, addUserIds);
LiveUsers.leaveGroup(
themeDisplay.getCompanyId(), groupId, removeUserIds);
}
protected void updateUserGroupGroupRole(ActionRequest actionRequest)
throws Exception {
long groupId = ParamUtil.getLong(actionRequest, "groupId");
long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
long[] addRoleIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
long[] removeRoleIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
UserGroupGroupRoleServiceUtil.addUserGroupGroupRoles(
userGroupId, groupId, addRoleIds);
UserGroupGroupRoleServiceUtil.deleteUserGroupGroupRoles(
userGroupId, groupId, removeRoleIds);
}
protected void updateUserGroupRole(ActionRequest actionRequest)
throws Exception {
User user = PortalUtil.getSelectedUser(actionRequest, false);
if (user == null) {
return;
}
long groupId = ParamUtil.getLong(actionRequest, "groupId");
long[] addRoleIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addRoleIds"), 0L);
long[] removeRoleIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeRoleIds"), 0L);
UserGroupRoleServiceUtil.addUserGroupRoles(
user.getUserId(), groupId, addRoleIds);
UserGroupRoleServiceUtil.deleteUserGroupRoles(
user.getUserId(), groupId, removeRoleIds);
}
}