-
Notifications
You must be signed in to change notification settings - Fork 67
/
ResetPasswordRequestRepositoryTrait.php
105 lines (91 loc) · 3.3 KB
/
ResetPasswordRequestRepositoryTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/*
* This file is part of the SymfonyCasts ResetPasswordBundle package.
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SymfonyCasts\Bundle\ResetPassword\Persistence\Repository;
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
/**
* Trait can be added to a Doctrine ORM repository to help implement
* ResetPasswordRequestRepositoryInterface.
*
* @author Jesse Rushlow <jr@rushlow.dev>
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
trait ResetPasswordRequestRepositoryTrait
{
public function getUserIdentifier(object $user): string
{
return (string) $this->getEntityManager()
->getUnitOfWork()
->getSingleIdentifierValue($user)
;
}
public function persistResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
{
$this->getEntityManager()->persist($resetPasswordRequest);
$this->getEntityManager()->flush();
}
public function findResetPasswordRequest(string $selector): ?ResetPasswordRequestInterface
{
return $this->findOneBy(['selector' => $selector]);
}
public function getMostRecentNonExpiredRequestDate(object $user): ?\DateTimeInterface
{
// Normally there is only 1 max request per use, but written to be flexible
/** @var ResetPasswordRequestInterface $resetPasswordRequest */
$resetPasswordRequest = $this->createQueryBuilder('t')
->where('t.user = :user')
->setParameter('user', $user)
->orderBy('t.requestedAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
if (null !== $resetPasswordRequest && !$resetPasswordRequest->isExpired()) {
return $resetPasswordRequest->getRequestedAt();
}
return null;
}
public function removeResetPasswordRequest(ResetPasswordRequestInterface $resetPasswordRequest): void
{
$this->createQueryBuilder('t')
->delete()
->where('t.user = :user')
->setParameter('user', $resetPasswordRequest->getUser())
->getQuery()
->execute()
;
}
public function removeExpiredResetPasswordRequests(): int
{
$time = new \DateTimeImmutable('-1 week');
$query = $this->createQueryBuilder('t')
->delete()
->where('t.expiresAt <= :time')
->setParameter('time', $time)
->getQuery()
;
return $query->execute();
}
/**
* Remove a users ResetPasswordRequest objects from persistence.
*
* Warning - This is a destructive operation. Calling this method
* may have undesired consequences for users who have valid
* ResetPasswordRequests but have not "checked their email" yet.
*
* @see https://github.com/SymfonyCasts/reset-password-bundle?tab=readme-ov-file#advanced-usage
*/
public function removeRequests(object $user): void
{
$query = $this->createQueryBuilder('t')
->delete()
->where('t.user = :user')
->setParameter('user', $user)
;
$query->getQuery()->execute();
}
}