Skip to content

Commit ac8b747

Browse files
committed
fix(trashbin): deletedBy of a file from a federated folder
Signed-off-by: Maxence Lange <[email protected]>
1 parent c19d37f commit ac8b747

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

apps/files_trashbin/lib/Trashbin.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@
3939
use OCP\FilesMetadata\IFilesMetadataManager;
4040
use OCP\IConfig;
4141
use OCP\IDBConnection;
42+
use OCP\IRequest;
4243
use OCP\Lock\ILockingProvider;
4344
use OCP\Lock\LockedException;
4445
use OCP\Server;
46+
use OCP\Share\Exceptions\ShareNotFound;
4547
use OCP\Util;
48+
use Psr\Container\ContainerExceptionInterface;
49+
use Psr\Container\NotFoundExceptionInterface;
4650
use Psr\Log\LoggerInterface;
4751

4852
/** @template-implements IEventListener<BeforeNodeDeletedEvent> */
@@ -323,13 +327,16 @@ public static function move2trash($file_path, $ownerOnly = false) {
323327
}
324328

325329
if ($moveSuccessful) {
330+
// there is still a possibility that the file has been deleted by a remote user
331+
$deletedBy = self::overwriteDeletedBy($user);
332+
326333
$query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
327334
$query->insert('files_trash')
328335
->setValue('id', $query->createNamedParameter($filename))
329336
->setValue('timestamp', $query->createNamedParameter($timestamp))
330337
->setValue('location', $query->createNamedParameter($location))
331338
->setValue('user', $query->createNamedParameter($owner))
332-
->setValue('deleted_by', $query->createNamedParameter($user));
339+
->setValue('deleted_by', $query->createNamedParameter($deletedBy));
333340
$result = $query->executeStatement();
334341
if (!$result) {
335342
\OC::$server->get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']);
@@ -1188,6 +1195,29 @@ private static function getNodeForPath(string $path): Node {
11881195
}
11891196
}
11901197

1198+
/**
1199+
* in case the request is authed, and user token is from a federated share
1200+
* we use shared_with as initiator of the deletion
1201+
*/
1202+
private static function overwriteDeletedBy(string $user) {
1203+
try {
1204+
$request = Server::get(IRequest::class);
1205+
/** @psalm-suppress NoInterfaceProperties */
1206+
$token = $request->server['PHP_AUTH_USER'] ?? '';
1207+
if ($token === '') {
1208+
return $user;
1209+
}
1210+
1211+
$federatedShareProvider = Server::get(\OCA\FederatedFileSharing\FederatedShareProvider::class);
1212+
$share = $federatedShareProvider->getShareByToken($token);
1213+
1214+
return $share->getSharedWith();
1215+
} catch (NotFoundExceptionInterface|ContainerExceptionInterface|ShareNotFound) {
1216+
}
1217+
1218+
return $user;
1219+
}
1220+
11911221
public function handle(Event $event): void {
11921222
if ($event instanceof BeforeNodeDeletedEvent) {
11931223
self::ensureFileScannedHook($event->getNode());

0 commit comments

Comments
 (0)