001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.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    /**
041     * @author Charles May
042     */
043    public class EditUserRolesAction extends PortletAction {
044    
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
051    
052                    try {
053                            if (cmd.equals("user_group_role_users")) {
054                                    updateUserGroupRoleUsers(actionRequest);
055                            }
056    
057                            sendRedirect(actionRequest, actionResponse);
058                    }
059                    catch (Exception e) {
060                            if (e instanceof PrincipalException) {
061                                    SessionErrors.add(actionRequest, e.getClass().getName());
062    
063                                    setForward(actionRequest, "portlet.communities.error");
064                            }
065                            else {
066                                    throw e;
067                            }
068                    }
069            }
070    
071            public ActionForward render(
072                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
073                            RenderRequest renderRequest, RenderResponse renderResponse)
074                    throws Exception {
075    
076                    try {
077                            ActionUtil.getGroup(renderRequest);
078                            ActionUtil.getRole(renderRequest);
079    
080                            Role role = (Role)renderRequest.getAttribute(WebKeys.ROLE);
081    
082                            if (role != null) {
083                                    String name = role.getName();
084    
085                                    if (name.equals(RoleConstants.COMMUNITY_MEMBER) ||
086                                            name.equals(RoleConstants.ORGANIZATION_MEMBER)) {
087    
088                                            throw new NoSuchRoleException();
089                                    }
090                            }
091                    }
092                    catch (Exception e) {
093                            if (e instanceof NoSuchGroupException ||
094                                    e instanceof NoSuchRoleException ||
095                                    e instanceof PrincipalException) {
096    
097                                    SessionErrors.add(renderRequest, e.getClass().getName());
098    
099                                    return mapping.findForward("portlet.communities.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return mapping.findForward(
107                            getForward(renderRequest, "portlet.communities.edit_user_roles"));
108            }
109    
110            protected void updateUserGroupRoleUsers(ActionRequest actionRequest)
111                    throws Exception {
112    
113                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
114                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
115    
116                    long[] addUserIds = StringUtil.split(
117                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
118                    long[] removeUserIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
120    
121                    UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId, roleId);
122                    UserGroupRoleServiceUtil.deleteUserGroupRoles(
123                            removeUserIds, groupId, roleId);
124            }
125    
126    }