|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\User\ForgotPassword\PasswordRequest; |
|
6
|
|
|
|
|
7
|
|
|
use Nette; |
|
8
|
|
|
use Doctrine; |
|
9
|
|
|
use SixtyEightPublishers; |
|
10
|
|
|
|
|
11
|
|
|
class PasswordRequestManager implements IPasswordRequestManager |
|
12
|
|
|
{ |
|
13
|
|
|
use Nette\SmartObject; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \Doctrine\ORM\EntityManagerInterface */ |
|
16
|
|
|
private $em; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy */ |
|
19
|
|
|
private $passwordHashStrategy; |
|
20
|
|
|
|
|
21
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory */ |
|
22
|
|
|
private $findPasswordRequestByIdsQueryFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param \Doctrine\ORM\EntityManagerInterface $em |
|
26
|
|
|
* @param \SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy $passwordHashStrategy |
|
27
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory $findPasswordRequestByIdsQueryFactory |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
Doctrine\ORM\EntityManagerInterface $em, |
|
31
|
|
|
SixtyEightPublishers\User\Common\PasswordHashStrategy\IPasswordHashStrategy $passwordHashStrategy, |
|
32
|
|
|
SixtyEightPublishers\User\ForgotPassword\Query\IFindPasswordRequestByIdsQueryFactory $findPasswordRequestByIdsQueryFactory |
|
33
|
|
|
) { |
|
34
|
|
|
$this->em = $em; |
|
35
|
|
|
$this->passwordHashStrategy = $passwordHashStrategy; |
|
36
|
|
|
$this->findPasswordRequestByIdsQueryFactory = $findPasswordRequestByIdsQueryFactory; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request |
|
41
|
|
|
* @param callable $try |
|
42
|
|
|
* |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
* @throws \SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException |
|
45
|
|
|
*/ |
|
46
|
|
|
private function tryCatch(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request, callable $try) |
|
47
|
|
|
{ |
|
48
|
|
|
try { |
|
49
|
|
|
return $try($request); |
|
50
|
|
|
} catch (\Throwable $e) { |
|
51
|
|
|
if (!$e instanceof SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException) { |
|
52
|
|
|
$e = new SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException($request->getUser()->getId(), $request->getId(), $e->getMessage(), 0, $e); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
throw $e; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/*********** interface \SixtyEightPublishers\User\ForgotPassword\IPasswordRequestSender ***********/ |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
|
|
public function findRequest($uid, $rid): SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest |
|
65
|
|
|
{ |
|
66
|
|
|
try { |
|
67
|
|
|
/** @var \SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $request */ |
|
68
|
|
|
$request = $this->findPasswordRequestByIdsQueryFactory |
|
69
|
|
|
->create($this->em, $uid, $rid) |
|
70
|
|
|
->getOneOrNullResult(); |
|
71
|
|
|
} catch (Doctrine\DBAL\DBALException $e) { |
|
72
|
|
|
throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::missingRequest($uid, $rid); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (NULL === $request) { |
|
76
|
|
|
throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::missingRequest($uid, $rid); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (TRUE === $request->isExpired()) { |
|
80
|
|
|
throw SixtyEightPublishers\User\ForgotPassword\Exception\PasswordRequestProcessException::expiredRequest($request->getUser()->getId(), $request->getId()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $request; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritdoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function reset(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest, string $password): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->tryCatch($passwordRequest, function (SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest) use ($password) { |
|
92
|
|
|
$user = $passwordRequest->getUser(); |
|
93
|
|
|
|
|
94
|
|
|
if ($this->passwordHashStrategy->needRehash($password)) { |
|
95
|
|
|
$password = $this->passwordHashStrategy->hash($password); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$passwordRequest->getResetDeviceInfo()->fill(); |
|
99
|
|
|
$passwordRequest->setStatus($passwordRequest::STATUS_COMPLETED); |
|
100
|
|
|
$user->setPassword($password); |
|
101
|
|
|
|
|
102
|
|
|
$this->em->persist($passwordRequest); |
|
103
|
|
|
$this->em->persist($user); |
|
104
|
|
|
$this->em->flush(); |
|
105
|
|
|
}); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function cancel(SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest): void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->tryCatch($passwordRequest, function (SixtyEightPublishers\User\ForgotPassword\DoctrineEntity\IPasswordRequest $passwordRequest) { |
|
114
|
|
|
$passwordRequest->getResetDeviceInfo()->fill(); |
|
115
|
|
|
$passwordRequest->setStatus($passwordRequest::STATUS_CANCELED); |
|
116
|
|
|
|
|
117
|
|
|
$this->em->persist($passwordRequest); |
|
118
|
|
|
$this->em->flush(); |
|
119
|
|
|
}); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope