001    /**
002     * Copyright (c) 2000-present 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.spring.osgi.OSGiBeanProperties;
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.BaseModelPermissionChecker;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.security.permission.ResourcePermissionChecker;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.bookmarks.NoSuchFolderException;
028    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
029    import com.liferay.portlet.bookmarks.model.BookmarksFolder;
030    import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
031    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
032    import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    @OSGiBeanProperties(
038            property = {
039                    "model.class.name=com.liferay.portlet.bookmarks.model.BookmarksEntry"
040            }
041    )
042    public class BookmarksEntryPermission
043            implements BaseModelPermissionChecker, ResourcePermissionChecker {
044    
045            public static void check(
046                            PermissionChecker permissionChecker, BookmarksEntry entry,
047                            String actionId)
048                    throws PortalException {
049    
050                    if (!contains(permissionChecker, entry, actionId)) {
051                            throw new PrincipalException();
052                    }
053            }
054    
055            public static void check(
056                            PermissionChecker permissionChecker, long entryId, String actionId)
057                    throws PortalException {
058    
059                    if (!contains(permissionChecker, entryId, actionId)) {
060                            throw new PrincipalException();
061                    }
062            }
063    
064            public static boolean contains(
065                            PermissionChecker permissionChecker, BookmarksEntry entry,
066                            String actionId)
067                    throws PortalException {
068    
069                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
070                            permissionChecker, entry.getGroupId(),
071                            BookmarksEntry.class.getName(), entry.getEntryId(),
072                            PortletKeys.BOOKMARKS, actionId);
073    
074                    if (hasPermission != null) {
075                            return hasPermission.booleanValue();
076                    }
077    
078                    if (actionId.equals(ActionKeys.VIEW) &&
079                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
080    
081                            long folderId = entry.getFolderId();
082    
083                            if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
084                                    if (!BookmarksPermission.contains(
085                                                    permissionChecker, entry.getGroupId(), actionId)) {
086    
087                                            return false;
088                                    }
089                            }
090                            else {
091                                    try {
092                                            BookmarksFolder folder =
093                                                    BookmarksFolderLocalServiceUtil.getFolder(folderId);
094    
095                                            if (!BookmarksFolderPermission.contains(
096                                                            permissionChecker, folder, ActionKeys.ACCESS) &&
097                                                    !BookmarksFolderPermission.contains(
098                                                            permissionChecker, folder, ActionKeys.VIEW)) {
099    
100                                                    return false;
101                                            }
102                                    }
103                                    catch (NoSuchFolderException nsfe) {
104                                            if (!entry.isInTrash()) {
105                                                    throw nsfe;
106                                            }
107                                    }
108                            }
109                    }
110    
111                    if (permissionChecker.hasOwnerPermission(
112                                    entry.getCompanyId(), BookmarksEntry.class.getName(),
113                                    entry.getEntryId(), entry.getUserId(), actionId)) {
114    
115                            return true;
116                    }
117    
118                    return permissionChecker.hasPermission(
119                            entry.getGroupId(), BookmarksEntry.class.getName(),
120                            entry.getEntryId(), actionId);
121            }
122    
123            public static boolean contains(
124                            PermissionChecker permissionChecker, long entryId, String actionId)
125                    throws PortalException {
126    
127                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(entryId);
128    
129                    return contains(permissionChecker, entry, actionId);
130            }
131    
132            @Override
133            public void checkBaseModel(
134                            PermissionChecker permissionChecker, long groupId, long primaryKey,
135                            String actionId)
136                    throws PortalException {
137    
138                    check(permissionChecker, primaryKey, actionId);
139            }
140    
141            @Override
142            public Boolean checkResource(
143                            PermissionChecker permissionChecker, long classPK, String actionId)
144                    throws PortalException {
145    
146                    return contains(permissionChecker, classPK, actionId);
147            }
148    
149    }