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.UserGroupGroupRoleServiceUtil;
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 EditUserGroupRolesAction 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_group_role_users")) {
056 updateUserGroupGroupRoleUsers(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(
112 renderRequest, "portlet.sites_admin.edit_user_group_roles"));
113 }
114
115 protected void updateUserGroupGroupRoleUsers(ActionRequest actionRequest)
116 throws Exception {
117
118 long groupId = ParamUtil.getLong(actionRequest, "groupId");
119 long roleId = ParamUtil.getLong(actionRequest, "roleId");
120
121 long[] addUserGroupIds = StringUtil.split(
122 ParamUtil.getString(actionRequest, "addUserGroupIds"), 0L);
123 long[] removeUserGroupIds = StringUtil.split(
124 ParamUtil.getString(actionRequest, "removeUserGroupIds"), 0L);
125
126 UserGroupGroupRoleServiceUtil.addUserGroupGroupRoles(
127 addUserGroupIds, groupId, roleId);
128 UserGroupGroupRoleServiceUtil.deleteUserGroupGroupRoles(
129 removeUserGroupIds, groupId, roleId);
130 }
131
132 }