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 (entry.getFolderId() !=
060                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
061    
062                            try {
063                                    BookmarksFolder folder =
064                                            BookmarksFolderLocalServiceUtil.getFolder(
065                                                    entry.getFolderId());
066    
067                                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE &&
068                                            !BookmarksFolderPermission.contains(
069                                                    permissionChecker, folder, ActionKeys.ACCESS) &&
070                                            !BookmarksFolderPermission.contains(
071                                                    permissionChecker, folder, ActionKeys.VIEW)) {
072    
073                                            return false;
074                                    }
075    
076                                    if (BookmarksFolderPermission.contains(
077                                                    permissionChecker, folder, actionId)) {
078    
079                                            return true;
080                                    }
081                            }
082                            catch (NoSuchFolderException nsfe) {
083                                    if (!entry.isInTrash()) {
084                                            throw nsfe;
085                                    }
086                            }
087                    }
088    
089                    if (permissionChecker.hasOwnerPermission(
090                                    entry.getCompanyId(), BookmarksEntry.class.getName(),
091                                    entry.getEntryId(), entry.getUserId(), actionId)) {
092    
093                            return true;
094                    }
095    
096                    return permissionChecker.hasPermission(
097                            entry.getGroupId(), BookmarksEntry.class.getName(),
098                            entry.getEntryId(), actionId);
099            }
100    
101            public static boolean contains(
102                            PermissionChecker permissionChecker, long entryId, String actionId)
103                    throws PortalException, SystemException {
104    
105                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(entryId);
106    
107                    return contains(permissionChecker, entry, actionId);
108            }
109    
110    }