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;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Group;
020    import com.liferay.portal.model.LayoutConstants;
021    import com.liferay.portal.model.Portlet;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.service.permission.PortletPermissionUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortletCategoryKeys;
027    
028    /**
029     * @author Jorge Ferrer
030     */
031    public abstract class BaseControlPanelEntry implements ControlPanelEntry {
032    
033            public boolean hasAccessPermission(
034                            PermissionChecker permissionChecker, Group group, Portlet portlet)
035                    throws Exception {
036    
037                    if (hasAccessPermissionDenied(permissionChecker, group, portlet)) {
038                            return false;
039                    }
040    
041                    if (hasAccessPermissionExplicitlyGranted(
042                                    permissionChecker, group, portlet)) {
043    
044                            return true;
045                    }
046    
047                    return hasPermissionImplicitlyGranted(
048                            permissionChecker, group, portlet);
049            }
050    
051            /**
052             * @deprecated As of 6.2, with no direct replacement.<p>This method was
053             *             originally defined to determine if a portlet should be
054             *             displayed in the Control Panel. In this version, this method
055             *             should always return <code>false</code> and remains only to
056             *             preserve binary compatibility. This method will be
057             *             permanently removed in a future version.</p><p>In lieu of
058             *             this method, the Control Panel now uses {@link
059             *             #hasAccessPermission} to determine if a portlet should be
060             *             displayed in the Control Panel.</p>
061             */
062            public boolean isVisible(
063                            PermissionChecker permissionChecker, Portlet portlet)
064                    throws Exception {
065    
066                    return false;
067            }
068    
069            /**
070             * @deprecated As of 6.2, with no direct replacement.<p>This method was
071             *             originally defined to determine if a portlet should be
072             *             displayed in the Control Panel. In this version, this method
073             *             should always return <code>false</code> and remains only to
074             *             preserve binary compatibility. This method will be
075             *             permanently removed in a future version.</p><p>In lieu of
076             *             this method, the Control Panel now uses {@link
077             *             #hasAccessPermission} to determine if a portlet should be
078             *             displayed in the Control Panel.</p>
079             */
080            public boolean isVisible(
081                            Portlet portlet, String category, ThemeDisplay themeDisplay)
082                    throws Exception {
083    
084                    return false;
085            }
086    
087            protected long getDefaultPlid(Group group, String category) {
088                    long plid = LayoutConstants.DEFAULT_PLID;
089    
090                    if (category.equals(PortletCategoryKeys.CONTENT)) {
091                            plid = group.getDefaultPublicPlid();
092    
093                            if (plid == LayoutConstants.DEFAULT_PLID) {
094                                    plid = group.getDefaultPrivatePlid();
095                            }
096                    }
097    
098                    return plid;
099            }
100    
101            protected boolean hasAccessPermissionDenied(
102                            PermissionChecker permissionChecker, Group group, Portlet portlet)
103                    throws Exception {
104    
105                    return false;
106            }
107    
108            protected boolean hasAccessPermissionExplicitlyGranted(
109                            PermissionChecker permissionChecker, Group group, Portlet portlet)
110                    throws PortalException, SystemException {
111    
112                    if (permissionChecker.isCompanyAdmin()) {
113                            return true;
114                    }
115    
116                    String category = portlet.getControlPanelEntryCategory();
117    
118                    if (category.equals(PortletCategoryKeys.CONTENT) &&
119                            permissionChecker.isGroupAdmin(group.getGroupId()) &&
120                            !group.isUser()) {
121    
122                            return true;
123                    }
124    
125                    long groupId = group.getGroupId();
126    
127                    if (category.equals(PortletCategoryKeys.PORTAL) ||
128                            category.equals(PortletCategoryKeys.SERVER)) {
129    
130                            groupId = 0;
131                    }
132    
133                    if (PortletPermissionUtil.contains(
134                                    permissionChecker, groupId, getDefaultPlid(group, category),
135                                    portlet.getPortletId(), ActionKeys.ACCESS_IN_CONTROL_PANEL,
136                                    true)) {
137    
138                            return true;
139                    }
140    
141                    return false;
142            }
143    
144            protected boolean hasPermissionImplicitlyGranted(
145                            PermissionChecker permissionChecker, Group group, Portlet portlet)
146                    throws Exception {
147    
148                    return false;
149            }
150    
151    }