1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.bookmarks.service.permission;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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.BookmarksFolderConstants;
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 boolean contains(
56              PermissionChecker permissionChecker, BookmarksFolder folder,
57              String actionId)
58          throws PortalException, SystemException {
59  
60          if (actionId.equals(ActionKeys.ADD_FOLDER)) {
61              actionId = ActionKeys.ADD_SUBFOLDER;
62          }
63  
64          long folderId = folder.getFolderId();
65  
66          if (actionId.equals(ActionKeys.VIEW)) {
67              while (folderId !=
68                      BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
69  
70                  folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
71  
72                  folderId = folder.getParentFolderId();
73  
74                  if (!permissionChecker.hasOwnerPermission(
75                          folder.getCompanyId(), BookmarksFolder.class.getName(),
76                          folder.getFolderId(), folder.getUserId(), actionId) &&
77                      !permissionChecker.hasPermission(
78                          folder.getGroupId(), BookmarksFolder.class.getName(),
79                          folder.getFolderId(), actionId)) {
80  
81                      return false;
82                  }
83  
84                  if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
85                      break;
86                  }
87              }
88  
89              return true;
90          }
91          else {
92              while (folderId !=
93                      BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
94  
95                  if (permissionChecker.hasOwnerPermission(
96                          folder.getCompanyId(), BookmarksFolder.class.getName(),
97                          folder.getFolderId(), folder.getUserId(), actionId)) {
98  
99                      return true;
100                 }
101 
102                 if (permissionChecker.hasPermission(
103                         folder.getGroupId(), BookmarksFolder.class.getName(),
104                         folder.getFolderId(), actionId)) {
105 
106                     return true;
107                 }
108 
109                 if (actionId.equals(ActionKeys.VIEW)) {
110                     break;
111                 }
112 
113                 folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
114 
115                 folderId = folder.getParentFolderId();
116             }
117 
118             return false;
119         }
120     }
121 
122     public static boolean contains(
123             PermissionChecker permissionChecker, long groupId, long folderId,
124             String actionId)
125         throws PortalException, SystemException {
126 
127         if (folderId == BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
128             return BookmarksPermission.contains(
129                 permissionChecker, groupId, actionId);
130         }
131         else {
132             BookmarksFolder folder =
133                 BookmarksFolderLocalServiceUtil.getBookmarksFolder(folderId);
134 
135             return contains(permissionChecker, folder, actionId);
136         }
137     }
138 
139 }