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, long fileEntryId,
48              String actionId)
49          throws PortalException, SystemException {
50  
51          if (!contains(permissionChecker, fileEntryId, actionId)) {
52              throw new PrincipalException();
53          }
54      }
55  
56      public static void check(
57              PermissionChecker permissionChecker, DLFileEntry fileEntry,
58              String actionId)
59          throws PortalException, SystemException {
60  
61          if (!contains(permissionChecker, fileEntry, actionId)) {
62              throw new PrincipalException();
63          }
64      }
65  
66      public static boolean contains(
67              PermissionChecker permissionChecker, long folderId, String name,
68              String actionId)
69          throws PortalException, SystemException {
70  
71          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
72              folderId, name);
73  
74          return contains(permissionChecker, fileEntry, actionId);
75      }
76  
77      public static boolean contains(
78              PermissionChecker permissionChecker, long fileEntryId,
79              String actionId)
80          throws PortalException, SystemException {
81  
82          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(
83              fileEntryId);
84  
85          return contains(permissionChecker, fileEntry, actionId);
86      }
87  
88      public static boolean contains(
89              PermissionChecker permissionChecker, DLFileEntry fileEntry,
90              String actionId)
91          throws PortalException, SystemException {
92  
93          DLFolder folder = DLFolderLocalServiceUtil.getFolder(
94              fileEntry.getFolderId());
95  
96          if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
97              if (!DLFolderPermission.contains(
98                      permissionChecker, folder, ActionKeys.VIEW)) {
99  
100                 return false;
101             }
102         }
103 
104         if (permissionChecker.hasOwnerPermission(
105                 fileEntry.getCompanyId(), DLFileEntry.class.getName(),
106                 fileEntry.getFileEntryId(), fileEntry.getUserId(), actionId)) {
107 
108             return true;
109         }
110 
111         return permissionChecker.hasPermission(
112             fileEntry.getGroupId(), DLFileEntry.class.getName(),
113             fileEntry.getFileEntryId(), actionId);
114     }
115 
116 }