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