001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.service.permission;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.staging.permission.StagingPermissionUtil;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.util.PortletKeys;
024    import com.liferay.portal.util.PropsValues;
025    import com.liferay.portlet.journal.NoSuchFolderException;
026    import com.liferay.portlet.journal.model.JournalFolder;
027    import com.liferay.portlet.journal.model.JournalFolderConstants;
028    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
029    
030    /**
031     * @author Juan Fern??ndez
032     * @author Zsolt Berentey
033     */
034    public class JournalFolderPermission {
035    
036            public static void check(
037                            PermissionChecker permissionChecker, JournalFolder folder,
038                            String actionId)
039                    throws PortalException, SystemException {
040    
041                    if (!contains(permissionChecker, folder, actionId)) {
042                            throw new PrincipalException();
043                    }
044            }
045    
046            public static void check(
047                            PermissionChecker permissionChecker, long groupId, long folderId,
048                            String actionId)
049                    throws PortalException, SystemException {
050    
051                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
052                            throw new PrincipalException();
053                    }
054            }
055    
056            public static boolean contains(
057                            PermissionChecker permissionChecker, JournalFolder folder,
058                            String actionId)
059                    throws PortalException, SystemException {
060    
061                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
062                            actionId = ActionKeys.ADD_SUBFOLDER;
063                    }
064    
065                    Boolean hasPermission = StagingPermissionUtil.hasPermission(
066                            permissionChecker, folder.getGroupId(),
067                            JournalFolder.class.getName(), folder.getFolderId(),
068                            PortletKeys.JOURNAL, actionId);
069    
070                    if (hasPermission != null) {
071                            return hasPermission.booleanValue();
072                    }
073    
074                    if (actionId.equals(ActionKeys.VIEW) &&
075                            PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
076    
077                            try {
078                                    long folderId = folder.getFolderId();
079    
080                                    while (folderId !=
081                                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
082    
083                                            folder = JournalFolderLocalServiceUtil.getFolder(folderId);
084    
085                                            if (!_hasPermission(permissionChecker, folder, actionId)) {
086                                                    return false;
087                                            }
088    
089                                            folderId = folder.getParentFolderId();
090                                    }
091                            }
092                            catch (NoSuchFolderException nsfe) {
093                                    if (!folder.isInTrash()) {
094                                            throw nsfe;
095                                    }
096                            }
097    
098                            return JournalPermission.contains(
099                                    permissionChecker, folder.getGroupId(), actionId);
100                    }
101    
102                    return _hasPermission(permissionChecker, folder, actionId);
103            }
104    
105            public static boolean contains(
106                            PermissionChecker permissionChecker, long groupId, long folderId,
107                            String actionId)
108                    throws PortalException, SystemException {
109    
110                    if (folderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
111                            return JournalPermission.contains(
112                                    permissionChecker, groupId, actionId);
113                    }
114                    else {
115                            JournalFolder folder =
116                                    JournalFolderLocalServiceUtil.getJournalFolder(folderId);
117    
118                            return contains(permissionChecker, folder, actionId);
119                    }
120            }
121    
122            private static boolean _hasPermission(
123                    PermissionChecker permissionChecker, JournalFolder folder,
124                    String actionId) {
125    
126                    if (permissionChecker.hasOwnerPermission(
127                                    folder.getCompanyId(), JournalFolder.class.getName(),
128                                    folder.getFolderId(), folder.getUserId(), actionId) ||
129                            permissionChecker.hasPermission(
130                                    folder.getGroupId(), JournalFolder.class.getName(),
131                                    folder.getFolderId(), actionId)) {
132    
133                            return true;
134                    }
135    
136                    return false;
137            }
138    
139    }