001
014
015 package com.liferay.portlet.sites.action;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.NoSuchRoleException;
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.model.Role;
024 import com.liferay.portal.model.RoleConstants;
025 import com.liferay.portal.security.auth.PrincipalException;
026 import com.liferay.portal.service.UserGroupRoleServiceUtil;
027 import com.liferay.portal.struts.PortletAction;
028 import com.liferay.portal.util.WebKeys;
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 EditUserRolesAction 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("user_group_role_users")) {
056 updateUserGroupRoleUsers(actionRequest);
057 }
058
059 sendRedirect(actionRequest, actionResponse);
060 }
061 catch (Exception e) {
062 if (e instanceof PrincipalException) {
063 SessionErrors.add(actionRequest, e.getClass());
064
065 setForward(actionRequest, "portlet.sites_admin.error");
066 }
067 else {
068 throw e;
069 }
070 }
071 }
072
073 @Override
074 public ActionForward render(
075 ActionMapping actionMapping, ActionForm actionForm,
076 PortletConfig portletConfig, RenderRequest renderRequest,
077 RenderResponse renderResponse)
078 throws Exception {
079
080 try {
081 ActionUtil.getGroup(renderRequest);
082 ActionUtil.getRole(renderRequest);
083
084 Role role = (Role)renderRequest.getAttribute(WebKeys.ROLE);
085
086 if (role != null) {
087 String name = role.getName();
088
089 if (name.equals(RoleConstants.ORGANIZATION_USER) ||
090 name.equals(RoleConstants.SITE_MEMBER)) {
091
092 throw new NoSuchRoleException();
093 }
094 }
095 }
096 catch (Exception e) {
097 if (e instanceof NoSuchGroupException ||
098 e instanceof NoSuchRoleException ||
099 e instanceof PrincipalException) {
100
101 SessionErrors.add(renderRequest, e.getClass());
102
103 return actionMapping.findForward("portlet.sites_admin.error");
104 }
105 else {
106 throw e;
107 }
108 }
109
110 return actionMapping.findForward(
111 getForward(renderRequest, "portlet.sites_admin.edit_user_roles"));
112 }
113
114 protected void updateUserGroupRoleUsers(ActionRequest actionRequest)
115 throws Exception {
116
117 long groupId = ParamUtil.getLong(actionRequest, "groupId");
118 long roleId = ParamUtil.getLong(actionRequest, "roleId");
119
120 long[] addUserIds = StringUtil.split(
121 ParamUtil.getString(actionRequest, "addUserIds"), 0L);
122 long[] removeUserIds = StringUtil.split(
123 ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
124
125 UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId, roleId);
126 UserGroupRoleServiceUtil.deleteUserGroupRoles(
127 removeUserIds, groupId, roleId);
128 }
129
130 }