001    /**
002     * Copyright (c) 2000-2012 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                    long folderId = folder.getFolderId();
064    
065                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
066                            long originalFolderId = folderId;
067    
068                            try {
069                                    while (folderId !=
070                                                            BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
071    
072                                            folder = BookmarksFolderLocalServiceUtil.getFolder(
073                                                    folderId);
074    
075                                            if (!permissionChecker.hasOwnerPermission(
076                                                            folder.getCompanyId(),
077                                                            BookmarksFolder.class.getName(),
078                                                            folder.getFolderId(), folder.getUserId(),
079                                                            actionId) &&
080                                                    !permissionChecker.hasPermission(
081                                                            folder.getGroupId(),
082                                                            BookmarksFolder.class.getName(),
083                                                            folder.getFolderId(), actionId)) {
084    
085                                                    return false;
086                                            }
087    
088                                            folderId = folder.getParentFolderId();
089                                    }
090                            }
091                            catch (NoSuchFolderException nsfe) {
092                                    if (!folder.isInTrash()) {
093                                            throw nsfe;
094                                    }
095                            }
096    
097                            if (actionId.equals(ActionKeys.VIEW)) {
098                                    return true;
099                            }
100    
101                            folderId = originalFolderId;
102                    }
103    
104                    try {
105                            while (folderId !=
106                                                    BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
107    
108                                    folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
109    
110                                    if (permissionChecker.hasOwnerPermission(
111                                                    folder.getCompanyId(), BookmarksFolder.class.getName(),
112                                                    folder.getFolderId(), folder.getUserId(), actionId) ||
113                                            permissionChecker.hasPermission(
114                                                    folder.getGroupId(), BookmarksFolder.class.getName(),
115                                                    folder.getFolderId(), actionId)) {
116    
117                                            return true;
118                                    }
119    
120                                    folderId = folder.getParentFolderId();
121                            }
122                    }
123                    catch (NoSuchFolderException nsfe) {
124                            if (!folder.isInTrash()) {
125                                    throw nsfe;
126                            }
127                    }
128    
129                    return false;
130            }
131    
132            public static boolean contains(
133                            PermissionChecker permissionChecker, long groupId, long folderId,
134                            String actionId)
135                    throws PortalException, SystemException {
136    
137                    if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
138                            return BookmarksPermission.contains(
139                                    permissionChecker, groupId, actionId);
140                    }
141                    else {
142                            BookmarksFolder folder =
143                                    BookmarksFolderLocalServiceUtil.getBookmarksFolder(folderId);
144    
145                            return contains(permissionChecker, folder, actionId);
146                    }
147            }
148    
149    }