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