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.journal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.spring.osgi.OSGiBeanProperties;
019    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.journal.model.JournalFeed;
025    import com.liferay.portlet.journal.service.JournalFeedLocalServiceUtil;
026    
027    /**
028     * @author Raymond Aug??
029     */
030    @OSGiBeanProperties(
031            property = {
032                    "model.class.name=com.liferay.portlet.journal.model.JournalFeed"
033            }
034    )
035    public class JournalFeedPermission implements BaseModelPermissionChecker {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, JournalFeed feed,
039                            String actionId)
040                    throws PortalException {
041    
042                    if (!contains(permissionChecker, feed, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static void check(
048                            PermissionChecker permissionChecker, long id, String actionId)
049                    throws PortalException {
050    
051                    if (!contains(permissionChecker, id, actionId)) {
052                            throw new PrincipalException();
053                    }
054            }
055    
056            public static void check(
057                            PermissionChecker permissionChecker, long groupId, String feedId,
058                            String actionId)
059                    throws PortalException {
060    
061                    if (!contains(permissionChecker, groupId, feedId, actionId)) {
062                            throw new PrincipalException();
063                    }
064            }
065    
066            public static boolean contains(
067                    PermissionChecker permissionChecker, JournalFeed feed,
068                    String actionId) {
069    
070                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
071                            permissionChecker, feed.getGroupId(), JournalFeed.class.getName(),
072                            feed.getPrimaryKey(), PortletKeys.JOURNAL, actionId);
073    
074                    if (hasPermission != null) {
075                            return hasPermission.booleanValue();
076                    }
077    
078                    if (permissionChecker.hasOwnerPermission(
079                                    feed.getCompanyId(), JournalFeed.class.getName(), feed.getId(),
080                                    feed.getUserId(), actionId)) {
081    
082                            return true;
083                    }
084    
085                    return permissionChecker.hasPermission(
086                            feed.getGroupId(), JournalFeed.class.getName(), feed.getId(),
087                            actionId);
088            }
089    
090            public static boolean contains(
091                            PermissionChecker permissionChecker, long feedId, String actionId)
092                    throws PortalException {
093    
094                    JournalFeed feed = JournalFeedLocalServiceUtil.getFeed(feedId);
095    
096                    return contains(permissionChecker, feed, actionId);
097            }
098    
099            public static boolean contains(
100                            PermissionChecker permissionChecker, long groupId, String feedId,
101                            String actionId)
102                    throws PortalException {
103    
104                    JournalFeed feed = JournalFeedLocalServiceUtil.getFeed(groupId, feedId);
105    
106                    return contains(permissionChecker, feed, actionId);
107            }
108    
109            @Override
110            public void checkBaseModel(
111                            PermissionChecker permissionChecker, long groupId, long primaryKey,
112                            String actionId)
113                    throws PortalException {
114    
115                    check(permissionChecker, primaryKey, actionId);
116            }
117    
118    }