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.blogs.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.kernel.workflow.permission.WorkflowPermissionUtil;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.BaseModelPermissionChecker;
024    import com.liferay.portal.security.permission.PermissionChecker;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portlet.blogs.model.BlogsEntry;
027    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    @OSGiBeanProperties(
033            property = {"model.class.name=com.liferay.portlet.blogs.model.BlogsEntry"}
034    )
035    public class BlogsEntryPermission implements BaseModelPermissionChecker {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, BlogsEntry entry,
039                            String actionId)
040                    throws PortalException {
041    
042                    if (!contains(permissionChecker, entry, actionId)) {
043                            throw new PrincipalException();
044                    }
045            }
046    
047            public static void check(
048                            PermissionChecker permissionChecker, long entryId, String actionId)
049                    throws PortalException {
050    
051                    if (!contains(permissionChecker, entryId, actionId)) {
052                            throw new PrincipalException();
053                    }
054            }
055    
056            public static boolean contains(
057                    PermissionChecker permissionChecker, BlogsEntry entry,
058                    String actionId) {
059    
060                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
061                            permissionChecker, entry.getGroupId(), BlogsEntry.class.getName(),
062                            entry.getEntryId(), PortletKeys.BLOGS, actionId);
063    
064                    if (hasPermission != null) {
065                            return hasPermission.booleanValue();
066                    }
067    
068                    if (entry.isDraft() || entry.isScheduled()) {
069                            if (actionId.equals(ActionKeys.VIEW) &&
070                                    !contains(permissionChecker, entry, ActionKeys.UPDATE)) {
071    
072                                    return false;
073                            }
074                    }
075                    else if (entry.isPending()) {
076                            hasPermission = WorkflowPermissionUtil.hasPermission(
077                                    permissionChecker, entry.getGroupId(),
078                                    BlogsEntry.class.getName(), entry.getEntryId(), actionId);
079    
080                            if (hasPermission != null) {
081                                    return hasPermission.booleanValue();
082                            }
083                    }
084    
085                    if (permissionChecker.hasOwnerPermission(
086                                    entry.getCompanyId(), BlogsEntry.class.getName(),
087                                    entry.getEntryId(), entry.getUserId(), actionId)) {
088    
089                            return true;
090                    }
091    
092                    return permissionChecker.hasPermission(
093                            entry.getGroupId(), BlogsEntry.class.getName(), entry.getEntryId(),
094                            actionId);
095            }
096    
097            public static boolean contains(
098                            PermissionChecker permissionChecker, long entryId, String actionId)
099                    throws PortalException {
100    
101                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(entryId);
102    
103                    return contains(permissionChecker, entry, actionId);
104            }
105    
106            @Override
107            public void checkBaseModel(
108                            PermissionChecker permissionChecker, long groupId, long primaryKey,
109                            String actionId)
110                    throws PortalException {
111    
112                    check(permissionChecker, primaryKey, actionId);
113            }
114    
115    }