File size: 10,396 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <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>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <hr>
* <a href="http://www.openolat.org">
* OpenOLAT - Online Learning and Training</a><br>
* This file has been modified by the OpenOLAT community. Changes are licensed
* under the Apache 2.0 license as the original file.
* <p>
*/
package org.olat.core.commons.modules.bc.commands;
import java.util.ArrayList;
import java.util.List;
import org.olat.core.commons.modules.bc.FileSelection;
import org.olat.core.commons.modules.bc.FolderEvent;
import org.olat.core.commons.modules.bc.components.FolderComponent;
import org.olat.core.commons.services.notifications.NotificationsManager;
import org.olat.core.commons.services.notifications.SubscriptionContext;
import org.olat.core.commons.services.vfs.VFSRepositoryService;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.components.link.Link;
import org.olat.core.gui.components.link.LinkFactory;
import org.olat.core.gui.components.tree.MenuTree;
import org.olat.core.gui.components.velocity.VelocityContainer;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.DefaultController;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.generic.folder.FolderTreeModel;
import org.olat.core.gui.translator.Translator;
import org.olat.core.util.Util;
import org.olat.core.util.vfs.VFSConstants;
import org.olat.core.util.vfs.VFSContainer;
import org.olat.core.util.vfs.VFSItem;
import org.olat.core.util.vfs.VFSLeaf;
import org.olat.core.util.vfs.VFSLockApplicationType;
import org.olat.core.util.vfs.VFSLockManager;
import org.olat.core.util.vfs.VFSManager;
import org.olat.core.util.vfs.VFSStatus;
import org.olat.core.util.vfs.callbacks.VFSSecurityCallback;
import org.olat.core.util.vfs.filters.VFSItemFilter;
import org.springframework.beans.factory.annotation.Autowired;
public class CmdMoveCopy extends DefaultController implements FolderCommand {
private static final String VELOCITY_ROOT = Util.getPackageVelocityRoot(CmdMoveCopy.class);
private static int status = FolderCommandStatus.STATUS_SUCCESS;
private Translator translator;
private MenuTree selTree;
private FileSelection fileSelection;
private Link selectButton, cancelButton;
private FolderComponent folderComponent;
private final boolean move;
@Autowired
private VFSLockManager vfsLockManager;
@Autowired
private VFSRepositoryService vfsRepositoryService;
@Autowired
private NotificationsManager notificationsManager;
protected CmdMoveCopy(WindowControl wControl, boolean move) {
super(wControl);
this.move = move;
}
@Override
public Controller execute(FolderComponent fc, UserRequest ureq, WindowControl windowControl, Translator trans) {
this.folderComponent = fc;
this.translator = trans;
// BUG: CWE-22 Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
// this.fileSelection = new FileSelection(ureq, fc.getCurrentContainerPath());
// FIXED:
this.fileSelection = new FileSelection(ureq, fc.getCurrentContainer(), fc.getCurrentContainerPath());
VelocityContainer main = new VelocityContainer("mc", VELOCITY_ROOT + "/movecopy.html", translator, this);
main.contextPut("fileselection", fileSelection);
//check if command is executed on a file list containing invalid filenames or paths
if(!fileSelection.getInvalidFileNames().isEmpty()) {
main.contextPut("invalidFileNames", fileSelection.getInvalidFileNames());
}
selTree = new MenuTree(null, "seltree", this);
FolderTreeModel ftm = new FolderTreeModel(ureq.getLocale(), fc.getRootContainer(),
true, false, true, fc.getRootContainer().canWrite() == VFSConstants.YES, new EditableFilter());
selTree.setTreeModel(ftm);
selectButton = LinkFactory.createButton(move ? "move" : "copy", main, this);
cancelButton = LinkFactory.createButton("cancel", main, this);
main.put("seltree", selTree);
if (move) {
main.contextPut("move", Boolean.TRUE);
}
setInitialComponent(main);
return this;
}
public boolean isMoved() {
return move;
}
public FileSelection getFileSelection() {
return fileSelection;
}
@Override
public int getStatus() {
return status;
}
@Override
public boolean runsModal() {
return false;
}
@Override
public String getModalTitle() {
return null;
}
public String getTarget() {
FolderTreeModel ftm = (FolderTreeModel) selTree.getTreeModel();
return ftm.getSelectedPath(selTree.getSelectedNode());
}
@Override
public void event(UserRequest ureq, Component source, Event event) {
if(cancelButton == source) {
status = FolderCommandStatus.STATUS_CANCELED;
fireEvent(ureq, FOLDERCOMMAND_FINISHED);
} else if (selectButton == source) {
doMove(ureq);
}
}
private void doMove(UserRequest ureq) {
FolderTreeModel ftm = (FolderTreeModel) selTree.getTreeModel();
String selectedPath = ftm.getSelectedPath(selTree.getSelectedNode());
if (selectedPath == null) {
abortFailed(ureq, "failed");
return;
}
VFSStatus vfsStatus = VFSConstants.SUCCESS;
VFSContainer rootContainer = folderComponent.getRootContainer();
VFSItem vfsItem = rootContainer.resolve(selectedPath);
if (vfsItem == null || (vfsItem.canWrite() != VFSConstants.YES)) {
abortFailed(ureq, "failed");
return;
}
// copy the files
VFSContainer target = (VFSContainer)vfsItem;
List<VFSItem> sources = getSanityCheckedSourceItems(target, ureq);
if (sources == null) return;
for (VFSItem vfsSource:sources) {
VFSItem targetFile = target.resolve(vfsSource.getName());
if(vfsSource instanceof VFSLeaf && targetFile != null && targetFile.canVersion() == VFSConstants.YES) {
//add a new version to the file
VFSLeaf sourceLeaf = (VFSLeaf)vfsSource;
vfsRepositoryService.addVersion(sourceLeaf, ureq.getIdentity(), false, "", sourceLeaf.getInputStream());
} else {
vfsStatus = target.copyFrom(vfsSource, ureq.getIdentity());
}
if (vfsStatus != VFSConstants.SUCCESS) {
String errorKey = "failed";
if (vfsStatus == VFSConstants.ERROR_QUOTA_EXCEEDED)
errorKey = "QuotaExceeded";
abortFailed(ureq, errorKey);
return;
}
if (move) {
// if move, delete the source. Note that meta source
// has already been delete (i.e. moved)
vfsSource.delete();
}
}
// after a copy or a move, notify the subscribers
VFSSecurityCallback secCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
if (secCallback != null) {
SubscriptionContext subsContext = secCallback.getSubscriptionContext();
if (subsContext != null) {
notificationsManager.markPublisherNews(subsContext, ureq.getIdentity(), true);
}
}
fireEvent(ureq, new FolderEvent(move ? FolderEvent.MOVE_EVENT : FolderEvent.COPY_EVENT, fileSelection.renderAsHtml()));
notifyFinished(ureq);
}
private void notifyFinished(UserRequest ureq) {
VFSContainer container = VFSManager.findInheritingSecurityCallbackContainer(folderComponent.getRootContainer());
VFSSecurityCallback secCallback = container.getLocalSecurityCallback();
if(secCallback != null) {
SubscriptionContext subsContext = secCallback.getSubscriptionContext();
if (subsContext != null) {
notificationsManager.markPublisherNews(subsContext, ureq.getIdentity(), true);
}
}
fireEvent(ureq, FOLDERCOMMAND_FINISHED);
}
/**
* Get the list of source files. Sanity check if resolveable, overlapping or
* a target with the same name already exists. In such cases, set the error message, fire
* the abort event and return null.
*
* @param target
* @param ureq
* @return
*/
private List<VFSItem> getSanityCheckedSourceItems(VFSContainer target, UserRequest ureq) {
// collect all source files first
List<VFSItem> sources = new ArrayList<>();
for (String sourceRelPath:fileSelection.getFiles()) {
VFSItem vfsSource = folderComponent.getCurrentContainer().resolve(sourceRelPath);
if (vfsSource == null) {
abortFailed(ureq, "FileDoesNotExist");
return null;
}
if (vfsSource instanceof VFSContainer) {
// if a folder... check if they are overlapping
if (VFSManager.isContainerDescendantOrSelf(target, (VFSContainer)vfsSource)) {
abortFailed(ureq, "OverlappingTarget");
return null;
}
}
if (vfsLockManager.isLockedForMe(vfsSource, ureq.getIdentity(), VFSLockApplicationType.vfs, null)) {
abortFailed(ureq, "lock.title");
return null;
}
// check for existence... this will also prevent to copy item over itself
VFSItem item = target.resolve(vfsSource.getName());
if (item != null) {
abortFailed(ureq, "TargetNameAlreadyUsed");
return null;
}
if (vfsSource.canCopy() != VFSConstants.YES) {
getWindowControl().setError(translator.translate("FileMoveCopyFailed", new String[] {vfsSource.getName()}));
status = FolderCommandStatus.STATUS_FAILED;
fireEvent(ureq, FOLDERCOMMAND_FINISHED);
return null;
}
sources.add(vfsSource);
}
return sources;
}
private void abortFailed(UserRequest ureq, String errorMessageKey) {
getWindowControl().setError(translator.translate(errorMessageKey));
status = FolderCommandStatus.STATUS_FAILED;
fireEvent(ureq, FOLDERCOMMAND_FINISHED);
}
@Override
protected void doDispose() {
//
}
private static final class EditableFilter implements VFSItemFilter {
@Override
public boolean accept(VFSItem vfsItem) {
VFSSecurityCallback secCallback = vfsItem.getLocalSecurityCallback();
if(secCallback != null && !secCallback.canWrite()) {
return false;
}
return true;
}
}
} |