001    /**
002     * Copyright (c) 2000-present 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.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.service.PermissionServiceUtil;
023    import com.liferay.portal.service.PortletLocalServiceUtil;
024    import com.liferay.portal.struts.PortletAction;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.StrictPortletPreferencesImpl;
028    import com.liferay.portlet.portletconfiguration.util.ConfigurationRenderRequest;
029    
030    import java.util.ArrayList;
031    import java.util.Enumeration;
032    import java.util.List;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Michael Bowerman
046     */
047    public class ViewPermissionsAction extends PortletAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping actionMapping, ActionForm actionForm,
052                            PortletConfig portletConfig, ActionRequest actionRequest,
053                            ActionResponse actionResponse)
054                    throws Exception {
055            }
056    
057            @Override
058            public ActionForward render(
059                            ActionMapping actionMapping, ActionForm actionForm,
060                            PortletConfig portletConfig, RenderRequest renderRequest,
061                            RenderResponse renderResponse)
062                    throws Exception {
063    
064                    renderRequest = new ConfigurationRenderRequest(
065                            renderRequest, new StrictPortletPreferencesImpl());
066    
067                    ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(
068                            WebKeys.THEME_DISPLAY);
069    
070                    long groupId = ParamUtil.getLong(
071                            renderRequest, "resourceGroupId", themeDisplay.getScopeGroupId());
072    
073                    String portletResource = ParamUtil.getString(
074                            renderRequest, "portletResource");
075                    String modelResource = ParamUtil.getString(
076                            renderRequest, "modelResource");
077                    String resourcePrimKey = ParamUtil.getString(
078                            renderRequest, "resourcePrimKey");
079    
080                    String selResource = portletResource;
081    
082                    if (Validator.isNotNull(modelResource)) {
083                            selResource = modelResource;
084                    }
085    
086                    try {
087                            PermissionServiceUtil.checkPermission(
088                                    groupId, selResource, resourcePrimKey);
089                    }
090                    catch (PrincipalException pe) {
091                            SessionErrors.add(
092                                    renderRequest, PrincipalException.class.getName());
093    
094                            return actionMapping.findForward(
095                                    "portlet.portlet_configuration.error");
096                    }
097    
098                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
099                            themeDisplay.getCompanyId(), portletResource);
100    
101                    if (portlet != null) {
102                            renderResponse.setTitle(
103                                    ActionUtil.getTitle(portlet, renderRequest));
104                    }
105    
106                    return actionMapping.findForward(
107                            getForward(
108                                    renderRequest,
109                                    "portlet.portlet_configuration.edit_permissions"));
110            }
111    
112            protected String[] getActionIds(
113                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
114    
115                    List<String> actionIds = getActionIdsList(
116                            actionRequest, roleId, includePreselected);
117    
118                    return actionIds.toArray(new String[actionIds.size()]);
119            }
120    
121            protected List<String> getActionIdsList(
122                    ActionRequest actionRequest, long roleId, boolean includePreselected) {
123    
124                    List<String> actionIds = new ArrayList<String>();
125    
126                    Enumeration<String> enu = actionRequest.getParameterNames();
127    
128                    while (enu.hasMoreElements()) {
129                            String name = enu.nextElement();
130    
131                            if (name.startsWith(roleId + ActionUtil.ACTION)) {
132                                    int pos = name.indexOf(ActionUtil.ACTION);
133    
134                                    String actionId = name.substring(
135                                            pos + ActionUtil.ACTION.length());
136    
137                                    actionIds.add(actionId);
138                            }
139                            else if (includePreselected &&
140                                             name.startsWith(roleId + ActionUtil.PRESELECTED)) {
141    
142                                    int pos = name.indexOf(ActionUtil.PRESELECTED);
143    
144                                    String actionId = name.substring(
145                                            pos + ActionUtil.PRESELECTED.length());
146    
147                                    actionIds.add(actionId);
148                            }
149                    }
150    
151                    return actionIds;
152            }
153    
154    }