001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.bookmarks.service.permission;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.security.permission.ActionKeys;
026: import com.liferay.portal.kernel.security.permission.PermissionChecker;
027: import com.liferay.portal.security.auth.PrincipalException;
028: import com.liferay.portal.service.permission.PortletPermissionUtil;
029: import com.liferay.portal.util.PortletKeys;
030: import com.liferay.portlet.bookmarks.model.BookmarksFolder;
031: import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderImpl;
032: import com.liferay.portlet.bookmarks.service.BookmarksFolderLocalServiceUtil;
033:
034: /**
035: * <a href="BookmarksFolderPermission.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class BookmarksFolderPermission {
041:
042: public static void check(PermissionChecker permissionChecker,
043: long plid, long folderId, String actionId)
044: throws PortalException, SystemException {
045:
046: if (!contains(permissionChecker, plid, folderId, actionId)) {
047: throw new PrincipalException();
048: }
049: }
050:
051: public static void check(PermissionChecker permissionChecker,
052: long folderId, String actionId) throws PortalException,
053: SystemException {
054:
055: if (!contains(permissionChecker, folderId, actionId)) {
056: throw new PrincipalException();
057: }
058: }
059:
060: public static void check(PermissionChecker permissionChecker,
061: BookmarksFolder folder, String actionId)
062: throws PortalException, SystemException {
063:
064: if (!contains(permissionChecker, folder, actionId)) {
065: throw new PrincipalException();
066: }
067: }
068:
069: public static boolean contains(PermissionChecker permissionChecker,
070: long plid, long folderId, String actionId)
071: throws PortalException, SystemException {
072:
073: if (folderId == BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
074: return PortletPermissionUtil.contains(permissionChecker,
075: plid, PortletKeys.BOOKMARKS, actionId);
076: } else {
077: return contains(permissionChecker, folderId, actionId);
078: }
079: }
080:
081: public static boolean contains(PermissionChecker permissionChecker,
082: long folderId, String actionId) throws PortalException,
083: SystemException {
084:
085: BookmarksFolder folder = BookmarksFolderLocalServiceUtil
086: .getFolder(folderId);
087:
088: return contains(permissionChecker, folder, actionId);
089: }
090:
091: public static boolean contains(PermissionChecker permissionChecker,
092: BookmarksFolder folder, String actionId)
093: throws PortalException, SystemException {
094:
095: if (actionId.equals(ActionKeys.ADD_FOLDER)) {
096: actionId = ActionKeys.ADD_SUBFOLDER;
097: }
098:
099: long folderId = folder.getFolderId();
100:
101: while (folderId != BookmarksFolderImpl.DEFAULT_PARENT_FOLDER_ID) {
102: folder = BookmarksFolderLocalServiceUtil
103: .getFolder(folderId);
104:
105: folderId = folder.getParentFolderId();
106:
107: if (permissionChecker.hasPermission(folder.getGroupId(),
108: BookmarksFolder.class.getName(), folder
109: .getFolderId(), actionId)) {
110:
111: return true;
112: }
113:
114: if (actionId.equals(ActionKeys.VIEW)) {
115: break;
116: }
117: }
118:
119: return false;
120: }
121:
122: }
|