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