001    /**
002     * Copyright (c) 2000-2012 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.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    /**
041     * @author Charles May
042     */
043    public class EditUserRolesAction extends PortletAction {
044    
045            @Override
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
052    
053                    try {
054                            if (cmd.equals("user_group_role_users")) {
055                                    updateUserGroupRoleUsers(actionRequest);
056                            }
057    
058                            sendRedirect(actionRequest, actionResponse);
059                    }
060                    catch (Exception e) {
061                            if (e instanceof PrincipalException) {
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063    
064                                    setForward(actionRequest, "portlet.sites_admin.error");
065                            }
066                            else {
067                                    throw e;
068                            }
069                    }
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
075                            RenderRequest renderRequest, RenderResponse renderResponse)
076                    throws Exception {
077    
078                    try {
079                            ActionUtil.getGroup(renderRequest);
080                            ActionUtil.getRole(renderRequest);
081    
082                            Role role = (Role)renderRequest.getAttribute(WebKeys.ROLE);
083    
084                            if (role != null) {
085                                    String name = role.getName();
086    
087                                    if (name.equals(RoleConstants.ORGANIZATION_USER) ||
088                                            name.equals(RoleConstants.SITE_MEMBER)) {
089    
090                                            throw new NoSuchRoleException();
091                                    }
092                            }
093                    }
094                    catch (Exception e) {
095                            if (e instanceof NoSuchGroupException ||
096                                    e instanceof NoSuchRoleException ||
097                                    e instanceof PrincipalException) {
098    
099                                    SessionErrors.add(renderRequest, e.getClass().getName());
100    
101                                    return mapping.findForward("portlet.sites_admin.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107    
108                    return mapping.findForward(
109                            getForward(renderRequest, "portlet.sites_admin.edit_user_roles"));
110            }
111    
112            protected void updateUserGroupRoleUsers(ActionRequest actionRequest)
113                    throws Exception {
114    
115                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
116                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
117    
118                    long[] addUserIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
120                    long[] removeUserIds = StringUtil.split(
121                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
122    
123                    UserGroupRoleServiceUtil.addUserGroupRoles(addUserIds, groupId, roleId);
124                    UserGroupRoleServiceUtil.deleteUserGroupRoles(
125                            removeUserIds, groupId, roleId);
126            }
127    
128    }