1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.permission;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.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.documentlibrary.model.DLFileEntry;
24  import com.liferay.portlet.documentlibrary.model.DLFolder;
25  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
26  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
27  
28  /**
29   * <a href="DLFileEntryPermission.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   * @author Charles May
33   */
34  public class DLFileEntryPermission {
35  
36      public static void check(
37              PermissionChecker permissionChecker, long folderId, String name,
38              String actionId)
39          throws PortalException, SystemException {
40  
41          if (!contains(permissionChecker, folderId, name, actionId)) {
42              throw new PrincipalException();
43          }
44      }
45  
46      public static void check(
47              PermissionChecker permissionChecker, DLFileEntry fileEntry,
48              String actionId)
49          throws PortalException, SystemException {
50  
51          if (!contains(permissionChecker, fileEntry, actionId)) {
52              throw new PrincipalException();
53          }
54      }
55  
56      public static boolean contains(
57              PermissionChecker permissionChecker, long folderId, String name,
58              String actionId)
59          throws PortalException, SystemException {
60  
61          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
62              folderId, name);
63  
64          return contains(permissionChecker, fileEntry, actionId);
65      }
66  
67      public static boolean contains(
68              PermissionChecker permissionChecker, DLFileEntry fileEntry,
69              String actionId)
70          throws PortalException, SystemException {
71  
72          DLFolder folder = DLFolderLocalServiceUtil.getFolder(
73              fileEntry.getFolderId());
74  
75          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
76              if (!DLFolderPermission.contains(
77                      permissionChecker, folder, ActionKeys.VIEW)) {
78  
79                  return false;
80              }
81          }
82  
83          if (permissionChecker.hasOwnerPermission(
84                  fileEntry.getCompanyId(), DLFileEntry.class.getName(),
85                  fileEntry.getFileEntryId(), fileEntry.getUserId(), actionId)) {
86  
87              return true;
88          }
89  
90          return permissionChecker.hasPermission(
91              fileEntry.getGroupId(), DLFileEntry.class.getName(),
92              fileEntry.getFileEntryId(), actionId);
93      }
94  
95  }