1
|
|
|
<?php |
2
|
|
|
namespace Gvera\Controllers; |
3
|
|
|
|
4
|
|
|
use Gvera\Helpers\http\Response; |
5
|
|
|
use Gvera\Models\User; |
6
|
|
|
use ReflectionException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class ForgotPassword |
10
|
|
|
* @package Gvera\Controllers |
11
|
|
|
*/ |
12
|
|
|
class ForgotPassword extends GvController |
13
|
|
|
{ |
14
|
|
|
public function index() |
15
|
|
|
{ |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @throws \Doctrine\ORM\OptimisticLockException |
20
|
|
|
* @throws ReflectionException |
21
|
|
|
*/ |
22
|
|
|
public function new() |
23
|
|
|
{ |
24
|
|
|
if (!$this->httpRequest->isPost()) { |
25
|
|
|
$this->redirectToIndex(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$forgotPassService = $this->getForgotPasswordService(); |
|
|
|
|
29
|
|
|
$entityManager = $this->getEntityManager(); |
|
|
|
|
30
|
|
|
|
31
|
|
|
$email = $this->httpRequest->getParameter('email'); |
32
|
|
|
|
33
|
|
|
$userRepository = $entityManager->getRepository(User::class); |
34
|
|
|
$user = $userRepository->findOneBy(['email' => $email]); |
35
|
|
|
|
36
|
|
|
if (null === $user) { |
37
|
|
|
$this->httpResponse->response(new Response('something went wrong')); |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!$forgotPassService->validateNewForgotPassword($user)) { |
42
|
|
|
$this->redirectToIndex(); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$newKey = $forgotPassService->generateNewForgotPassword($user); |
47
|
|
|
$this->httpResponse->response(new Response('Key generated successfully: ' . $newKey)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws ReflectionException |
52
|
|
|
*/ |
53
|
|
|
public function use() |
54
|
|
|
{ |
55
|
|
|
$forgotPasswordService = $this->diContainer->get("forgotPasswordService"); |
56
|
|
|
try { |
57
|
|
|
$forgotPasswordService->useForgotPassword($this->httpRequest->getParameter('key')); |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
$this->redirectToIndex(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @throws ReflectionException |
65
|
|
|
*/ |
66
|
|
|
public function regenerate() |
67
|
|
|
{ |
68
|
|
|
$session = $this->getSession(); |
|
|
|
|
69
|
|
|
if (!($session->get('forgot_password')) || !$this->httpRequest->isPost()) { |
70
|
|
|
$session->destroy(); |
71
|
|
|
$this->redirectToIndex(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$forgotPasswordService = $this->diContainer->get("forgotPasswordService"); |
75
|
|
|
$forgotPasswordService->regeneratePassword( |
76
|
|
|
$session->get('forgot_password'), |
77
|
|
|
$this->httpRequest->getParameter('new_password') |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function redirectToIndex() |
82
|
|
|
{ |
83
|
|
|
$this->httpResponse->redirect('/forgotpassword'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|