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