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.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                                    folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
094    
095                                    folderId = folder.getParentFolderId();
096    
097                                    if (permissionChecker.hasOwnerPermission(
098                                                    folder.getCompanyId(), BookmarksFolder.class.getName(),
099                                                    folder.getFolderId(), folder.getUserId(), actionId)) {
100    
101                                            return true;
102                                    }
103    
104                                    if (permissionChecker.hasPermission(
105                                                    folder.getGroupId(), BookmarksFolder.class.getName(),
106                                                    folder.getFolderId(), actionId)) {
107    
108                                            return true;
109                                    }
110    
111                                    if (actionId.equals(ActionKeys.VIEW)) {
112                                            break;
113                                    }
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    }