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