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.BookmarksFolder;
025    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
026    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Raymond Aug??
031     */
032    public class BookmarksFolderPermission {
033    
034            public static void check(
035                            PermissionChecker permissionChecker, BookmarksFolder folder,
036                            String actionId)
037                    throws PortalException, SystemException {
038    
039                    if (!contains(permissionChecker, folder, actionId)) {
040                            throw new PrincipalException();
041                    }
042            }
043    
044            public static void check(
045                            PermissionChecker permissionChecker, long groupId, long folderId,
046                            String actionId)
047                    throws PortalException, SystemException {
048    
049                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
050                            throw new PrincipalException();
051                    }
052            }
053    
054            public static boolean contains(
055                            PermissionChecker permissionChecker, BookmarksFolder folder,
056                            String actionId)
057                    throws PortalException, SystemException {
058    
059                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
060                            actionId = ActionKeys.ADD_SUBFOLDER;
061                    }
062    
063                    if (actionId.equals(ActionKeys.VIEW) &&
064                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
065    
066                            try {
067                                    long folderId = folder.getFolderId();
068    
069                                    while (folderId !=
070                                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
071    
072                                            folder = BookmarksFolderLocalServiceUtil.getFolder(
073                                                    folderId);
074    
075                                            if (!_hasPermission(permissionChecker, folder, actionId)) {
076                                                    return false;
077                                            }
078    
079                                            folderId = folder.getParentFolderId();
080                                    }
081                            }
082                            catch (NoSuchFolderException nsfe) {
083                                    if (!folder.isInTrash()) {
084                                            throw nsfe;
085                                    }
086                            }
087    
088                            return true;
089                    }
090    
091                    return _hasPermission(permissionChecker, folder, actionId);
092            }
093    
094            public static boolean contains(
095                            PermissionChecker permissionChecker, long groupId, long folderId,
096                            String actionId)
097                    throws PortalException, SystemException {
098    
099                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
100                            return BookmarksPermission.contains(
101                                    permissionChecker, groupId, actionId);
102                    }
103                    else {
104                            BookmarksFolder folder =
105                                    BookmarksFolderLocalServiceUtil.getBookmarksFolder(folderId);
106    
107                            return contains(permissionChecker, folder, actionId);
108                    }
109            }
110    
111            private static boolean _hasPermission(
112                    PermissionChecker permissionChecker, BookmarksFolder folder,
113                    String actionId) {
114    
115                    if (permissionChecker.hasOwnerPermission(
116                                    folder.getCompanyId(), BookmarksFolder.class.getName(),
117                                    folder.getFolderId(), folder.getUserId(), actionId) ||
118                            permissionChecker.hasPermission(
119                                    folder.getGroupId(), BookmarksFolder.class.getName(),
120                                    folder.getFolderId(), actionId)) {
121    
122                            return true;
123                    }
124    
125                    return false;
126            }
127    
128    }