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.documentlibrary.security.permission;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.repository.model.FileEntry;
021    import com.liferay.portal.kernel.repository.model.Folder;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowConstants;
024    import com.liferay.portal.security.permission.BasePermissionPropagator;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
026    import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
029    
030    import java.util.List;
031    
032    import javax.portlet.ActionRequest;
033    
034    /**
035     * @author Jonathan McCann
036     */
037    public class DLPermissionPropagatorImpl extends BasePermissionPropagator {
038    
039            @Override
040            public void propagateRolePermissions(
041                            ActionRequest actionRequest, String className, String primKey,
042                            long[] roleIds)
043                    throws PortalException, SystemException {
044    
045                    if (!className.equals(DLFolder.class.getName())) {
046                            return;
047                    }
048    
049                    long folderId = GetterUtil.getLong(primKey);
050    
051                    Folder folder = DLAppServiceUtil.getFolder(folderId);
052    
053                    List<Object> foldersAndFileEntriesAndFileShortcuts =
054                            DLAppServiceUtil.getFoldersAndFileEntriesAndFileShortcuts(
055                                    folder.getGroupId(), folderId, WorkflowConstants.STATUS_ANY,
056                                    true, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
057    
058                    for (Object folderAndFileEntryAndFileShortcut :
059                                    foldersAndFileEntriesAndFileShortcuts) {
060    
061                            if (folderAndFileEntryAndFileShortcut instanceof DLFileShortcut) {
062                                    DLFileShortcut dlFileShorcut =
063                                            (DLFileShortcut)folderAndFileEntryAndFileShortcut;
064    
065                                    for (long roleId : roleIds) {
066                                            propagateRolePermissions(
067                                                    actionRequest, roleId, DLFolder.class.getName(),
068                                                    folderId, DLFileShortcut.class.getName(),
069                                                    dlFileShorcut.getFileShortcutId());
070                                    }
071                            }
072                            else if (folderAndFileEntryAndFileShortcut instanceof FileEntry) {
073                                    FileEntry fileEntry =
074                                            (FileEntry)folderAndFileEntryAndFileShortcut;
075    
076                                    for (long roleId : roleIds) {
077                                            propagateRolePermissions(
078                                                    actionRequest, roleId, DLFolder.class.getName(),
079                                                    folderId, DLFileEntry.class.getName(),
080                                                    fileEntry.getFileEntryId());
081                                    }
082                            }
083                            else if (folderAndFileEntryAndFileShortcut instanceof Folder) {
084                                    Folder subfolder = (Folder)folderAndFileEntryAndFileShortcut;
085    
086                                    for (long roleId : roleIds) {
087                                            propagateRolePermissions(
088                                                    actionRequest, roleId, DLFolder.class.getName(),
089                                                    folderId, DLFolder.class.getName(),
090                                                    subfolder.getFolderId());
091                                    }
092    
093                                    propagateRolePermissions(
094                                            actionRequest, className,
095                                            String.valueOf(subfolder.getFolderId()), roleIds);
096                            }
097                    }
098            }
099    
100    }