/**
* Copyright (c) 2000-present 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.users.admin.web.portlet.action;
import com.liferay.portal.kernel.exception.NoSuchOrganizationException;
import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocal;
import com.liferay.portal.kernel.messaging.proxy.ProxyModeThreadLocalCloseable;
import com.liferay.portal.kernel.portlet.bridges.mvc.BaseMVCActionCommand;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCActionCommand;
import com.liferay.portal.kernel.security.auth.PrincipalException;
import com.liferay.portal.kernel.security.membershippolicy.MembershipPolicyException;
import com.liferay.portal.kernel.service.UserService;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.users.admin.constants.UsersAdminPortletKeys;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Brian Wing Shun Chan
*/
@Component(
immediate = true,
property = {
"javax.portlet.name=" + UsersAdminPortletKeys.MY_ORGANIZATIONS,
"javax.portlet.name=" + UsersAdminPortletKeys.USERS_ADMIN,
"mvc.command.name=/users_admin/edit_organization_assignments"
},
service = MVCActionCommand.class
)
public class EditOrganizationAssignmentsMVCActionCommand
extends BaseMVCActionCommand {
@Override
protected void doProcessAction(
ActionRequest actionRequest, ActionResponse actionResponse)
throws Exception {
try {
updateOrganizationUsers(actionRequest);
String redirect = ParamUtil.getString(
actionRequest, "assignmentsRedirect");
sendRedirect(actionRequest, actionResponse, redirect);
}
catch (Exception e) {
if (e instanceof MembershipPolicyException) {
SessionErrors.add(actionRequest, e.getClass(), e);
}
else if (e instanceof NoSuchOrganizationException ||
e instanceof PrincipalException) {
SessionErrors.add(actionRequest, e.getClass());
actionResponse.setRenderParameter("mvcPath", "/error.jsp");
}
else {
throw e;
}
}
}
@Reference(unbind = "-")
protected void setUserService(UserService userService) {
_userService = userService;
}
protected void updateOrganizationUsers(ActionRequest actionRequest)
throws Exception {
long organizationId = ParamUtil.getLong(
actionRequest, "organizationId");
long[] addUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "addUserIds"), 0L);
long[] removeUserIds = StringUtil.split(
ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
try (ProxyModeThreadLocalCloseable proxyModeThreadLocalCloseable =
new ProxyModeThreadLocalCloseable()) {
ProxyModeThreadLocal.setForceSync(true);
_userService.addOrganizationUsers(organizationId, addUserIds);
_userService.unsetOrganizationUsers(organizationId, removeUserIds);
}
}
private UserService _userService;
}