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