1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gvera\Services; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\OptimisticLockException; |
6
|
|
|
use Doctrine\ORM\ORMException; |
7
|
|
|
use Gvera\Events\ForgotPasswordCreatedEvent; |
8
|
|
|
use Gvera\Helpers\entities\GvEntityManager; |
9
|
|
|
use Gvera\Helpers\events\EventDispatcher; |
10
|
|
|
use Gvera\Models\ForgotPassword; |
11
|
|
|
use Gvera\Models\User; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @Inject session |
15
|
|
|
*/ |
16
|
|
|
class ForgotPasswordService |
17
|
|
|
{ |
18
|
|
|
private $repository; |
19
|
|
|
private GvEntityManager $entityManager; |
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct(GvEntityManager $entityManager) |
22
|
|
|
{ |
23
|
|
|
$this->entityManager = $entityManager; |
24
|
|
|
$this->repository = $this->entityManager->getRepository(ForgotPassword::class); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param User $user |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function validateNewForgotPassword(User $user): bool |
32
|
|
|
{ |
33
|
|
|
$activeForgotPass = $this->repository->findOneBy(['user' => $user, 'alreadyUsed' => false]); |
34
|
|
|
|
35
|
|
|
return !isset($activeForgotPass); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param User $user |
40
|
|
|
* @throws OptimisticLockException |
41
|
|
|
* @throws ORMException |
42
|
|
|
*/ |
43
|
|
|
public function generateNewForgotPassword(User $user) |
44
|
|
|
{ |
45
|
|
|
$dateTime = new \DateTime(); |
46
|
|
|
$newKey = md5($dateTime->format('Y-m-d H:i:s') . $user->getId()); |
47
|
|
|
$forgotPassword = new ForgotPassword($user, $newKey); |
48
|
|
|
|
49
|
|
|
$this->entityManager->merge($forgotPassword); |
50
|
|
|
$this->entityManager->flush(); |
51
|
|
|
|
52
|
|
|
EventDispatcher::dispatchEvent( |
53
|
|
|
ForgotPasswordCreatedEvent::FORGOT_PASSWORD_EVENT, |
54
|
|
|
new ForgotPasswordCreatedEvent($user->getEmail()) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $key |
60
|
|
|
* @throws OptimisticLockException |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
public function useForgotPassword($key) |
64
|
|
|
{ |
65
|
|
|
$forgotPassword = $this->repository->findOneBy(['forgotPasswordKey' => $key]); |
66
|
|
|
if (!isset($forgotPassword)) { |
67
|
|
|
throw new \Exception('Forgot Password was never generated'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($forgotPassword->getAlreadyUsed()) { |
71
|
|
|
throw new \Exception('Forgot Password was already used'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$forgotPassword->setAlreadyUsed(true); |
75
|
|
|
$this->entityManager->persist($forgotPassword); |
76
|
|
|
$this->entityManager->flush(); |
77
|
|
|
$this->session->set('forgot_password', $key); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param $key |
82
|
|
|
* @param $newPassword |
83
|
|
|
* @throws OptimisticLockException |
84
|
|
|
*/ |
85
|
|
|
public function regeneratePassword($key, $newPassword) |
86
|
|
|
{ |
87
|
|
|
$forgotPassword = $this->repository->findOneBy(['forgotPasswordKey' => $key]); |
88
|
|
|
$user = $forgotPassword->getUser(); |
89
|
|
|
$user->setPassword($newPassword); |
90
|
|
|
|
91
|
|
|
$this->entityManager->persist($user); |
92
|
|
|
$this->entityManager->flush(); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|