001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.communities.action;
022:
023: import com.liferay.portal.NoSuchGroupException;
024: import com.liferay.portal.NoSuchRoleException;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.ParamUtil;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.security.auth.PrincipalException;
029: import com.liferay.portal.service.UserGroupRoleServiceUtil;
030: import com.liferay.portal.struts.PortletAction;
031: import com.liferay.util.servlet.SessionErrors;
032:
033: import javax.portlet.ActionRequest;
034: import javax.portlet.ActionResponse;
035: import javax.portlet.PortletConfig;
036: import javax.portlet.RenderRequest;
037: import javax.portlet.RenderResponse;
038:
039: import org.apache.struts.action.ActionForm;
040: import org.apache.struts.action.ActionForward;
041: import org.apache.struts.action.ActionMapping;
042:
043: /**
044: * <a href="EditUserRolesAction.java.html"><b><i>View Source</i></b></a>
045: *
046: * @author Charles May
047: *
048: */
049: public class EditUserRolesAction extends PortletAction {
050:
051: public void processAction(ActionMapping mapping, ActionForm form,
052: PortletConfig config, ActionRequest req, ActionResponse res)
053: throws Exception {
054:
055: String cmd = ParamUtil.getString(req, Constants.CMD);
056:
057: try {
058: if (cmd.equals("user_group_role_users")) {
059: updateUserGroupRoleUsers(req);
060: }
061:
062: sendRedirect(req, res);
063: } catch (Exception e) {
064: if (e instanceof PrincipalException) {
065: SessionErrors.add(req, e.getClass().getName());
066:
067: setForward(req, "portlet.communities.error");
068: } else {
069: throw e;
070: }
071: }
072: }
073:
074: public ActionForward render(ActionMapping mapping, ActionForm form,
075: PortletConfig config, RenderRequest req, RenderResponse res)
076: throws Exception {
077:
078: try {
079: ActionUtil.getGroup(req);
080: ActionUtil.getRole(req);
081: } catch (Exception e) {
082: if (e instanceof NoSuchGroupException
083: || e instanceof NoSuchRoleException
084: || e instanceof PrincipalException) {
085:
086: SessionErrors.add(req, e.getClass().getName());
087:
088: return mapping.findForward("portlet.communities.error");
089: } else {
090: throw e;
091: }
092: }
093:
094: return mapping.findForward(getForward(req,
095: "portlet.communities.edit_user_roles"));
096: }
097:
098: protected void updateUserGroupRoleUsers(ActionRequest req)
099: throws Exception {
100:
101: long groupId = ParamUtil.getLong(req, "groupId");
102: long roleId = ParamUtil.getLong(req, "roleId");
103:
104: long[] addUserIds = StringUtil.split(ParamUtil.getString(req,
105: "addUserIds"), 0L);
106: long[] removeUserIds = StringUtil.split(ParamUtil.getString(
107: req, "removeUserIds"), 0L);
108:
109: UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId,
110: roleId);
111: UserGroupRoleServiceUtil.deleteUserGroupRoles(removeUserIds,
112: groupId, roleId);
113: }
114:
115: }
|