Skip to content

Commit

Permalink
OO-5696: validate file selections against current container
Browse files Browse the repository at this point in the history
  • Loading branch information
lainsr committed Sep 6, 2021
1 parent 53086dc commit 418bb50
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
29 changes: 23 additions & 6 deletions src/main/java/org/olat/core/commons/modules/bc/FileSelection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,29 @@
package org.olat.core.commons.modules.bc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.olat.core.gui.UserRequest;
import org.olat.core.util.FileUtils;
import org.olat.core.util.StringHelper;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;

public class FileSelection {

/** HTML form identifier */
public static final String FORM_ID = "paths";

private List<String> files = new ArrayList<>();
private String currentContainerRelPath;
private final List<String> files = new ArrayList<>();
private final String currentContainerRelPath;
private final VFSContainer currentContainer;

public FileSelection(UserRequest ureq, String currentContainerRelPath) {
public FileSelection(UserRequest ureq, VFSContainer currentContainer, String currentContainerRelPath) {
if (currentContainerRelPath.equals("/")) currentContainerRelPath = "";
this.currentContainerRelPath = currentContainerRelPath;
this.currentContainer = currentContainer;
parse(ureq);
}

Expand Down Expand Up @@ -76,8 +81,20 @@ public List<String> getInvalidFileNames() {
*/
private void parse(UserRequest ureq) {
String[] sFiles = ureq.getHttpReq().getParameterValues(FORM_ID);
if (sFiles == null || sFiles.length == 0) return;
files = Arrays.asList(sFiles);
if (sFiles == null || sFiles.length == 0) {
return;
}
List<VFSItem> items = currentContainer.getItems();
if(items != null && !items.isEmpty()) {
Set<String> itemNames = items.stream()
.map(VFSItem::getName)
.collect(Collectors.toSet());
for(String sFile:sFiles) {
if(itemNames.contains(sFile)) {
files.add(sFile);
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected CmdDelete(UserRequest ureq, WindowControl wControl) {
public Controller execute(FolderComponent fc, UserRequest ureq, WindowControl wContr, Translator trans) {
this.translator = trans;
this.folderComponent = fc;
this.fileSelection = new FileSelection(ureq, fc.getCurrentContainerPath());
this.fileSelection = new FileSelection(ureq, fc.getCurrentContainer(), fc.getCurrentContainerPath());

VFSContainer currentContainer = folderComponent.getCurrentContainer();
List<String> lockedFiles = hasLockedFiles(currentContainer, fileSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ public Controller execute(FolderComponent folderComponent, UserRequest ureq, Win
return null;
}

FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
status = FolderCommandHelper.sanityCheck3(wControl, folderComponent, selection);
if(status == FolderCommandStatus.STATUS_FAILED) {
return null;
}

if(selection.getFiles().isEmpty()) {
status = FolderCommandStatus.STATUS_FAILED;
wControl.setWarning(trans.translate("warning.file.selection.empty"));
wControl.setWarning(trans.translate("warning.file.selection.empty22"));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected CmdMoveCopy(WindowControl wControl, boolean move) {
public Controller execute(FolderComponent fc, UserRequest ureq, WindowControl windowControl, Translator trans) {
this.folderComponent = fc;
this.translator = trans;
this.fileSelection = new FileSelection(ureq, fc.getCurrentContainerPath());
this.fileSelection = new FileSelection(ureq, fc.getCurrentContainer(), fc.getCurrentContainerPath());

VelocityContainer main = new VelocityContainer("mc", VELOCITY_ROOT + "/movecopy.html", translator, this);
main.contextPut("fileselection", fileSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CmdUnzip(UserRequest ureq, WindowControl wControl) {
@Override
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wContr, Translator trans) {
this.translator = trans;
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
VFSContainer currentContainer = folderComponent.getCurrentContainer();
if (currentContainer.canWrite() != VFSConstants.YES)
throw new AssertException("Cannot unzip to folder. Writing denied.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Controller execute(FolderComponent folderComponent, UserRequest ureq, Win
return null;
}

selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
status = FolderCommandHelper.sanityCheck3(wControl, folderComponent, selection);
if(status == FolderCommandStatus.STATUS_FAILED) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public Controller execute(FolderComponent folderComponent, UserRequest ureq, Win
if (status == FolderCommandStatus.STATUS_FAILED) {
return null;
}
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainerPath());
FileSelection selection = new FileSelection(ureq, folderComponent.getCurrentContainer(), folderComponent.getCurrentContainerPath());
status = FolderCommandHelper.sanityCheck3(wControl, folderComponent, selection);
if (status == FolderCommandStatus.STATUS_FAILED) {
return null;
Expand Down

0 comments on commit 418bb50

Please sign in to comment.