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.enterpriseadmin.action;
022:
023: import com.liferay.portal.NoSuchUserException;
024: import com.liferay.portal.kernel.util.Constants;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.kernel.util.Validator;
028: import com.liferay.portal.model.User;
029: import com.liferay.portal.security.auth.PrincipalException;
030: import com.liferay.portal.service.RoleServiceUtil;
031: import com.liferay.portal.struts.PortletAction;
032: import com.liferay.portal.util.PortalUtil;
033: import com.liferay.util.servlet.SessionErrors;
034:
035: import javax.portlet.ActionRequest;
036: import javax.portlet.ActionResponse;
037: import javax.portlet.PortletConfig;
038: import javax.portlet.RenderRequest;
039: import javax.portlet.RenderResponse;
040:
041: import org.apache.struts.action.ActionForm;
042: import org.apache.struts.action.ActionForward;
043: import org.apache.struts.action.ActionMapping;
044:
045: /**
046: * <a href="EditUserRegularRoleAssignmentsAction.java.html"><b><i>View Source
047: * </i></b></a>
048: *
049: * @author Brian Wing Shun Chan
050: *
051: */
052: public class EditUserRegularRoleAssignmentsAction extends PortletAction {
053:
054: public void processAction(ActionMapping mapping, ActionForm form,
055: PortletConfig config, ActionRequest req, ActionResponse res)
056: throws Exception {
057:
058: String cmd = ParamUtil.getString(req, Constants.CMD);
059:
060: try {
061: if (Validator.isNotNull(cmd)) {
062: updateUserRoles(req);
063:
064: sendRedirect(req, res);
065: }
066: } catch (Exception e) {
067: if (e instanceof NoSuchUserException
068: || e instanceof PrincipalException) {
069:
070: SessionErrors.add(req, e.getClass().getName());
071:
072: setForward(req, "portlet.enterprise_admin.error");
073: } else {
074: throw e;
075: }
076: }
077: }
078:
079: public ActionForward render(ActionMapping mapping, ActionForm form,
080: PortletConfig config, RenderRequest req, RenderResponse res)
081: throws Exception {
082:
083: try {
084: PortalUtil.getSelectedUser(req);
085: } catch (Exception e) {
086: if (e instanceof NoSuchUserException
087: || e instanceof PrincipalException) {
088:
089: SessionErrors.add(req, e.getClass().getName());
090:
091: return mapping
092: .findForward("portlet.enterprise_admin.error");
093: } else {
094: throw e;
095: }
096: }
097:
098: return mapping
099: .findForward(getForward(req,
100: "portlet.enterprise_admin.edit_user_regular_role_assignments"));
101: }
102:
103: protected void updateUserRoles(ActionRequest req) throws Exception {
104: User user = PortalUtil.getSelectedUser(req);
105:
106: long[] addRoleIds = StringUtil.split(ParamUtil.getString(req,
107: "addRoleIds"), 0L);
108: long[] removeRoleIds = StringUtil.split(ParamUtil.getString(
109: req, "removeRoleIds"), 0L);
110:
111: RoleServiceUtil.addUserRoles(user.getUserId(), addRoleIds);
112: RoleServiceUtil.unsetUserRoles(user.getUserId(), removeRoleIds);
113: }
114:
115: }
|