001    /**
002     * Copyright (c) 2000-2012 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.security.auth.PrincipalException;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.util.PropsValues;
023    import com.liferay.portlet.journal.model.JournalFolder;
024    import com.liferay.portlet.journal.model.JournalFolderConstants;
025    import com.liferay.portlet.journal.service.JournalFolderLocalServiceUtil;
026    
027    /**
028     * @author Juan Fernández
029     */
030    public class JournalFolderPermission {
031    
032            public static void check(
033                            PermissionChecker permissionChecker, JournalFolder folder,
034                            String actionId)
035                    throws PortalException, SystemException {
036    
037                    if (!contains(permissionChecker, folder, actionId)) {
038                            throw new PrincipalException();
039                    }
040            }
041    
042            public static void check(
043                            PermissionChecker permissionChecker, long groupId, long folderId,
044                            String actionId)
045                    throws PortalException, SystemException {
046    
047                    if (!contains(permissionChecker, groupId, folderId, actionId)) {
048                            throw new PrincipalException();
049                    }
050            }
051    
052            public static boolean contains(
053                            PermissionChecker permissionChecker, JournalFolder folder,
054                            String actionId)
055                    throws PortalException, SystemException {
056    
057                    if (actionId.equals(ActionKeys.ADD_FOLDER)) {
058                            actionId = ActionKeys.ADD_SUBFOLDER;
059                    }
060    
061                    long folderId = folder.getFolderId();
062    
063                    if (actionId.equals(ActionKeys.VIEW)) {
064                            while (folderId !=
065                                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
066    
067                                    folder = JournalFolderLocalServiceUtil.getFolder(folderId);
068    
069                                    folderId = folder.getParentFolderId();
070    
071                                    if (!permissionChecker.hasOwnerPermission(
072                                                    folder.getCompanyId(), JournalFolder.class.getName(),
073                                                    folder.getFolderId(), folder.getUserId(), actionId) &&
074                                            !permissionChecker.hasPermission(
075                                                    folder.getGroupId(), JournalFolder.class.getName(),
076                                                    folder.getFolderId(), actionId)) {
077    
078                                            return false;
079                                    }
080    
081                                    if (!PropsValues.PERMISSIONS_VIEW_DYNAMIC_INHERITANCE) {
082                                            break;
083                                    }
084                            }
085    
086                            return true;
087                    }
088                    else {
089                            while (folderId !=
090                                                    JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
091    
092                                    folder = JournalFolderLocalServiceUtil.getFolder(folderId);
093    
094                                    folderId = folder.getParentFolderId();
095    
096                                    if (permissionChecker.hasOwnerPermission(
097                                                    folder.getCompanyId(), JournalFolder.class.getName(),
098                                                    folder.getFolderId(), folder.getUserId(), actionId)) {
099    
100                                            return true;
101                                    }
102    
103                                    if (permissionChecker.hasPermission(
104                                                    folder.getGroupId(), JournalFolder.class.getName(),
105                                                    folder.getFolderId(), actionId)) {
106    
107                                            return true;
108                                    }
109    
110                                    if (actionId.equals(ActionKeys.VIEW)) {
111                                            break;
112                                    }
113                            }
114    
115                            return false;
116                    }
117            }
118    
119            public static boolean contains(
120                            PermissionChecker permissionChecker, long groupId, long folderId,
121                            String actionId)
122                    throws PortalException, SystemException {
123    
124                    if (folderId == JournalFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
125                            return JournalPermission.contains(
126                                    permissionChecker, groupId, actionId);
127                    }
128                    else {
129                            JournalFolder folder =
130                                    JournalFolderLocalServiceUtil.getJournalFolder(folderId);
131    
132                            return contains(permissionChecker, folder, actionId);
133                    }
134            }
135    
136    }