|
39 | 39 | use OCP\FilesMetadata\IFilesMetadataManager; |
40 | 40 | use OCP\IConfig; |
41 | 41 | use OCP\IDBConnection; |
| 42 | +use OCP\IRequest; |
42 | 43 | use OCP\Lock\ILockingProvider; |
43 | 44 | use OCP\Lock\LockedException; |
44 | 45 | use OCP\Server; |
| 46 | +use OCP\Share\Exceptions\ShareNotFound; |
45 | 47 | use OCP\Util; |
| 48 | +use Psr\Container\ContainerExceptionInterface; |
| 49 | +use Psr\Container\NotFoundExceptionInterface; |
46 | 50 | use Psr\Log\LoggerInterface; |
47 | 51 |
|
48 | 52 | /** @template-implements IEventListener<BeforeNodeDeletedEvent> */ |
@@ -323,13 +327,16 @@ public static function move2trash($file_path, $ownerOnly = false) { |
323 | 327 | } |
324 | 328 |
|
325 | 329 | if ($moveSuccessful) { |
| 330 | + // there is still a possibility that the file has been deleted by a remote user |
| 331 | + $deletedBy = self::overwriteDeletedBy($user); |
| 332 | + |
326 | 333 | $query = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
327 | 334 | $query->insert('files_trash') |
328 | 335 | ->setValue('id', $query->createNamedParameter($filename)) |
329 | 336 | ->setValue('timestamp', $query->createNamedParameter($timestamp)) |
330 | 337 | ->setValue('location', $query->createNamedParameter($location)) |
331 | 338 | ->setValue('user', $query->createNamedParameter($owner)) |
332 | | - ->setValue('deleted_by', $query->createNamedParameter($user)); |
| 339 | + ->setValue('deleted_by', $query->createNamedParameter($deletedBy)); |
333 | 340 | $result = $query->executeStatement(); |
334 | 341 | if (!$result) { |
335 | 342 | \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 { |
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