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.DLFolder;
24  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
25  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
26  
27  /**
28   * <a href="DLFolderPermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class DLFolderPermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, long groupId, long folderId,
36              String actionId)
37          throws PortalException, SystemException {
38  
39          if (!contains(permissionChecker, groupId, folderId, actionId)) {
40              throw new PrincipalException();
41          }
42      }
43  
44      public static void check(
45              PermissionChecker permissionChecker, long folderId, String actionId)
46          throws PortalException, SystemException {
47  
48          if (!contains(permissionChecker, folderId, actionId)) {
49              throw new PrincipalException();
50          }
51      }
52  
53      public static void check(
54              PermissionChecker permissionChecker, DLFolder folder,
55              String actionId)
56          throws PortalException, SystemException {
57  
58          if (!contains(permissionChecker, folder, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public static boolean contains(
64              PermissionChecker permissionChecker, long groupId, long folderId,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
69              return DLPermission.contains(permissionChecker, groupId, actionId);
70          }
71          else {
72              return contains(permissionChecker, folderId, actionId);
73          }
74      }
75  
76      public static boolean contains(
77              PermissionChecker permissionChecker, long folderId, String actionId)
78          throws PortalException, SystemException {
79  
80          DLFolder folder = DLFolderLocalServiceUtil.getFolder(folderId);
81  
82          return contains(permissionChecker, folder, actionId);
83      }
84  
85      public static boolean contains(
86              PermissionChecker permissionChecker, DLFolder folder,
87              String actionId)
88          throws PortalException, SystemException {
89  
90          if (actionId.equals(ActionKeys.ADD_FOLDER)) {
91              actionId = ActionKeys.ADD_SUBFOLDER;
92          }
93  
94          long folderId = folder.getFolderId();
95  
96          if (actionId.equals(ActionKeys.VIEW)) {
97              while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
98                  folder = DLFolderLocalServiceUtil.getFolder(folderId);
99  
100                 folderId = folder.getParentFolderId();
101 
102                 if (!permissionChecker.hasOwnerPermission(
103                         folder.getCompanyId(), DLFolder.class.getName(),
104                         folder.getFolderId(), folder.getUserId(), actionId) &&
105                     !permissionChecker.hasPermission(
106                         folder.getGroupId(), DLFolder.class.getName(),
107                         folder.getFolderId(), actionId)) {
108 
109                     return false;
110                 }
111 
112                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
113                     break;
114                 }
115             }
116 
117             return true;
118         }
119         else {
120             while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
121                 folder = DLFolderLocalServiceUtil.getFolder(folderId);
122 
123                 folderId = folder.getParentFolderId();
124 
125                 if (permissionChecker.hasOwnerPermission(
126                         folder.getCompanyId(), DLFolder.class.getName(),
127                         folder.getFolderId(), folder.getUserId(), actionId)) {
128 
129                     return true;
130                 }
131 
132                 if (permissionChecker.hasPermission(
133                         folder.getGroupId(), DLFolder.class.getName(),
134                         folder.getFolderId(), actionId)) {
135 
136                     return true;
137                 }
138             }
139 
140             return false;
141         }
142     }
143 
144 }