1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.imagegallery.service.permission;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portlet.imagegallery.model.IGFolder;
28  import com.liferay.portlet.imagegallery.model.impl.IGFolderImpl;
29  import com.liferay.portlet.imagegallery.service.IGFolderLocalServiceUtil;
30  
31  /**
32   * <a href="IGFolderPermission.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
36   */
37  public class IGFolderPermission {
38  
39      public static void check(
40              PermissionChecker permissionChecker, long groupId, long folderId,
41              String actionId)
42          throws PortalException, SystemException {
43  
44          if (!contains(permissionChecker, groupId, folderId, actionId)) {
45              throw new PrincipalException();
46          }
47      }
48  
49      public static void check(
50              PermissionChecker permissionChecker, long folderId, String actionId)
51          throws PortalException, SystemException {
52  
53          if (!contains(permissionChecker, folderId, actionId)) {
54              throw new PrincipalException();
55          }
56      }
57  
58      public static void check(
59              PermissionChecker permissionChecker, IGFolder folder,
60              String actionId)
61          throws PortalException, SystemException {
62  
63          if (!contains(permissionChecker, folder, actionId)) {
64              throw new PrincipalException();
65          }
66      }
67  
68      public static boolean contains(
69              PermissionChecker permissionChecker, long groupId, long folderId,
70              String actionId)
71          throws PortalException, SystemException {
72  
73          if (folderId == IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
74              return IGPermission.contains(permissionChecker, groupId, actionId);
75          }
76          else {
77              return contains(permissionChecker, folderId, actionId);
78          }
79      }
80  
81      public static boolean contains(
82              PermissionChecker permissionChecker, long folderId, String actionId)
83          throws PortalException, SystemException {
84  
85          IGFolder folder = IGFolderLocalServiceUtil.getFolder(folderId);
86  
87          return contains(permissionChecker, folder, actionId);
88      }
89  
90      public static boolean contains(
91              PermissionChecker permissionChecker, IGFolder folder,
92              String actionId)
93          throws PortalException, SystemException {
94  
95          if (actionId.equals(ActionKeys.ADD_FOLDER)) {
96              actionId = ActionKeys.ADD_SUBFOLDER;
97          }
98  
99          long folderId = folder.getFolderId();
100 
101         while (folderId != IGFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
102             if (permissionChecker.hasOwnerPermission(
103                     folder.getCompanyId(), IGFolder.class.getName(),
104                     folder.getFolderId(), folder.getUserId(), actionId)) {
105 
106                 return true;
107             }
108 
109             if (permissionChecker.hasPermission(
110                     folder.getGroupId(), IGFolder.class.getName(),
111                     folder.getFolderId(), actionId)) {
112 
113                 return true;
114             }
115 
116             if (actionId.equals(ActionKeys.VIEW)) {
117                 break;
118             }
119 
120             folder = IGFolderLocalServiceUtil.getFolder(folderId);
121 
122             folderId = folder.getParentFolderId();
123         }
124 
125         return false;
126     }
127 
128 }