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.documentlibrary.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.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, DLFolder folder,
36              String actionId)
37          throws PortalException, SystemException {
38  
39          if (!contains(permissionChecker, folder, actionId)) {
40              throw new PrincipalException();
41          }
42      }
43  
44      public static void check(
45              PermissionChecker permissionChecker, long groupId, long folderId,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, groupId, folderId, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static boolean contains(
55              PermissionChecker permissionChecker, DLFolder folder,
56              String actionId)
57          throws PortalException, SystemException {
58  
59          if (actionId.equals(ActionKeys.ADD_FOLDER)) {
60              actionId = ActionKeys.ADD_SUBFOLDER;
61          }
62  
63          long folderId = folder.getFolderId();
64  
65          if (actionId.equals(ActionKeys.VIEW)) {
66              while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
67                  folder = DLFolderLocalServiceUtil.getFolder(folderId);
68  
69                  folderId = folder.getParentFolderId();
70  
71                  if (!permissionChecker.hasOwnerPermission(
72                          folder.getCompanyId(), DLFolder.class.getName(),
73                          folder.getFolderId(), folder.getUserId(), actionId) &&
74                      !permissionChecker.hasPermission(
75                          folder.getGroupId(), DLFolder.class.getName(),
76                          folder.getFolderId(), actionId)) {
77  
78                      return false;
79                  }
80  
81                  if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
82                      break;
83                  }
84              }
85  
86              return true;
87          }
88          else {
89              while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
90                  folder = DLFolderLocalServiceUtil.getFolder(folderId);
91  
92                  folderId = folder.getParentFolderId();
93  
94                  if (permissionChecker.hasOwnerPermission(
95                          folder.getCompanyId(), DLFolder.class.getName(),
96                          folder.getFolderId(), folder.getUserId(), actionId)) {
97  
98                      return true;
99                  }
100 
101                 if (permissionChecker.hasPermission(
102                         folder.getGroupId(), DLFolder.class.getName(),
103                         folder.getFolderId(), actionId)) {
104 
105                     return true;
106                 }
107             }
108 
109             return false;
110         }
111     }
112 
113     public static boolean contains(
114             PermissionChecker permissionChecker, long groupId, long folderId,
115             String actionId)
116         throws PortalException, SystemException {
117 
118         if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
119             return DLPermission.contains(permissionChecker, groupId, actionId);
120         }
121         else {
122             DLFolder folder = DLFolderLocalServiceUtil.getFolder(folderId);
123 
124             return contains(permissionChecker, folder, actionId);
125         }
126     }
127 
128 }