Datasets:

Modalities:
Text
Formats:
json
Languages:
code
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
File size: 6,129 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
/**
* 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.Collections;
import java.util.List;

import org.olat.core.CoreSpringFactory;
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.vfs.VFSVersionModule;
import org.olat.core.gui.UserRequest;
import org.olat.core.gui.components.Component;
import org.olat.core.gui.control.Controller;
import org.olat.core.gui.control.Event;
import org.olat.core.gui.control.WindowControl;
import org.olat.core.gui.control.controller.BasicController;
import org.olat.core.gui.control.generic.modal.DialogBoxController;
import org.olat.core.gui.control.generic.modal.DialogBoxUIFactory;
import org.olat.core.gui.translator.Translator;
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.VFSLockApplicationType;
import org.olat.core.util.vfs.VFSLockManager;

public class CmdDelete extends BasicController implements FolderCommand {

	private static int status = FolderCommandStatus.STATUS_SUCCESS;

	private Translator translator;
	private FolderComponent folderComponent;
	private FileSelection fileSelection;

	private DialogBoxController dialogCtr;
	private DialogBoxController lockedFiledCtr;
	
	private final boolean versionsEnabled;
	private final VFSLockManager lockManager;
	
	protected CmdDelete(UserRequest ureq, WindowControl wControl) {
		super(ureq, wControl);
		versionsEnabled = CoreSpringFactory.getImpl(VFSVersionModule.class).isEnabled();
		lockManager = CoreSpringFactory.getImpl(VFSLockManager.class);
	}

	@Override
	public Controller execute(FolderComponent fc, UserRequest ureq, WindowControl wContr, Translator trans) {
		this.translator = trans;
		this.folderComponent = fc;
		// 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());

		VFSContainer currentContainer = folderComponent.getCurrentContainer();
		List<String> lockedFiles = hasLockedFiles(currentContainer, fileSelection);
		if (lockedFiles.isEmpty()) {
			String msg = trans.translate("del.confirm") + "<p>" + fileSelection.renderAsHtml() + "</p>";		
			// create dialog controller
			dialogCtr = activateYesNoDialog(ureq, trans.translate("del.header"), msg, dialogCtr);
		} else {
			String msg = FolderCommandHelper.renderLockedMessageAsHtml(trans, lockedFiles);
			List<String> buttonLabels = Collections.singletonList(trans.translate("ok"));
			lockedFiledCtr = activateGenericDialog(ureq, trans.translate("lock.title"), msg, buttonLabels, lockedFiledCtr);
		}
		return this;
	}
	
	public List<String> hasLockedFiles(VFSContainer container, FileSelection selection) {
		List<String> lockedFiles = new ArrayList<>();
		for (String file : selection.getFiles()) {
			VFSItem item = container.resolve(file);
			if (lockManager.isLockedForMe(item, getIdentity(), VFSLockApplicationType.vfs, null)) {
				lockedFiles.add(file);
			}
		}
		return lockedFiles;
	}

	@Override
	public int getStatus() {
		return status;
	}

	@Override
	public boolean runsModal() {
		// this controller has its own modal dialog box
		return true;
	}
	
	@Override
	public String getModalTitle() {
		return null;
	}

	public FileSelection getFileSelection() {
		return fileSelection;
	}

	@Override
	public void event(UserRequest ureq, Controller source, Event event) {
		if (source == dialogCtr) {
			if (DialogBoxUIFactory.isYesEvent(event)) {				
				// do delete
				VFSContainer currentContainer = folderComponent.getCurrentContainer();
				List<String> files = fileSelection.getFiles();
				if (files.isEmpty()) {
					// sometimes, browser sends empty form data...
					getWindowControl().setError(translator.translate("failed"));
					status = FolderCommandStatus.STATUS_FAILED;
					fireEvent(ureq, FOLDERCOMMAND_FINISHED);
				}
				for (String file : files) {
					VFSItem item = currentContainer.resolve(file);
					if (item != null && (item.canDelete() == VFSConstants.YES)) {
						if (versionsEnabled && item.canVersion() == VFSConstants.YES) {
							// Move to pub
							item.delete();
						} else {
							item.deleteSilently();
						}
					} else {
						getWindowControl().setWarning(translator.translate("del.partial"));
					}
				}
				
				String confirmationText = fileSelection.renderAsHtml();
				fireEvent(ureq, new FolderEvent(FolderEvent.DELETE_EVENT, confirmationText));
				fireEvent(ureq, FOLDERCOMMAND_FINISHED);
			} else {
				// abort
				status = FolderCommandStatus.STATUS_CANCELED;
				fireEvent(ureq, FOLDERCOMMAND_FINISHED);
			}
		}

	}
	
	@Override
	protected void event(UserRequest ureq, Component source, Event event) {
		// no events to catch
	}

	@Override
	protected void doDispose() {
		// autodisposed by basic controller
	}
}