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                    long folderId = folder.getFolderId();
075    
076                    if (PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
077                            long originalFolderId = folderId;
078    
079                            try {
080                                    while (folderId !=
081                                                            JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
082    
083                                            folder = JournalFolderLocalServiceUtil.getFolder(folderId);
084    
085                                            if (!permissionChecker.hasOwnerPermission(
086                                                            folder.getCompanyId(),
087                                                            JournalFolder.class.getName(), folderId,
088                                                            folder.getUserId(), ActionKeys.VIEW) &&
089                                                    !permissionChecker.hasPermission(
090                                                            folder.getGroupId(), JournalFolder.class.getName(),
091                                                            folderId, ActionKeys.VIEW)) {
092    
093                                                    return false;
094                                            }
095    
096                                            folderId = folder.getParentFolderId();
097                                    }
098                            }
099                            catch (NoSuchFolderException nsfe) {
100                                    if (!folder.isInTrash()) {
101                                            throw nsfe;
102                                    }
103                            }
104    
105                            if (actionId.equals(ActionKeys.VIEW)) {
106                                    return true;
107                            }
108    
109                            folderId = originalFolderId;
110                    }
111    
112                    try {
113                            while (folderId !=
114                                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
115    
116                                    folder = JournalFolderLocalServiceUtil.getFolder(folderId);
117    
118                                    if (permissionChecker.hasOwnerPermission(
119                                                    folder.getCompanyId(), JournalFolder.class.getName(),
120                                                    folderId, folder.getUserId(), actionId) ||
121                                            permissionChecker.hasPermission(
122                                                    folder.getGroupId(), JournalFolder.class.getName(),
123                                                    folderId, actionId)) {
124    
125                                            return true;
126                                    }
127    
128                                    folderId = folder.getParentFolderId();
129                            }
130                    }
131                    catch (NoSuchFolderException nsfe) {
132                            if (!folder.isInTrash()) {
133                                    throw nsfe;
134                            }
135                    }
136    
137                    return false;
138            }
139    
140            public static boolean contains(
141                            PermissionChecker permissionChecker, long groupId, long folderId,
142                            String actionId)
143                    throws PortalException, SystemException {
144    
145                    if (folderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
146                            return JournalPermission.contains(
147                                    permissionChecker, groupId, actionId);
148                    }
149                    else {
150                            JournalFolder folder =
151                                    JournalFolderLocalServiceUtil.getJournalFolder(folderId);
152    
153                            return contains(permissionChecker, folder, actionId);
154                    }
155            }
156    
157    }