001
014
015 package com.liferay.portlet.rolesadmin.action;
016
017 import com.liferay.portal.NoSuchRoleException;
018 import com.liferay.portal.RoleAssignmentException;
019 import com.liferay.portal.kernel.servlet.SessionErrors;
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.model.Role;
025 import com.liferay.portal.model.RoleConstants;
026 import com.liferay.portal.security.auth.PrincipalException;
027 import com.liferay.portal.service.GroupServiceUtil;
028 import com.liferay.portal.service.RoleLocalServiceUtil;
029 import com.liferay.portal.service.UserServiceUtil;
030 import com.liferay.portal.struts.PortletAction;
031
032 import javax.portlet.ActionRequest;
033 import javax.portlet.ActionResponse;
034 import javax.portlet.PortletConfig;
035 import javax.portlet.RenderRequest;
036 import javax.portlet.RenderResponse;
037
038 import org.apache.struts.action.ActionForm;
039 import org.apache.struts.action.ActionForward;
040 import org.apache.struts.action.ActionMapping;
041
042
045 public class EditRoleAssignmentsAction extends PortletAction {
046
047 @Override
048 public void processAction(
049 ActionMapping actionMapping, ActionForm actionForm,
050 PortletConfig portletConfig, ActionRequest actionRequest,
051 ActionResponse actionResponse)
052 throws Exception {
053
054 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055
056 try {
057 if (cmd.equals("role_groups")) {
058 updateRoleGroups(actionRequest);
059 }
060 else if (cmd.equals("role_users")) {
061 updateRoleUsers(actionRequest);
062 }
063
064 if (Validator.isNotNull(cmd)) {
065 String redirect = ParamUtil.getString(
066 actionRequest, "assignmentsRedirect");
067
068 sendRedirect(actionRequest, actionResponse, redirect);
069 }
070 }
071 catch (Exception e) {
072 if (e instanceof NoSuchRoleException ||
073 e instanceof PrincipalException ||
074 e instanceof RoleAssignmentException) {
075
076 SessionErrors.add(actionRequest, e.getClass());
077
078 setForward(actionRequest, "portlet.roles_admin.error");
079 }
080 else {
081 throw e;
082 }
083 }
084 }
085
086 @Override
087 public ActionForward render(
088 ActionMapping actionMapping, ActionForm actionForm,
089 PortletConfig portletConfig, RenderRequest renderRequest,
090 RenderResponse renderResponse)
091 throws Exception {
092
093 try {
094 ActionUtil.getRole(renderRequest);
095 }
096 catch (Exception e) {
097 if (e instanceof NoSuchRoleException ||
098 e instanceof PrincipalException) {
099
100 SessionErrors.add(renderRequest, e.getClass());
101
102 return actionMapping.findForward("portlet.roles_admin.error");
103 }
104 else {
105 throw e;
106 }
107 }
108
109 return actionMapping.findForward(
110 getForward(
111 renderRequest, "portlet.roles_admin.edit_role_assignments"));
112 }
113
114 protected void updateRoleGroups(ActionRequest actionRequest)
115 throws Exception {
116
117 long roleId = ParamUtil.getLong(actionRequest, "roleId");
118
119 long[] addGroupIds = StringUtil.split(
120 ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
121 long[] removeGroupIds = StringUtil.split(
122 ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
123
124 Role role = RoleLocalServiceUtil.getRole(roleId);
125
126 if (role.getName().equals(RoleConstants.OWNER)) {
127 throw new RoleAssignmentException(role.getName());
128 }
129
130 GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
131 GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
132 }
133
134 protected void updateRoleUsers(ActionRequest actionRequest)
135 throws Exception {
136
137 long roleId = ParamUtil.getLong(actionRequest, "roleId");
138
139 long[] addUserIds = StringUtil.split(
140 ParamUtil.getString(actionRequest, "addUserIds"), 0L);
141 long[] removeUserIds = StringUtil.split(
142 ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
143
144 Role role = RoleLocalServiceUtil.getRole(roleId);
145
146 if (role.getName().equals(RoleConstants.OWNER)) {
147 throw new RoleAssignmentException(role.getName());
148 }
149
150 UserServiceUtil.addRoleUsers(roleId, addUserIds);
151 UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
152 }
153
154 }