Skip to content

Commit 0311de2

Browse files
committed
Merge branch 'master' of github.com:chamilo/chamilo-lms
2 parents d385c00 + 42cb133 commit 0311de2

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

Diff for: src/CoreBundle/Repository/Node/UserRepository.php

+50-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
3535
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
3636
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
37+
use Symfony\Contracts\Translation\TranslatorInterface;
3738

3839
use const MB_CASE_LOWER;
3940

@@ -48,7 +49,8 @@ class UserRepository extends ResourceRepository implements PasswordUpgraderInter
4849

4950
public function __construct(
5051
ManagerRegistry $registry,
51-
private readonly IllustrationRepository $illustrationRepository
52+
private readonly IllustrationRepository $illustrationRepository,
53+
private readonly TranslatorInterface $translator
5254
) {
5355
parent::__construct($registry, User::class);
5456
}
@@ -149,6 +151,9 @@ public function deleteUser(User $user, bool $destroy = false): void
149151

150152
try {
151153
if ($destroy) {
154+
// Call method to delete messages and attachments
155+
$this->deleteUserMessagesAndAttachments($user);
156+
152157
$fallbackUser = $this->getFallbackUser();
153158

154159
if ($fallbackUser) {
@@ -233,8 +238,12 @@ protected function reassignUserResourcesToFallbackSQL(User $userToDelete, User $
233238
}
234239

235240
/**
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.
238247
*/
239248
protected function getRelations(): array
240249
{
@@ -335,6 +344,44 @@ protected function getRelations(): array
335344
];
336345
}
337346

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+
338385
public function getFallbackUser(): ?User
339386
{
340387
return $this->findOneBy(['status' => User::ROLE_FALLBACK], ['id' => 'ASC']);

0 commit comments

Comments
 (0)