-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForgotResetController.php
More file actions
163 lines (140 loc) · 6.55 KB
/
ForgotResetController.php
File metadata and controls
163 lines (140 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Dayspring\LoginBundle\Controller;
use Dayspring\LoginBundle\Entity\ChangePasswordEntity;
use Dayspring\LoginBundle\Form\Type\ChangePasswordType;
use Dayspring\LoginBundle\Form\Type\ResetPasswordType;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
class ForgotResetController extends AbstractController
{
public function __construct(
protected UserProviderInterface $userProvider,
protected RequestStack $requestStack,
protected MailerInterface $mailer,
protected TokenStorageInterface $tokenStorage,
protected UserPasswordHasherInterface $userPasswordHasher
) {
}
/**
* @Route("/forgot-password", name="forgot_password")
*/
public function forgotPasswordAction(Request $request)
{
$genericMsg = 'Your request has been sent. If an account was found, an email has been sent. Please check your email for further instructions.';
$form = $this->createFormBuilder([])
->add('email', EmailType::class)
->getForm();
if ($request->getMethod() == "POST") {
$form->handleRequest($request);
$data = $form->getData();
$email = $data['email'];
try {
$user = $this->userProvider->loadUserByUsername($email);
if ($user->getIsActive()) {
$user->generateResetToken();
$subject = "Reset Password";
$data = ['user' => $user];
$fromAddress = $this->getParameter('login_bundle.from_address');
$fromDisplayName = $this->getParameter('login_bundle.from_display_name');
$message = (new Email())
->subject($subject)
->to($user->getEmail())
->html($this->renderView(
'@DayspringLogin/Emails/reset_password.html.twig',
$data
));
if (is_array($fromAddress)) {
foreach($fromAddress as $from) {
$message ->from($from);
}
} else {
$message->from($fromAddress);
}
$this->mailer->send($message);
}
} catch (UserNotFoundException $e) {
// do not throw an error for UsernameNotFoundException
}
$request->getSession()->getFlashBag()->add(
"success",
$genericMsg
);
return $this->redirect($this->generateUrl('_login'));
}
return $this->render('@DayspringLogin/ForgotReset/forgotPassword.html.twig', ['form' => $form->createView()]);
}
/**
* @Route("/reset-password/{resetToken}", name="reset_password", defaults={"resetToken"=null})
*/
public function resetPasswordAction(Request $request, $resetToken)
{
$user = $this->userProvider->loadUserByResetToken($resetToken);
if ($user) {
$form = $this->createForm(ResetPasswordType::class, $user);
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$encoded = $this->userPasswordHasher->hashPassword($user, $data->getPassword());
$user->setPassword($encoded);
$user->save();
$data->setResetToken(null);
$data->setResetTokenExpire(null);
$data->save();
$request->getSession()->getFlashBag()->add(
'success',
'New password has been saved, please login with new password.'
);
return $this->redirect($this->generateUrl('_login'));
}
}
return $this->render('@DayspringLogin/ForgotReset/resetPassword.html.twig', ['form' => $form->createView()]);
} else {
throw new AccessDeniedHttpException("No User found with this reset token.");
}
}
/**
* @Route("/account/change-password", name="change_password")
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function changePasswordAction(Request $request)
{
$currentUser = $this->getUser();
$form = $this->createForm(ChangePasswordType::class, new ChangePasswordEntity());
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();
$encoded = $this->userPasswordHasher->hashPassword($currentUser, $data->getNewPassword());
$currentUser->setPassword($encoded);
$currentUser->save();
// $token = new UsernamePasswordToken(
// $currentUser,
// $data->getNewPassword(),
// "secured_area",
// $currentUser->getRoles()
// );
// $token = $this->authenticationManager->authenticate($token);
// $this->tokenStorage->setToken($token);
$this->requestStack->getSession()->getFlashBag()->add('success', 'New password has been saved.');
return $this->redirect($this->generateUrl("account_dashboard"));
}
}
return $this->render('@DayspringLogin/ForgotReset/changePassword.html.twig', ['form' => $form->createView()]);
}
}