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.blogs.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.kernel.workflow.permission.WorkflowPermissionUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portlet.blogs.model.BlogsEntry;
025    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class BlogsEntryPermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, BlogsEntry entry,
034                            String actionId)
035                    throws PortalException {
036    
037                    if (!contains(permissionChecker, entry, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, long entryId, String actionId)
044                    throws PortalException, SystemException {
045    
046                    if (!contains(permissionChecker, entryId, actionId)) {
047                            throw new PrincipalException();
048                    }
049            }
050    
051            public static boolean contains(
052                    PermissionChecker permissionChecker, BlogsEntry entry,
053                    String actionId) {
054    
055                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
056                            permissionChecker, entry.getGroupId(), BlogsEntry.class.getName(),
057                            entry.getEntryId(), PortletKeys.BLOGS, actionId);
058    
059                    if (hasPermission != null) {
060                            return hasPermission.booleanValue();
061                    }
062    
063                    if (entry.isPending()) {
064                            hasPermission = WorkflowPermissionUtil.hasPermission(
065                                    permissionChecker, entry.getGroupId(),
066                                    BlogsEntry.class.getName(), entry.getEntryId(), actionId);
067    
068                            if (hasPermission != null) {
069                                    return hasPermission.booleanValue();
070                            }
071                    }
072    
073                    if (permissionChecker.hasOwnerPermission(
074                                    entry.getCompanyId(), BlogsEntry.class.getName(),
075                                    entry.getEntryId(), entry.getUserId(), actionId)) {
076    
077                            return true;
078                    }
079    
080                    return permissionChecker.hasPermission(
081                            entry.getGroupId(), BlogsEntry.class.getName(), entry.getEntryId(),
082                            actionId);
083            }
084    
085            public static boolean contains(
086                            PermissionChecker permissionChecker, long entryId, String actionId)
087                    throws PortalException, SystemException {
088    
089                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
090    
091                    return contains(permissionChecker, entry, actionId);
092            }
093    
094    }