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().getName());
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().getName());
099    
100                                    return mapping.findForward("portlet.roles_admin.error");
101                            }
102                            else {
103                                    throw e;
104                            }
105                    }
106    
107                    return mapping.findForward(getForward(
108                            renderRequest, "portlet.roles_admin.edit_role_assignments"));
109            }
110    
111            protected void updateRoleGroups(ActionRequest actionRequest)
112                    throws Exception {
113    
114                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
115    
116                    long[] addGroupIds = StringUtil.split(
117                            ParamUtil.getString(actionRequest, "addGroupIds"), 0L);
118                    long[] removeGroupIds = StringUtil.split(
119                            ParamUtil.getString(actionRequest, "removeGroupIds"), 0L);
120    
121                    Role role = RoleLocalServiceUtil.getRole(roleId);
122    
123                    if (role.getName().equals(RoleConstants.OWNER)) {
124                            throw new RoleAssignmentException(role.getName());
125                    }
126    
127                    GroupServiceUtil.addRoleGroups(roleId, addGroupIds);
128                    GroupServiceUtil.unsetRoleGroups(roleId, removeGroupIds);
129            }
130    
131            protected void updateRoleUsers(ActionRequest actionRequest)
132                    throws Exception {
133    
134                    long roleId = ParamUtil.getLong(actionRequest, "roleId");
135    
136                    long[] addUserIds = StringUtil.split(
137                            ParamUtil.getString(actionRequest, "addUserIds"), 0L);
138                    long[] removeUserIds = StringUtil.split(
139                            ParamUtil.getString(actionRequest, "removeUserIds"), 0L);
140    
141                    Role role = RoleLocalServiceUtil.getRole(roleId);
142    
143                    if (role.getName().equals(RoleConstants.OWNER)) {
144                            throw new RoleAssignmentException(role.getName());
145                    }
146    
147                    UserServiceUtil.addRoleUsers(roleId, addUserIds);
148                    UserServiceUtil.unsetRoleUsers(roleId, removeUserIds);
149            }
150    
151    }