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