001
014
015 package com.liferay.portlet.usergroupsadmin.action;
016
017 import com.liferay.portal.NoSuchUserGroupException;
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.security.auth.PrincipalException;
024 import com.liferay.portal.service.UserServiceUtil;
025 import com.liferay.portal.struts.PortletAction;
026
027 import javax.portlet.ActionRequest;
028 import javax.portlet.ActionResponse;
029 import javax.portlet.PortletConfig;
030 import javax.portlet.RenderRequest;
031 import javax.portlet.RenderResponse;
032
033 import org.apache.struts.action.ActionForm;
034 import org.apache.struts.action.ActionForward;
035 import org.apache.struts.action.ActionMapping;
036
037
040 public class EditUserGroupAssignmentsAction extends PortletAction {
041
042 @Override
043 public void processAction(
044 ActionMapping actionMapping, ActionForm actionForm,
045 PortletConfig portletConfig, ActionRequest actionRequest,
046 ActionResponse actionResponse)
047 throws Exception {
048
049 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
050
051 try {
052 if (cmd.equals("user_group_users")) {
053 updateUserGroupUsers(actionRequest);
054 }
055
056 if (Validator.isNotNull(cmd)) {
057 String redirect = ParamUtil.getString(
058 actionRequest, "assignmentsRedirect");
059
060 sendRedirect(actionRequest, actionResponse, redirect);
061 }
062 }
063 catch (Exception e) {
064 if (e instanceof NoSuchUserGroupException ||
065 e instanceof PrincipalException) {
066
067 SessionErrors.add(actionRequest, e.getClass());
068
069 setForward(actionRequest, "portlet.user_groups_admin.error");
070 }
071 else {
072 throw e;
073 }
074 }
075 }
076
077 @Override
078 public ActionForward render(
079 ActionMapping actionMapping, ActionForm actionForm,
080 PortletConfig portletConfig, RenderRequest renderRequest,
081 RenderResponse renderResponse)
082 throws Exception {
083
084 try {
085 ActionUtil.getUserGroup(renderRequest);
086 }
087 catch (Exception e) {
088 if (e instanceof NoSuchUserGroupException ||
089 e instanceof PrincipalException) {
090
091 SessionErrors.add(renderRequest, e.getClass());
092
093 return actionMapping.findForward(
094 "portlet.user_groups_admin.error");
095 }
096 else {
097 throw e;
098 }
099 }
100
101 return actionMapping.findForward(
102 getForward(
103 renderRequest,
104 "portlet.user_groups_admin.edit_user_group_assignments"));
105 }
106
107 protected void updateUserGroupUsers(ActionRequest actionRequest)
108 throws Exception {
109
110 long userGroupId = ParamUtil.getLong(actionRequest, "userGroupId");
111
112 long[] addUserIds = StringUtil.split(
113 ParamUtil.getString(actionRequest, "addUserIds"), 0L);
114 long[] removeUserIds = StringUtil.split(
115 ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
116
117 UserServiceUtil.addUserGroupUsers(userGroupId, addUserIds);
118 UserServiceUtil.unsetUserGroupUsers(userGroupId, removeUserIds);
119 }
120
121 }