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.rolesadmin.action;
016    
017    import com.liferay.portal.NoSuchRoleException;
018    import com.liferay.portal.RoleAssignmentException;
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.kernel.util.Validator;
024    import com.liferay.portal.model.Role;
025    import com.liferay.portal.model.RoleConstants;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.security.membershippolicy.MembershipPolicyException;
028    import com.liferay.portal.service.GroupServiceUtil;
029    import com.liferay.portal.service.RoleLocalServiceUtil;
030    import com.liferay.portal.service.UserServiceUtil;
031    import com.liferay.portal.struts.PortletAction;
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     * @author Brian Wing Shun Chan
045     */
046    public class EditRoleAssignmentsAction extends PortletAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping actionMapping, ActionForm actionForm,
051                            PortletConfig portletConfig, ActionRequest actionRequest,
052                            ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals("role_groups")) {
059                                    updateRoleGroups(actionRequest);
060                            }
061                            else if (cmd.equals("role_users")) {
062                                    updateRoleUsers(actionRequest);
063                            }
064    
065                            if (Validator.isNotNull(cmd)) {
066                                    String redirect = ParamUtil.getString(
067                                            actionRequest, "assignmentsRedirect");
068    
069                                    sendRedirect(actionRequest, actionResponse, redirect);
070                            }
071                    }
072                    catch (Exception e) {
073                            if (e instanceof MembershipPolicyException) {
074                                    SessionErrors.add(actionRequest, e.getClass(), e);
075                            }
076                            else if (e instanceof NoSuchRoleException ||
077                                             e instanceof PrincipalException ||
078                                             e instanceof RoleAssignmentException) {
079    
080                                    SessionErrors.add(actionRequest, e.getClass());
081    
082                                    setForward(actionRequest, "portlet.roles_admin.error");
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping actionMapping, ActionForm actionForm,
093                            PortletConfig portletConfig, RenderRequest renderRequest,
094                            RenderResponse renderResponse)
095                    throws Exception {
096    
097                    try {
098                            ActionUtil.getRole(renderRequest);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchRoleException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(renderRequest, e.getClass());
105    
106                                    return actionMapping.findForward("portlet.roles_admin.error");
107                            }
108                            else {
109                                    throw e;
110                            }
111                    }
112    
113                    return actionMapping.findForward(
114                            getForward(
115                                    renderRequest, "portlet.roles_admin.edit_role_assignments"));
116            }
117    
118            protected void updateRoleGroups(ActionRequest actionRequest)
119                    throws Exception {
120    
121                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
122    
123                    long[] addGroupIds = StringUtil.split(
124                            ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
125                    long[] removeGroupIds = StringUtil.split(
126                            ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
127    
128                    Role role = RoleLocalServiceUtil.getRole(roleId);
129    
130                    if (role.getName().equals(RoleConstants.OWNER)) {
131                            throw new RoleAssignmentException(role.getName());
132                    }
133    
134                    GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
135                    GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
136            }
137    
138            protected void updateRoleUsers(ActionRequest actionRequest)
139                    throws Exception {
140    
141                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
142    
143                    long[] addUserIds = StringUtil.split(
144                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
145                    long[] removeUserIds = StringUtil.split(
146                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
147    
148                    Role role = RoleLocalServiceUtil.getRole(roleId);
149    
150                    if (role.getName().equals(RoleConstants.OWNER)) {
151                            throw new RoleAssignmentException(role.getName());
152                    }
153    
154                    UserServiceUtil.addRoleUsers(roleId, addUserIds);
155                    UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
156            }
157    
158    }