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