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.bookmarks.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.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.bookmarks.NoSuchFolderException;
026    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
027    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
028    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
029    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
030    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class BookmarksEntryPermission {
036    
037            public static void check(
038                            PermissionChecker permissionChecker, BookmarksEntry entry,
039                            String actionId)
040                    throws PortalException, SystemException {
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, SystemException {
050    
051                    if (!contains(permissionChecker, entryId, actionId)) {
052                            throw new PrincipalException();
053                    }
054            }
055    
056            public static boolean contains(
057                            PermissionChecker permissionChecker, BookmarksEntry entry,
058                            String actionId)
059                    throws PortalException, SystemException {
060    
061                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
062                            permissionChecker, entry.getGroupId(),
063                            BookmarksEntry.class.getName(), entry.getEntryId(),
064                            PortletKeys.BOOKMARKS, actionId);
065    
066                    if (hasPermission != null) {
067                            return hasPermission.booleanValue();
068                    }
069    
070                    if (actionId.equals(ActionKeys.VIEW) &&
071                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
072    
073                            long folderId = entry.getFolderId();
074    
075                            if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
076                                    if (!BookmarksPermission.contains(
077                                                    permissionChecker, entry.getGroupId(), actionId)) {
078    
079                                            return false;
080                                    }
081                            }
082                            else {
083                                    try {
084                                            BookmarksFolder folder =
085                                                    BookmarksFolderLocalServiceUtil.getFolder(folderId);
086    
087                                            if (!BookmarksFolderPermission.contains(
088                                                            permissionChecker, folder, ActionKeys.ACCESS) &&
089                                                    !BookmarksFolderPermission.contains(
090                                                            permissionChecker, folder, ActionKeys.VIEW)) {
091    
092                                                    return false;
093                                            }
094                                    }
095                                    catch (NoSuchFolderException nsfe) {
096                                            if (!entry.isInTrash()) {
097                                                    throw nsfe;
098                                            }
099                                    }
100                            }
101                    }
102    
103                    if (permissionChecker.hasOwnerPermission(
104                                    entry.getCompanyId(), BookmarksEntry.class.getName(),
105                                    entry.getEntryId(), entry.getUserId(), actionId)) {
106    
107                            return true;
108                    }
109    
110                    return permissionChecker.hasPermission(
111                            entry.getGroupId(), BookmarksEntry.class.getName(),
112                            entry.getEntryId(), actionId);
113            }
114    
115            public static boolean contains(
116                            PermissionChecker permissionChecker, long entryId, String actionId)
117                    throws PortalException, SystemException {
118    
119                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(entryId);
120    
121                    return contains(permissionChecker, entry, actionId);
122            }
123    
124    }