1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.service.permission;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.security.auth.PrincipalException;
28  import com.liferay.portal.security.permission.ActionKeys;
29  import com.liferay.portal.security.permission.PermissionChecker;
30  import com.liferay.portal.service.permission.PortletPermissionUtil;
31  import com.liferay.portal.util.PortletKeys;
32  import com.liferay.portal.util.PropsValues;
33  import com.liferay.portlet.documentlibrary.model.DLFolder;
34  import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
35  import com.liferay.portlet.documentlibrary.service.DLFolderLocalServiceUtil;
36  
37  /**
38   * <a href="DLFolderPermission.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class DLFolderPermission {
43  
44      public static void check(
45              PermissionChecker permissionChecker, long plid, long folderId,
46              String actionId)
47          throws PortalException, SystemException {
48  
49          if (!contains(permissionChecker, plid, folderId, actionId)) {
50              throw new PrincipalException();
51          }
52      }
53  
54      public static void check(
55              PermissionChecker permissionChecker, long folderId, String actionId)
56          throws PortalException, SystemException {
57  
58          if (!contains(permissionChecker, folderId, actionId)) {
59              throw new PrincipalException();
60          }
61      }
62  
63      public static void check(
64              PermissionChecker permissionChecker, DLFolder folder,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (!contains(permissionChecker, folder, actionId)) {
69              throw new PrincipalException();
70          }
71      }
72  
73      public static boolean contains(
74              PermissionChecker permissionChecker, long plid, long folderId,
75              String actionId)
76          throws PortalException, SystemException {
77  
78          if (folderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
79              return PortletPermissionUtil.contains(
80                  permissionChecker, plid, PortletKeys.DOCUMENT_LIBRARY,
81                  actionId);
82          }
83          else {
84              return contains(permissionChecker, folderId, actionId);
85          }
86      }
87  
88      public static boolean contains(
89              PermissionChecker permissionChecker, long folderId, String actionId)
90          throws PortalException, SystemException {
91  
92          DLFolder folder = DLFolderLocalServiceUtil.getFolder(folderId);
93  
94          return contains(permissionChecker, folder, actionId);
95      }
96  
97      public static boolean contains(
98              PermissionChecker permissionChecker, DLFolder folder,
99              String actionId)
100         throws PortalException, SystemException {
101 
102         if (actionId.equals(ActionKeys.ADD_FOLDER)) {
103             actionId = ActionKeys.ADD_SUBFOLDER;
104         }
105 
106         long folderId = folder.getFolderId();
107 
108         if (actionId.equals(ActionKeys.VIEW)) {
109             while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
110                 folder = DLFolderLocalServiceUtil.getFolder(folderId);
111 
112                 folderId = folder.getParentFolderId();
113 
114                 if (!permissionChecker.hasOwnerPermission(
115                         folder.getCompanyId(), DLFolder.class.getName(),
116                         folder.getFolderId(), folder.getUserId(), actionId) &&
117                     !permissionChecker.hasPermission(
118                         folder.getGroupId(), DLFolder.class.getName(),
119                         folder.getFolderId(), actionId)) {
120 
121                     return false;
122                 }
123 
124                 if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
125                     break;
126                 }
127             }
128 
129             return true;
130         }
131         else {
132             while (folderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
133                 folder = DLFolderLocalServiceUtil.getFolder(folderId);
134 
135                 folderId = folder.getParentFolderId();
136 
137                 if (permissionChecker.hasOwnerPermission(
138                         folder.getCompanyId(), DLFolder.class.getName(),
139                         folder.getFolderId(), folder.getUserId(), actionId)) {
140 
141                     return true;
142                 }
143 
144                 if (permissionChecker.hasPermission(
145                         folder.getGroupId(), DLFolder.class.getName(),
146                         folder.getFolderId(), actionId)) {
147 
148                     return true;
149                 }
150             }
151 
152             return false;
153         }
154     }
155 
156 }