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