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