1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\Controller; |
4
|
|
|
|
5
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Command\ResetPasswordHandler; |
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\DataTransferObject\ChangePasswordDataTransferObject; |
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Exception\InvalidPasswordConfirmationException; |
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Form\ChangePasswordType; |
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
10
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\BaseUserRepositoryCollection; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Translation\Translator; |
14
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
19
|
|
|
|
20
|
|
|
class PasswordResetController |
21
|
|
|
{ |
22
|
|
|
/** @var BaseUserRepositoryCollection */ |
23
|
|
|
private $userRepositoryCollection; |
24
|
|
|
|
25
|
|
|
/** @var ResetPasswordHandler */ |
26
|
|
|
private $resetPasswordHandler; |
27
|
|
|
|
28
|
|
|
/** @var Router */ |
29
|
|
|
private $router; |
30
|
|
|
|
31
|
|
|
/** @var FormFactoryInterface */ |
32
|
|
|
private $formFactory; |
33
|
|
|
|
34
|
|
|
/** @var EngineInterface */ |
35
|
|
|
private $templating; |
36
|
|
|
|
37
|
|
|
/** @var Translator */ |
38
|
|
|
private $translator; |
39
|
|
|
|
40
|
|
|
/** @var FlashBagInterface */ |
41
|
|
|
private $flashBag; |
42
|
|
|
|
43
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
44
|
|
|
BaseUserRepositoryCollection $userRepositoryCollection, |
45
|
|
|
ResetPasswordHandler $resetPasswordHandler, |
46
|
|
|
Router $router, |
47
|
|
|
FormFactoryInterface $formFactory, |
48
|
|
|
EngineInterface $templating, |
49
|
|
|
Translator $translator, |
50
|
|
|
FlashBagInterface $flashBag |
51
|
|
|
) { |
52
|
|
|
$this->userRepositoryCollection = $userRepositoryCollection; |
53
|
|
|
$this->resetPasswordHandler = $resetPasswordHandler; |
54
|
|
|
$this->router = $router; |
55
|
|
|
$this->formFactory = $formFactory; |
56
|
|
|
$this->templating = $templating; |
57
|
|
|
$this->translator = $translator; |
58
|
|
|
$this->flashBag = $flashBag; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Request $request |
63
|
|
|
* @param PasswordResetToken $token |
64
|
|
|
* |
65
|
|
|
* @throws InvalidPasswordConfirmationException |
66
|
|
|
* |
67
|
|
|
* @return Response |
68
|
|
|
*/ |
69
|
|
|
public function resetAction(Request $request, PasswordResetToken $token): Response |
70
|
|
|
{ |
71
|
|
|
$user = $this->userRepositoryCollection->findUserByToken($token); |
72
|
|
|
|
73
|
|
|
if ($user === null) { |
74
|
|
|
return new RedirectResponse( |
75
|
|
|
$this->router->generate('multi_user_login') |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$changePasswordTransferObject = ChangePasswordDataTransferObject::forUser($user); |
80
|
|
|
$form = $this->formFactory->create(ChangePasswordType::class, $changePasswordTransferObject); |
81
|
|
|
|
82
|
|
|
$form->handleRequest($request); |
83
|
|
|
|
84
|
|
View Code Duplication |
if ($form->isSubmitted() && $form->isValid()) { |
|
|
|
|
85
|
|
|
$this->resetPasswordHandler->handle($form->getData()); |
86
|
|
|
|
87
|
|
|
$this->flashBag->add( |
88
|
|
|
'success', |
89
|
|
|
$this->translator->trans( |
90
|
|
|
'sumocoders.multiuserbundle.flash.password_reset_success' |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return new RedirectResponse( |
95
|
|
|
$this->router->generate('multi_user_login') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->templating->renderResponse( |
100
|
|
|
'SumoCodersFrameworkMultiUserBundle:PasswordReset:reset.html.twig', |
101
|
|
|
[ |
102
|
|
|
'form' => $form->createView(), |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.