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