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