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.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PortletConstants;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.security.permission.PermissionPropagator;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.PermissionServiceUtil;
029    import com.liferay.portal.service.PortletLocalServiceUtil;
030    import com.liferay.portal.service.ResourceBlockLocalServiceUtil;
031    import com.liferay.portal.service.ResourceBlockServiceUtil;
032    import com.liferay.portal.service.ResourcePermissionServiceUtil;
033    import com.liferay.portal.servlet.filters.cache.CacheUtil;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portal.theme.ThemeDisplay;
036    import com.liferay.portal.util.PropsValues;
037    import com.liferay.portal.util.WebKeys;
038    
039    import java.util.ArrayList;
040    import java.util.Date;
041    import java.util.Enumeration;
042    import java.util.HashMap;
043    import java.util.List;
044    import java.util.Map;
045    
046    import javax.portlet.ActionRequest;
047    import javax.portlet.ActionResponse;
048    import javax.portlet.PortletConfig;
049    import javax.portlet.RenderRequest;
050    import javax.portlet.RenderResponse;
051    
052    import org.apache.struts.action.ActionForm;
053    import org.apache.struts.action.ActionForward;
054    import org.apache.struts.action.ActionMapping;
055    
056    /**
057     * @author Brian Wing Shun Chan
058     * @author Connor McKay
059     */
060    public class EditPermissionsAction extends PortletAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping actionMapping, ActionForm actionForm,
065                            PortletConfig portletConfig, ActionRequest actionRequest,
066                            ActionResponse actionResponse)
067                    throws Exception {
068    
069                    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, null);
070    
071                    try {
072                            updateRolePermissions(actionRequest);
073    
074                            addSuccessMessage(actionRequest, actionResponse);
075                    }
076                    catch (Exception e) {
077                            if (e instanceof PrincipalException) {
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(
081                                            actionRequest, "portlet.portlet_configuration.error");
082                            }
083                            else {
084                                    throw e;
085                            }
086                    }
087            }
088    
089            @Override
090            public ActionForward render(
091                            ActionMapping actionMapping, ActionForm actionForm,
092                            PortletConfig portletConfig, RenderRequest renderRequest,
093                            RenderResponse renderResponse)
094                    throws Exception {
095    
096                    renderRequest = ActionUtil.getWrappedRenderRequest(renderRequest, null);
097    
098                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
099                            WebKeys.THEME_DISPLAY);
100    
101                    long groupId = ParamUtil.getLong(
102                            renderRequest, "resourceGroupId", themeDisplay.getScopeGroupId());
103    
104                    String portletResource = ParamUtil.getString(
105                            renderRequest, "portletResource");
106                    String modelResource = ParamUtil.getString(
107                            renderRequest, "modelResource");
108                    String resourcePrimKey = ParamUtil.getString(
109                            renderRequest, "resourcePrimKey");
110    
111                    String selResource = portletResource;
112    
113                    if (Validator.isNotNull(modelResource)) {
114                            selResource = modelResource;
115                    }
116    
117                    try {
118                            PermissionServiceUtil.checkPermission(
119                                    groupId, selResource, resourcePrimKey);
120                    }
121                    catch (PrincipalException pe) {
122                            SessionErrors.add(
123                                    renderRequest, PrincipalException.class.getName());
124    
125                            setForward(renderRequest, "portlet.portlet_configuration.error");
126                    }
127    
128                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
129                            themeDisplay.getCompanyId(), portletResource);
130    
131                    if (portlet != null) {
132                            renderResponse.setTitle(
133                                    ActionUtil.getTitle(portlet, renderRequest));
134                    }
135    
136                    return actionMapping.findForward(
137                            getForward(
138                                    renderRequest,
139                                    "portlet.portlet_configuration.edit_permissions"));
140            }
141    
142            protected String[] getActionIds(
143                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
144    
145                    List<String> actionIds = getActionIdsList(
146                            actionRequest, roleId, includePreselected);
147    
148                    return actionIds.toArray(new String[actionIds.size()]);
149            }
150    
151            protected List<String> getActionIdsList(
152                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
153    
154                    List<String> actionIds = new ArrayList<String>();
155    
156                    Enumeration<String> enu = actionRequest.getParameterNames();
157    
158                    while (enu.hasMoreElements()) {
159                            String name = enu.nextElement();
160    
161                            if (name.startsWith(roleId + ActionUtil.ACTION)) {
162                                    int pos = name.indexOf(ActionUtil.ACTION);
163    
164                                    String actionId = name.substring(
165                                            pos + ActionUtil.ACTION.length());
166    
167                                    actionIds.add(actionId);
168                            }
169                            else if (includePreselected &&
170                                             name.startsWith(roleId + ActionUtil.PRESELECTED)) {
171    
172                                    int pos = name.indexOf(ActionUtil.PRESELECTED);
173    
174                                    String actionId = name.substring(
175                                            pos + ActionUtil.PRESELECTED.length());
176    
177                                    actionIds.add(actionId);
178                            }
179                    }
180    
181                    return actionIds;
182            }
183    
184            protected void updateRolePermissions(ActionRequest actionRequest)
185                    throws Exception {
186    
187                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
188                            WebKeys.THEME_DISPLAY);
189    
190                    String portletResource = ParamUtil.getString(
191                            actionRequest, "portletResource");
192                    String modelResource = ParamUtil.getString(
193                            actionRequest, "modelResource");
194                    long[] roleIds = StringUtil.split(
195                            ParamUtil.getString(
196                                    actionRequest, "rolesSearchContainerPrimaryKeys"), 0L);
197    
198                    String selResource = PortletConstants.getRootPortletId(portletResource);
199    
200                    if (Validator.isNotNull(modelResource)) {
201                            selResource = modelResource;
202                    }
203    
204                    long resourceGroupId = ParamUtil.getLong(
205                            actionRequest, "resourceGroupId", themeDisplay.getScopeGroupId());
206                    String resourcePrimKey = ParamUtil.getString(
207                            actionRequest, "resourcePrimKey");
208    
209                    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
210    
211                    if (ResourceBlockLocalServiceUtil.isSupported(selResource)) {
212                            for (long roleId : roleIds) {
213                                    List<String> actionIds = getActionIdsList(
214                                            actionRequest, roleId, true);
215    
216                                    roleIdsToActionIds.put(
217                                            roleId, actionIds.toArray(new String[actionIds.size()]));
218                            }
219    
220                            ResourceBlockServiceUtil.setIndividualScopePermissions(
221                                    themeDisplay.getCompanyId(), resourceGroupId, selResource,
222                                    GetterUtil.getLong(resourcePrimKey), roleIdsToActionIds);
223                    }
224                    else {
225                            for (long roleId : roleIds) {
226                                    String[] actionIds = getActionIds(actionRequest, roleId, false);
227    
228                                    roleIdsToActionIds.put(roleId, actionIds);
229                            }
230    
231                            ResourcePermissionServiceUtil.setIndividualResourcePermissions(
232                                    resourceGroupId, themeDisplay.getCompanyId(), selResource,
233                                    resourcePrimKey, roleIdsToActionIds);
234                    }
235    
236                    int pos = resourcePrimKey.indexOf(PortletConstants.LAYOUT_SEPARATOR);
237    
238                    if (pos != -1) {
239                            long plid = GetterUtil.getLong(resourcePrimKey.substring(0, pos));
240    
241                            Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
242    
243                            if (layout != null) {
244                                    layout.setModifiedDate(new Date());
245    
246                                    LayoutLocalServiceUtil.updateLayout(layout);
247    
248                                    CacheUtil.clearCache(layout.getCompanyId());
249                            }
250                    }
251    
252                    if (PropsValues.PERMISSIONS_PROPAGATION_ENABLED) {
253                            Portlet portlet = PortletLocalServiceUtil.getPortletById(
254                                    themeDisplay.getCompanyId(), portletResource);
255    
256                            PermissionPropagator permissionPropagator =
257                                    portlet.getPermissionPropagatorInstance();
258    
259                            if (permissionPropagator != null) {
260                                    permissionPropagator.propagateRolePermissions(
261                                            actionRequest, modelResource, resourcePrimKey, roleIds);
262                            }
263                    }
264            }
265    
266    }