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.announcements.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.impl.VirtualLayout;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.service.permission.PortletPermissionUtil;
025    import com.liferay.portlet.announcements.model.AnnouncementsEntry;
026    import com.liferay.portlet.announcements.service.AnnouncementsEntryLocalServiceUtil;
027    
028    /**
029     * @author Raymond Aug??
030     */
031    public class AnnouncementsEntryPermission {
032    
033            public static void check(
034                            PermissionChecker permissionChecker, AnnouncementsEntry entry,
035                            String actionId)
036                    throws PortalException, SystemException {
037    
038                    if (!contains(permissionChecker, entry, actionId)) {
039                            throw new PrincipalException();
040                    }
041            }
042    
043            public static void check(
044                            PermissionChecker permissionChecker, Layout layout, String name,
045                            String actionId)
046                    throws PortalException, SystemException {
047    
048                    if (!contains(permissionChecker, layout, name, actionId)) {
049                            throw new PrincipalException();
050                    }
051            }
052    
053            public static void check(
054                            PermissionChecker permissionChecker, long entryId, String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (!contains(permissionChecker, entryId, actionId)) {
058                            throw new PrincipalException();
059                    }
060            }
061    
062            public static void check(
063                            PermissionChecker permissionChecker, long plid, String name,
064                            String actionId)
065                    throws PortalException, SystemException {
066    
067                    if (!contains(permissionChecker, plid, name, actionId)) {
068                            throw new PrincipalException();
069                    }
070            }
071    
072            public static boolean contains(
073                            PermissionChecker permissionChecker, AnnouncementsEntry entry,
074                            String actionId)
075                    throws PortalException, SystemException {
076    
077                    if (permissionChecker.hasOwnerPermission(
078                                    entry.getCompanyId(), AnnouncementsEntry.class.getName(),
079                                    entry.getEntryId(), entry.getUserId(), actionId)) {
080    
081                            return true;
082                    }
083    
084                    return permissionChecker.hasPermission(
085                            entry.getGroupId(), AnnouncementsEntry.class.getName(),
086                            entry.getEntryId(), actionId);
087            }
088    
089            public static boolean contains(
090                            PermissionChecker permissionChecker, Layout layout, String name,
091                            String actionId)
092                    throws PortalException, SystemException {
093    
094                    if (layout instanceof VirtualLayout) {
095                            VirtualLayout virtualLayout = (VirtualLayout)layout;
096    
097                            layout = virtualLayout.getSourceLayout();
098                    }
099    
100                    if (permissionChecker.isGroupAdmin(layout.getGroupId()) ||
101                            permissionChecker.isGroupOwner(layout.getGroupId())) {
102    
103                            return true;
104                    }
105    
106                    return PortletPermissionUtil.contains(
107                                    permissionChecker, layout, name, actionId);
108            }
109    
110            public static boolean contains(
111                            PermissionChecker permissionChecker, long entryId, String actionId)
112                    throws PortalException, SystemException {
113    
114                    AnnouncementsEntry entry = AnnouncementsEntryLocalServiceUtil.getEntry(
115                            entryId);
116    
117                    return contains(permissionChecker, entry, actionId);
118            }
119    
120            public static boolean contains(
121                            PermissionChecker permissionChecker, long plid, String name,
122                            String actionId)
123                    throws PortalException, SystemException {
124    
125                    Layout layout = LayoutLocalServiceUtil.fetchLayout(plid);
126    
127                    return contains(permissionChecker, layout, name, actionId);
128            }
129    
130    }