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.bookmarks.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.bookmarks.model.BookmarksFolder;
28  import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
29  import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
30  
31  /**
32   * <a href="BookmarksFolderPermission.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   * @author Raymond Augé
36   *
37   */
38  public class BookmarksFolderPermission {
39  
40      public static void check(
41              PermissionChecker permissionChecker, long groupId, long folderId,
42              String actionId)
43          throws PortalException, SystemException {
44  
45          if (!contains(permissionChecker, groupId, folderId, actionId)) {
46              throw new PrincipalException();
47          }
48      }
49  
50      public static void check(
51              PermissionChecker permissionChecker, long folderId,
52              String actionId)
53          throws PortalException, SystemException {
54  
55          if (!contains(permissionChecker, folderId, actionId)) {
56              throw new PrincipalException();
57          }
58      }
59  
60      public static void check(
61              PermissionChecker permissionChecker, BookmarksFolder folder,
62              String actionId)
63          throws PortalException, SystemException {
64  
65          if (!contains(permissionChecker, folder, actionId)) {
66              throw new PrincipalException();
67          }
68      }
69  
70      public static boolean contains(
71              PermissionChecker permissionChecker, long groupId, long folderId,
72              String actionId)
73          throws PortalException, SystemException {
74  
75          if (folderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
76              return BookmarksPermission.contains(
77                  permissionChecker, groupId, actionId);
78          }
79          else {
80              return contains(permissionChecker, folderId, actionId);
81          }
82      }
83  
84      public static boolean contains(
85              PermissionChecker permissionChecker, long folderId,
86              String actionId)
87          throws PortalException, SystemException {
88  
89          BookmarksFolder folder =
90              BookmarksFolderLocalServiceUtil.getFolder(folderId);
91  
92          return contains(permissionChecker, folder, actionId);
93      }
94  
95      public static boolean contains(
96              PermissionChecker permissionChecker, BookmarksFolder folder,
97              String actionId)
98          throws PortalException, SystemException {
99  
100         if (actionId.equals(ActionKeys.ADD_FOLDER)) {
101             actionId = ActionKeys.ADD_SUBFOLDER;
102         }
103 
104         long folderId = folder.getFolderId();
105 
106         while (folderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
107             if (permissionChecker.hasOwnerPermission(
108                     folder.getCompanyId(), BookmarksFolder.class.getName(),
109                     folder.getFolderId(), folder.getUserId(), actionId)) {
110 
111                 return true;
112             }
113 
114             if (permissionChecker.hasPermission(
115                     folder.getGroupId(), BookmarksFolder.class.getName(),
116                     folder.getFolderId(), actionId)) {
117 
118                 return true;
119             }
120 
121             if (actionId.equals(ActionKeys.VIEW)) {
122                 break;
123             }
124 
125             folder = BookmarksFolderLocalServiceUtil.getFolder(folderId);
126 
127             folderId = folder.getParentFolderId();
128         }
129 
130         return false;
131     }
132 
133 }