Fixes the 500 on admin user deletion: deleting a user with an email_log row (i.e. basically anyone who received any email) hit an unhandled ForeignKeyConstraintViolationException, since email_log, reset_password_request and player all have non-nullable FKs to user with no cascade at the DB level. Instead of cascading the delete (which would be fine for email_log and reset_password_request but risky for player - removing a player row could corrupt other real players' session state), admin delete now sets a deletedAt timestamp instead of removing the row: - UserChecker blocks login for deleted users (checkPreAuth). - EmailLoggerListener rejects the message before send for deleted recipients (checked at actual send time, not at queue time). - Added a last_login_at column + a LoginSuccessEvent listener to populate it, and surfaced both last login and status in the admin users list. Added `app:users:purge-deleted`, a command intended to run on a schedule that permanently removes users who were soft-deleted more than 3 months ago and never played a game (join to Player via a NOT EXISTS subquery). For that eventual hard-delete to actually succeed, email_log and reset_password_request now cascade-delete at the DB level (migration drops+recreates both FK constraints with ON DELETE CASCADE) - player intentionally still isn't cascaded, so a user with game history can never be purged this way even by mistake.
64 lines
2.0 KiB
PHP
64 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Tech\Repository;
|
|
|
|
use App\Tech\Entity\User;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
|
|
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
|
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<User>
|
|
*/
|
|
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, User::class);
|
|
}
|
|
|
|
/**
|
|
* Used to upgrade (rehash) the user's password automatically over time.
|
|
*/
|
|
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
|
|
{
|
|
if (!$user instanceof User) {
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
|
|
}
|
|
|
|
$user->setPassword($newHashedPassword);
|
|
$this->getEntityManager()->persist($user);
|
|
$this->getEntityManager()->flush();
|
|
}
|
|
|
|
/**
|
|
* @return User[]
|
|
*/
|
|
public function findByRole(string $role): array
|
|
{
|
|
return $this->createQueryBuilder('u')
|
|
->andWhere('u.roles LIKE :role')
|
|
->setParameter('role', '%"' . $role . '"%')
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
/**
|
|
* Soft-deleted users, deleted before the given date, who never played a game.
|
|
*
|
|
* @return User[]
|
|
*/
|
|
public function findPurgeableDeletedUsers(\DateTimeImmutable $deletedBefore): array
|
|
{
|
|
return $this->createQueryBuilder('u')
|
|
->andWhere('u.deletedAt IS NOT NULL')
|
|
->andWhere('u.deletedAt <= :deletedBefore')
|
|
->andWhere('NOT EXISTS (SELECT 1 FROM App\Game\Entity\Player p WHERE p.user = u)')
|
|
->setParameter('deletedBefore', $deletedBefore)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
}
|