Skip to content
Snippets Groups Projects
SharedFolderWebDAVMergeSource.java 5.02 KiB
Newer Older
/**
 * <a href="http://www.openolat.org">
 * OpenOLAT - Online Learning and Training</a><br>
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at the
 * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Initial code contributed and copyrighted by<br>
 * frentix GmbH, http://www.frentix.com
 * <p>
 */
package org.olat.modules.sharedfolder;

import static org.olat.modules.sharedfolder.SharedFolderWebDAVProvider.readOnlyCallback;

import java.util.ArrayList;
import java.util.HashSet;
import org.olat.core.commons.services.webdav.manager.WebDAVMergeSource;
import org.olat.core.id.Identity;
import org.olat.core.id.Roles;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.fileresource.types.SharedFolderFileResource;
import org.olat.repository.RepositoryEntry;
import org.olat.repository.RepositoryManager;


/**
 * Delivery the shared folders configuered in SharedFolderWebDAVProvider
 * 
 * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
 */
public class SharedFolderWebDAVMergeSource extends WebDAVMergeSource {
	private static final OLog log = Tracing.createLoggerFor(SharedFolderWebDAVMergeSource.class);

	private final List<String> publiclyReadableFolders;
	
	public SharedFolderWebDAVMergeSource(Identity identity, List<String> publiclyReadableFolders) {
		this.publiclyReadableFolders = publiclyReadableFolders;
	}

	@Override
	protected List<VFSContainer> loadMergedContainers() {
		SharedFolderManager sfm = SharedFolderManager.getInstance();
		RepositoryManager repoManager = RepositoryManager.getInstance();
		List<VFSContainer> containers = new ArrayList<>();
		Set<Long> addedEntries = new HashSet<>();
		List<RepositoryEntry> ownerEntries = (List<RepositoryEntry>) repoManager.queryByOwner(getIdentity(), SharedFolderFileResource.TYPE_NAME);
		for (RepositoryEntry entry : ownerEntries) {
			VFSContainer container = sfm.getNamedSharedFolder(entry, true);
			addContainerToList(container, containers);
			addedEntries.add(entry.getKey());
		}

		// see /olat3/webapp/WEB-INF/olat_extensions.xml
		if (publiclyReadableFolders != null && publiclyReadableFolders.size() > 0) {
			// Temporarily save added entries. This is needed to make sure not to add an entry twice.
			String firstItem = publiclyReadableFolders.get(0);
			// If the first value in the list is '*', list all resource folders.
			if (firstItem != null && firstItem.equals("*")) {
				// fake role that represents normally logged in user
				Roles registeredUserRole = new Roles(false, false, false, false, false, false, false);
				List<String> types = Collections.singletonList(SharedFolderFileResource.TYPE_NAME);
				List<RepositoryEntry> allEntries = repoManager.queryByTypeLimitAccess(getIdentity(), types, registeredUserRole);
				for (RepositoryEntry entry : allEntries) {
					addReadonlyFolder(entry, sfm, addedEntries, containers);
				}
			} else {
				// only list the specified folders
				List<Long> publiclyReadableFoldersKeys = getSharedKeys();	
				List<RepositoryEntry> entries = repoManager.lookupRepositoryEntries(publiclyReadableFoldersKeys);
				for (RepositoryEntry entry:entries) {
					if (entry.getAccess() >= RepositoryEntry.ACC_USERS || (entry.getAccess() == RepositoryEntry.ACC_OWNERS && entry.isMembersOnly())) {
						// add folder (which is a repo entry) to root container if not present
						addReadonlyFolder(entry, sfm, addedEntries, containers);
					} else {
						log.warn("Access denied on entry::" + entry.getKey(), null);
					}
				}
			}
		}

	private void addReadonlyFolder(RepositoryEntry entry, SharedFolderManager sfm,
			Set<Long> addedEntries, List<VFSContainer> containers) {
		//
		if (!addedEntries.contains(entry.getKey())) {
			// add the entry (readonly)
			VFSContainer folder = sfm.getNamedSharedFolder(entry, true);
			folder.setLocalSecurityCallback(readOnlyCallback);
			addContainerToList(folder, containers);
			addedEntries.add(entry.getKey());
		}
	}
	
	
	private List<Long> getSharedKeys() {
		List<Long> publiclyReadableFoldersKeys = new ArrayList<Long>();
		for (String folder : publiclyReadableFolders) {
			try {
				Long repoKey = Long.parseLong(folder);
				publiclyReadableFoldersKeys.add(repoKey);
			} catch (NumberFormatException e) {
				// Invalid id name
				log.warn("The list item::" + folder + " of publiclyReadableFolders is invalid. Should be repsitoryEntryId or '*'.", e);
			}
		}
		return publiclyReadableFoldersKeys;	
	}
}