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.imagegallery.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.imagegallery.model.IGFolder;
24  import com.liferay.portlet.imagegallery.model.IGFolderConstants;
25  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
26  
27  /**
28   * <a href="IGFolderPermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class IGFolderPermission {
33  
34      public static void check(
35              PermissionChecker permissionChecker, IGFolder 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, IGFolder 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 != IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
67                  folder = IGFolderLocalServiceUtil.getFolder(folderId);
68  
69                  folderId = folder.getParentFolderId();
70  
71                  if (!permissionChecker.hasOwnerPermission(
72                          folder.getCompanyId(), IGFolder.class.getName(),
73                          folder.getFolderId(), folder.getUserId(), actionId) &&
74                      !permissionChecker.hasPermission(
75                          folder.getGroupId(), IGFolder.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 != IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
90                  if (permissionChecker.hasOwnerPermission(
91                          folder.getCompanyId(), IGFolder.class.getName(),
92                          folder.getFolderId(), folder.getUserId(), actionId)) {
93  
94                      return true;
95                  }
96  
97                  if (permissionChecker.hasPermission(
98                          folder.getGroupId(), IGFolder.class.getName(),
99                          folder.getFolderId(), actionId)) {
100 
101                     return true;
102                 }
103 
104                 if (actionId.equals(ActionKeys.VIEW)) {
105                     break;
106                 }
107 
108                 folder = IGFolderLocalServiceUtil.getFolder(folderId);
109 
110                 folderId = folder.getParentFolderId();
111             }
112 
113             return false;
114         }
115     }
116 
117     public static boolean contains(
118             PermissionChecker permissionChecker, long groupId, long folderId,
119             String actionId)
120         throws PortalException, SystemException {
121 
122         if (folderId == IGFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
123             return IGPermission.contains(permissionChecker, groupId, actionId);
124         }
125         else {
126             IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
127 
128             return contains(permissionChecker, folder, actionId);
129         }
130     }
131 
132 }