UserRepository.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\User;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  7. use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
  8. use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  9. /**
  10. * @extends ServiceEntityRepository<User>
  11. */
  12. class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
  13. {
  14. public function __construct(ManagerRegistry $registry)
  15. {
  16. parent::__construct($registry, User::class);
  17. }
  18. /**
  19. * Used to upgrade (rehash) the user's password automatically over time.
  20. */
  21. public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
  22. {
  23. if (!$user instanceof User) {
  24. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
  25. }
  26. $user->setPassword($newHashedPassword);
  27. $this->getEntityManager()->persist($user);
  28. $this->getEntityManager()->flush();
  29. }
  30. /**
  31. * Who's online
  32. * = dernière action réalisée il y a moins de 5 minutes
  33. */
  34. public function findWhoIsOnline(): array
  35. {
  36. $qb = $this->createQueryBuilder('u')
  37. ->where('u.lastAction > :lastAction')
  38. ->setParameter('lastAction', new \DateTime('-5 minutes'))
  39. ->getQuery();
  40. return $qb->getResult();
  41. }
  42. // /**
  43. // * @return User[] Returns an array of User objects
  44. // */
  45. // public function findByExampleField($value): array
  46. // {
  47. // return $this->createQueryBuilder('u')
  48. // ->andWhere('u.exampleField = :val')
  49. // ->setParameter('val', $value)
  50. // ->orderBy('u.id', 'ASC')
  51. // ->setMaxResults(10)
  52. // ->getQuery()
  53. // ->getResult()
  54. // ;
  55. // }
  56. // public function findOneBySomeField($value): ?User
  57. // {
  58. // return $this->createQueryBuilder('u')
  59. // ->andWhere('u.exampleField = :val')
  60. // ->setParameter('val', $value)
  61. // ->getQuery()
  62. // ->getOneOrNullResult()
  63. // ;
  64. // }
  65. }