1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.bookmarks.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.security.permission.ActionKeys;
21  import com.liferay.portal.security.permission.PermissionChecker;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
24  import com.liferay.portlet.bookmarks.model.BookmarksFolder;
25  import com.liferay.portlet.bookmarks.model.BookmarksFolderConstants;
26  import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
27  
28  /**
29   * <a href="BookmarksEntryPermission.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class BookmarksEntryPermission {
34  
35      public static void check(
36              PermissionChecker permissionChecker, BookmarksEntry entry,
37              String actionId)
38          throws PortalException, SystemException {
39  
40          if (!contains(permissionChecker, entry, actionId)) {
41              throw new PrincipalException();
42          }
43      }
44  
45      public static void check(
46              PermissionChecker permissionChecker, long entryId,
47              String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, entryId, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public static boolean contains(
56              PermissionChecker permissionChecker, BookmarksEntry entry,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
61              if (entry.getFolderId() !=
62                      BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
63  
64                  BookmarksFolder folder = entry.getFolder();
65  
66                  if (!BookmarksFolderPermission.contains(
67                          permissionChecker, folder, ActionKeys.ACCESS) &&
68                      !BookmarksFolderPermission.contains(
69                          permissionChecker, folder, ActionKeys.VIEW)) {
70  
71                      return false;
72                  }
73              }
74          }
75  
76          if (permissionChecker.hasOwnerPermission(
77                  entry.getCompanyId(), BookmarksEntry.class.getName(),
78                  entry.getEntryId(), entry.getUserId(), actionId)) {
79  
80              return true;
81          }
82  
83          return permissionChecker.hasPermission(
84              entry.getGroupId(), BookmarksEntry.class.getName(),
85              entry.getEntryId(), actionId);
86      }
87  
88      public static boolean contains(
89              PermissionChecker permissionChecker, long entryId,
90              String actionId)
91          throws PortalException, SystemException {
92  
93          BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(entryId);
94  
95          return contains(permissionChecker, entry, actionId);
96      }
97  
98  }