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