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