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.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, String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, entryId, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static boolean contains(
053                            PermissionChecker permissionChecker, BookmarksEntry entry,
054                            String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
058                            if (entry.getFolderId() !=
059                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
060    
061                                    BookmarksFolder folder = entry.getFolder();
062    
063                                    if (!BookmarksFolderPermission.contains(
064                                                    permissionChecker, folder, ActionKeys.ACCESS) &&
065                                            !BookmarksFolderPermission.contains(
066                                                    permissionChecker, folder, ActionKeys.VIEW)) {
067    
068                                            return false;
069                                    }
070                            }
071                    }
072    
073                    if (permissionChecker.hasOwnerPermission(
074                                    entry.getCompanyId(), BookmarksEntry.class.getName(),
075                                    entry.getEntryId(), entry.getUserId(), actionId)) {
076    
077                            return true;
078                    }
079    
080                    return permissionChecker.hasPermission(
081                            entry.getGroupId(), BookmarksEntry.class.getName(),
082                            entry.getEntryId(), actionId);
083            }
084    
085            public static boolean contains(
086                            PermissionChecker permissionChecker, long entryId, String actionId)
087                    throws PortalException, SystemException {
088    
089                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(entryId);
090    
091                    return contains(permissionChecker, entry, actionId);
092            }
093    
094    }