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