|
41 | 41 | use OCP\FilesMetadata\IFilesMetadataManager; |
42 | 42 | use OCP\IConfig; |
43 | 43 | use OCP\IDBConnection; |
| 44 | +use OCP\IRequest; |
44 | 45 | use OCP\IURLGenerator; |
45 | 46 | use OCP\IUserManager; |
46 | 47 | use OCP\Lock\ILockingProvider; |
47 | 48 | use OCP\Lock\LockedException; |
48 | 49 | use OCP\Server; |
| 50 | +use OCP\Share\Exceptions\ShareNotFound; |
49 | 51 | use OCP\Util; |
| 52 | +use Psr\Container\ContainerExceptionInterface; |
| 53 | +use Psr\Container\NotFoundExceptionInterface; |
50 | 54 | use Psr\Log\LoggerInterface; |
51 | 55 |
|
52 | 56 | /** @template-implements IEventListener<BeforeNodeDeletedEvent> */ |
@@ -327,13 +331,16 @@ public static function move2trash($file_path, $ownerOnly = false) { |
327 | 331 | } |
328 | 332 |
|
329 | 333 | if ($moveSuccessful) { |
| 334 | + // there is still a possibility that the file has been deleted by a remote user |
| 335 | + $deletedBy = self::overwriteDeletedBy($user); |
| 336 | + |
330 | 337 | $query = Server::get(IDBConnection::class)->getQueryBuilder(); |
331 | 338 | $query->insert('files_trash') |
332 | 339 | ->setValue('id', $query->createNamedParameter($filename)) |
333 | 340 | ->setValue('timestamp', $query->createNamedParameter($timestamp)) |
334 | 341 | ->setValue('location', $query->createNamedParameter($location)) |
335 | 342 | ->setValue('user', $query->createNamedParameter($owner)) |
336 | | - ->setValue('deleted_by', $query->createNamedParameter($user)); |
| 343 | + ->setValue('deleted_by', $query->createNamedParameter($deletedBy)); |
337 | 344 | $result = $query->executeStatement(); |
338 | 345 | if (!$result) { |
339 | 346 | Server::get(LoggerInterface::class)->error('trash bin database couldn\'t be updated', ['app' => 'files_trashbin']); |
@@ -1188,6 +1195,29 @@ private static function getNodeForPath(string $user, string $path, string $baseD |
1188 | 1195 | } |
1189 | 1196 | } |
1190 | 1197 |
|
| 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 | + |
1191 | 1221 | public function handle(Event $event): void { |
1192 | 1222 | if ($event instanceof BeforeNodeDeletedEvent) { |
1193 | 1223 | self::ensureFileScannedHook($event->getNode()); |
|
0 commit comments