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.bookmarks.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.bookmarks.model.BookmarksFolder;
24  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
25  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
26  
27  /**
28   * <a href="BookmarksFolderPermission.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   * @author Raymond Augé
32   */
33  public class BookmarksFolderPermission {
34  
35      public static void check(
36              PermissionChecker permissionChecker, BookmarksFolder folder,
37              String actionId)
38          throws PortalException, SystemException {
39  
40          if (!contains(permissionChecker, folder, actionId)) {
41              throw new PrincipalException();
42          }
43      }
44  
45      public static void check(
46              PermissionChecker permissionChecker, long groupId, long folderId,
47              String actionId)
48          throws PortalException, SystemException {
49  
50          if (!contains(permissionChecker, groupId, folderId, actionId)) {
51              throw new PrincipalException();
52          }
53      }
54  
55      public static void check(
56              PermissionChecker permissionChecker, long folderId,
57              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, BookmarksFolder 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 != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
78                  folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
79  
80                  folderId = folder.getParentFolderId();
81  
82                  if (!permissionChecker.hasOwnerPermission(
83                          folder.getCompanyId(), BookmarksFolder.class.getName(),
84                          folder.getFolderId(), folder.getUserId(), actionId) &&
85                      !permissionChecker.hasPermission(
86                          folder.getGroupId(), BookmarksFolder.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 != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
101                 if (permissionChecker.hasOwnerPermission(
102                         folder.getCompanyId(), BookmarksFolder.class.getName(),
103                         folder.getFolderId(), folder.getUserId(), actionId)) {
104 
105                     return true;
106                 }
107 
108                 if (permissionChecker.hasPermission(
109                         folder.getGroupId(), BookmarksFolder.class.getName(),
110                         folder.getFolderId(), actionId)) {
111 
112                     return true;
113                 }
114 
115                 if (actionId.equals(ActionKeys.VIEW)) {
116                     break;
117                 }
118 
119                 folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
120 
121                 folderId = folder.getParentFolderId();
122             }
123 
124             return false;
125         }
126     }
127 
128     public static boolean contains(
129             PermissionChecker permissionChecker, long groupId, long folderId,
130             String actionId)
131         throws PortalException, SystemException {
132 
133         if (folderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
134             return BookmarksPermission.contains(
135                 permissionChecker, groupId, actionId);
136         }
137         else {
138             return contains(permissionChecker, folderId, actionId);
139         }
140     }
141 
142     public static boolean contains(
143             PermissionChecker permissionChecker, long folderId,
144             String actionId)
145         throws PortalException, SystemException {
146 
147         BookmarksFolder folder =
148             BookmarksFolderLocalServiceUtil.getFolder(folderId);
149 
150         return contains(permissionChecker, folder, actionId);
151     }
152 
153 }