34
34
use Symfony \Component \Security \Core \Exception \UserNotFoundException ;
35
35
use Symfony \Component \Security \Core \User \PasswordAuthenticatedUserInterface ;
36
36
use Symfony \Component \Security \Core \User \PasswordUpgraderInterface ;
37
+ use Symfony \Contracts \Translation \TranslatorInterface ;
37
38
38
39
use const MB_CASE_LOWER ;
39
40
@@ -48,7 +49,8 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter
48
49
49
50
public function __construct (
50
51
ManagerRegistry $ registry ,
51
- private readonly IllustrationRepository $ illustrationRepository
52
+ private readonly IllustrationRepository $ illustrationRepository ,
53
+ private readonly TranslatorInterface $ translator
52
54
) {
53
55
parent ::__construct ($ registry , User::class);
54
56
}
@@ -149,6 +151,9 @@ public function deleteUser(User $user, bool $destroy = false): void
149
151
150
152
try {
151
153
if ($ destroy ) {
154
+ // Call method to delete messages and attachments
155
+ $ this ->deleteUserMessagesAndAttachments ($ user );
156
+
152
157
$ fallbackUser = $ this ->getFallbackUser ();
153
158
154
159
if ($ fallbackUser ) {
@@ -233,8 +238,12 @@ protected function reassignUserResourcesToFallbackSQL(User $userToDelete, User $
233
238
}
234
239
235
240
/**
236
- * Retrieves a list of database table relations and their corresponding actions
237
- * to handle user resource reassignment or deletion.
241
+ * Provides a list of database table relations and their respective actions
242
+ * (update or delete) for handling user resource reassignment or deletion.
243
+ *
244
+ * Any new database table that stores references to users and requires updates
245
+ * or deletions when a user is removed should be added to this list. This ensures
246
+ * proper handling of dependencies and avoids orphaned data.
238
247
*/
239
248
protected function getRelations (): array
240
249
{
@@ -335,6 +344,44 @@ protected function getRelations(): array
335
344
];
336
345
}
337
346
347
+ /**
348
+ * Deletes a user's messages and their attachments, updates the message content,
349
+ * and detaches the user as the sender.
350
+ */
351
+ public function deleteUserMessagesAndAttachments (User $ user ): void
352
+ {
353
+ $ em = $ this ->getEntityManager ();
354
+ $ connection = $ em ->getConnection ();
355
+
356
+ $ currentDate = (new \DateTime ())->format ('Y-m-d H:i:s ' );
357
+ $ updatedContent = sprintf (
358
+ $ this ->translator ->trans ('This message was deleted when the user was removed from the platform on %s ' ),
359
+ $ currentDate
360
+ );
361
+
362
+ $ connection ->executeStatement (
363
+ 'UPDATE message m
364
+ SET m.content = :content, m.user_sender_id = NULL
365
+ WHERE m.user_sender_id = :userId ' ,
366
+ [
367
+ 'content ' => $ updatedContent ,
368
+ 'userId ' => $ user ->getId (),
369
+ ]
370
+ );
371
+
372
+ $ connection ->executeStatement (
373
+ 'DELETE ma
374
+ FROM message_attachment ma
375
+ INNER JOIN message m ON ma.message_id = m.id
376
+ WHERE m.user_sender_id IS NULL ' ,
377
+ [
378
+ 'userId ' => $ user ->getId (),
379
+ ]
380
+ );
381
+
382
+ $ em ->clear ();
383
+ }
384
+
338
385
public function getFallbackUser (): ?User
339
386
{
340
387
return $ this ->findOneBy (['status ' => User::ROLE_FALLBACK ], ['id ' => 'ASC ' ]);
0 commit comments