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.imagegallery.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.imagegallery.model.IGFolder;
24  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
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 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 boolean contains(
64              PermissionChecker permissionChecker, IGFolder folder,
65              String actionId)
66          throws PortalException, SystemException {
67  
68          if (actionId.equals(ActionKeys.ADD_FOLDER)) {
69              actionId = ActionKeys.ADD_SUBFOLDER;
70          }
71  
72          long folderId = folder.getFolderId();
73  
74          if (actionId.equals(ActionKeys.VIEW)) {
75              while (folderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
76                  folder = IGFolderLocalServiceUtil.getFolder(folderId);
77  
78                  folderId = folder.getParentFolderId();
79  
80                  if (!permissionChecker.hasOwnerPermission(
81                          folder.getCompanyId(), IGFolder.class.getName(),
82                          folder.getFolderId(), folder.getUserId(), actionId) &&
83                      !permissionChecker.hasPermission(
84                          folder.getGroupId(), IGFolder.class.getName(),
85                          folder.getFolderId(), actionId)) {
86  
87                      return false;
88                  }
89  
90                  if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
91                      break;
92                  }
93              }
94  
95              return true;
96          }
97          else {
98              while (folderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
99                  if (permissionChecker.hasOwnerPermission(
100                         folder.getCompanyId(), IGFolder.class.getName(),
101                         folder.getFolderId(), folder.getUserId(), actionId)) {
102 
103                     return true;
104                 }
105 
106                 if (permissionChecker.hasPermission(
107                         folder.getGroupId(), IGFolder.class.getName(),
108                         folder.getFolderId(), actionId)) {
109 
110                     return true;
111                 }
112 
113                 if (actionId.equals(ActionKeys.VIEW)) {
114                     break;
115                 }
116 
117                 folder = IGFolderLocalServiceUtil.getFolder(folderId);
118 
119                 folderId = folder.getParentFolderId();
120             }
121 
122             return false;
123         }
124     }
125 
126     public static boolean contains(
127             PermissionChecker permissionChecker, long groupId, long folderId,
128             String actionId)
129         throws PortalException, SystemException {
130 
131         if (folderId == IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
132             return IGPermission.contains(permissionChecker, groupId, actionId);
133         }
134         else {
135             return contains(permissionChecker, folderId, actionId);
136         }
137     }
138 
139     public static boolean contains(
140             PermissionChecker permissionChecker, long folderId, String actionId)
141         throws PortalException, SystemException {
142 
143         IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
144 
145         return contains(permissionChecker, folder, actionId);
146     }
147 
148 }