Skip to content

Commit 27a1f89

Browse files
committed
fix(trashbin): deletedBy of a file from a federated folder
1 parent fac380f commit 27a1f89

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
@@ -40,12 +40,17 @@
4040
use OCP\FilesMetadata\IFilesMetadataManager;
4141
use OCP\IConfig;
4242
use OCP\IDBConnection;
43+
use OCP\IRequest;
4344
use OCP\IURLGenerator;
4445
use OCP\IUserManager;
4546
use OCP\Lock\ILockingProvider;
4647
use OCP\Lock\LockedException;
4748
use OCP\Server;
49+
use OCP\Share\Exceptions\ShareNotFound;
50+
use OCP\Share\IShare;
4851
use OCP\Util;
52+
use Psr\Container\ContainerExceptionInterface;
53+
use Psr\Container\NotFoundExceptionInterface;
4954
use Psr\Log\LoggerInterface;
5055

5156
/** @template-implements IEventListener<BeforeNodeDeletedEvent> */
@@ -326,13 +331,16 @@ public static function move2trash($file_path, $ownerOnly = false) {
326331
}
327332

328333
if ($moveSuccessful) {
334+
// there is still a possibility that the file has been deleted by a remote user
335+
$deletedBy = self::overwriteDeletedBy($user);
336+
329337
$query = Server::get(IDBConnection::class)->getQueryBuilder();
330338
$query->insert('files_trash')
331339
->setValue('id', $query->createNamedParameter($filename))
332340
->setValue('timestamp', $query->createNamedParameter($timestamp))
333341
->setValue('location', $query->createNamedParameter($location))
334342
->setValue('user', $query->createNamedParameter($owner))
335-
->setValue('deleted_by', $query->createNamedParameter($user));
343+
->setValue('deleted_by', $query->createNamedParameter($deletedBy));
336344
$result = $query->executeStatement();
337345
if (!$result) {
338346
Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']);
@@ -1182,6 +1190,28 @@ private static function getNodeForPath(string $user, string $path, string $baseD
11821190
}
11831191
}
11841192

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

0 commit comments

Comments
 (0)