1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\User; |
4
|
|
|
|
5
|
|
|
use App\Entity\User; |
6
|
|
|
use App\Event\User\UserForgotpasswordEvent; |
7
|
|
|
use App\FlashMessage\FlashMessageCategory; |
8
|
|
|
use App\Form\ForgotpasswordFormType; |
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
13
|
|
|
|
14
|
|
|
class ForgotPasswordController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var EventDispatcherInterface |
18
|
|
|
*/ |
19
|
|
|
private $dispatcher; |
20
|
|
|
|
21
|
|
|
public function __construct(EventDispatcherInterface $dispatcher) |
22
|
|
|
{ |
23
|
|
|
$this->dispatcher = $dispatcher; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @Route("/user/forgotpassword", name="app_forgotpassword") |
28
|
|
|
*/ |
29
|
|
|
public function forgotPassword(Request $request) |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
$form = $this->createForm(ForgotpasswordFormType::class); |
33
|
|
|
$form->handleRequest($request); |
34
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
35
|
|
|
|
36
|
|
|
//get the user object from the email or user |
37
|
|
|
//this smells a bit as I don't like calls in a controller. But I don't want to redo a service just for a simple doctrine call |
38
|
|
|
$user = $this->getDoctrine() |
39
|
|
|
->getRepository(User::class) |
40
|
|
|
->findUserByMailOrUsername($form->get('userName')->getData()); |
41
|
|
|
|
42
|
|
|
if ($user) {//Only send mail if an account was found |
43
|
|
|
$event = new UserForgotpasswordEvent($user); |
44
|
|
|
$this->dispatcher->dispatch(UserForgotpasswordEvent::NAME, $event); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
//Do not say if account was found or not to avoid robots testing for emails. This can still be tested by a hacker by calculating the reply time but not as easy. |
48
|
|
|
$this->addFlash(FlashMessageCategory::INFO, |
49
|
|
|
'If you have an account, then an email has been sent to your registered email'); |
50
|
|
|
return $this->redirectToRoute('home'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->render('registration/forgotpassword.html.twig', [ |
54
|
|
|
'forgotpasswordForm' => $form->createView(), |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
} |