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