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