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