|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AppBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use AppBundle\Entity\User; |
|
6
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
11
|
|
|
|
|
12
|
|
|
class SecurityController extends Controller |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @Route("/entrar", name="login", methods={"GET"}) |
|
16
|
|
|
*/ |
|
17
|
|
|
public function loginAction() |
|
18
|
|
|
{ |
|
19
|
|
|
$authenticationUtils = $this->get('security.authentication_utils'); |
|
20
|
|
|
|
|
21
|
|
|
// obtener el error de entrada, si existe alguno |
|
22
|
|
|
$error = $authenticationUtils->getLastAuthenticationError(); |
|
23
|
|
|
|
|
24
|
|
|
// último nombre de usuario introducido |
|
25
|
|
|
$lastUsername = $authenticationUtils->getLastUsername(); |
|
26
|
|
|
|
|
27
|
|
|
return $this->render( |
|
28
|
|
|
'security/login.html.twig', |
|
29
|
|
|
array( |
|
30
|
|
|
'last_username' => $lastUsername, |
|
31
|
|
|
'login_error' => $error, |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @Route("/restablecer", name="login_password_reset", methods={"GET", "POST"}) |
|
38
|
|
|
*/ |
|
39
|
|
|
public function passwordResetRequestAction(Request $request) |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
$data = [ |
|
43
|
|
|
'email' => '' |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
$form = $this->createForm('AppBundle\Form\Type\PasswordResetType', $data); |
|
47
|
|
|
|
|
48
|
|
|
$form->handleRequest($request); |
|
49
|
|
|
|
|
50
|
|
|
$data = $form->getData(); |
|
51
|
|
|
$email = $data['email']; |
|
52
|
|
|
$error = ''; |
|
53
|
|
|
|
|
54
|
|
|
// ¿se ha enviado una dirección? |
|
55
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
56
|
|
|
|
|
57
|
|
|
// comprobar que está asociada a un usuario |
|
58
|
|
|
$user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneBy(['email' => $email]); |
|
59
|
|
|
|
|
60
|
|
|
if (null === $user) { |
|
61
|
|
|
$error = $this->get('translator')->trans('form.reset.notfound', [], 'security'); |
|
62
|
|
|
} else { |
|
63
|
|
|
// almacenar como último correo electrónico el indicado |
|
64
|
|
|
$this->get('session')->set('_security.last_username', $email); |
|
65
|
|
|
|
|
66
|
|
|
// obtener tiempo de expiración del token |
|
67
|
|
|
$expire = (int) $this->getParameter('password_reset.expire'); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->getParameter('external.enabled') && $user->getAllowExternalLogin() && $user->hasExternalLogin()) { |
|
70
|
|
|
$this->addFlash('error', $this->get('translator')->trans('form.reset.external_login.error', [], 'security')); |
|
71
|
|
|
} else { |
|
72
|
|
|
// comprobar que no se ha generado un token hace poco |
|
73
|
|
|
if ($user->getToken() && $user->getTokenValidity() > new \DateTime()) { |
|
74
|
|
|
$error = $this->get('translator')->trans('form.reset.wait', ['%expiry%' => $expire], 'security'); |
|
75
|
|
|
} else { |
|
76
|
|
|
// generar un nuevo token |
|
77
|
|
|
$token = bin2hex(random_bytes(16)); |
|
78
|
|
|
$user->setToken($token); |
|
79
|
|
|
|
|
80
|
|
|
// calcular fecha de expiración del token |
|
81
|
|
|
$validity = new \DateTime(); |
|
82
|
|
|
$validity->add(new \DateInterval('PT' . $expire . 'M')); |
|
83
|
|
|
$user->setTokenValidity($validity); |
|
84
|
|
|
|
|
85
|
|
|
// enviar correo |
|
86
|
|
|
if (0 === $this->get('app.mailer')->sendEmail([$user], |
|
87
|
|
|
['id' => 'form.reset.email.subject', 'parameters' => []], |
|
88
|
|
|
[ |
|
89
|
|
|
'id' => 'form.reset.email.body', |
|
90
|
|
|
'parameters' => [ |
|
91
|
|
|
'%name%' => $user->getFirstName(), |
|
92
|
|
|
'%link%' => $this->generateUrl('login_password_reset_do', |
|
93
|
|
|
['userId' => $user->getId(), 'token' => $token], |
|
94
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL), |
|
95
|
|
|
'%expiry%' => $expire |
|
96
|
|
|
] |
|
97
|
|
|
], 'security') |
|
98
|
|
|
) { |
|
99
|
|
|
$this->addFlash('error', $this->get('translator')->trans('form.reset.error', [], 'security')); |
|
100
|
|
|
} else { |
|
101
|
|
|
|
|
102
|
|
|
// guardar token |
|
103
|
|
|
$this->get('doctrine')->getManager()->flush(); |
|
104
|
|
|
|
|
105
|
|
|
$this->addFlash('success', |
|
106
|
|
|
$this->get('translator')->trans('form.reset.sent', ['%email%' => $email], 'security')); |
|
107
|
|
|
return $this->redirectToRoute('login'); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $this->render( |
|
115
|
|
|
':security:login_password_reset.html.twig', [ |
|
116
|
|
|
'last_username' => $this->get('session')->get('_security.last_username', ''), |
|
117
|
|
|
'form' => $form->createView(), |
|
118
|
|
|
'error' => $error |
|
119
|
|
|
] |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @Route("/restablecer/{userId}/{token}", name="login_password_reset_do", methods={"GET", "POST"}) |
|
125
|
|
|
*/ |
|
126
|
|
|
public function passwordResetAction(Request $request, $userId, $token) |
|
127
|
|
|
{ |
|
128
|
|
|
/** |
|
129
|
|
|
* @var User|null |
|
130
|
|
|
*/ |
|
131
|
|
|
$user = $this->getDoctrine()->getManager()->getRepository('AppBundle:User')->findOneBy([ |
|
132
|
|
|
'id' => $userId, |
|
133
|
|
|
'token' => $token |
|
134
|
|
|
]); |
|
135
|
|
|
|
|
136
|
|
|
if (null === $user || ($user->getTokenValidity() < new \DateTime())) { |
|
137
|
|
|
$this->addFlash('error', $this->get('translator')->trans('form.reset.notvalid', [], 'security')); |
|
138
|
|
|
return $this->redirectToRoute('login'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$data = [ |
|
142
|
|
|
'password' => '', |
|
143
|
|
|
'repeat' => '' |
|
144
|
|
|
]; |
|
145
|
|
|
|
|
146
|
|
|
$form = $this->createForm('AppBundle\Form\Type\NewPasswordType', $data); |
|
147
|
|
|
|
|
148
|
|
|
$form->handleRequest($request); |
|
149
|
|
|
|
|
150
|
|
|
$error = ''; |
|
151
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
152
|
|
|
|
|
153
|
|
|
//codificar la nueva contraseña y asignarla al usuario |
|
154
|
|
|
$password = $this->container->get('security.password_encoder') |
|
155
|
|
|
->encodePassword($user, $form->get('newPassword')->get('first')->getData()); |
|
156
|
|
|
$user->setPassword($password)->setToken(null)->setTokenValidity(null); |
|
157
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
158
|
|
|
|
|
159
|
|
|
// indicar que los cambios se han realizado con éxito y volver a la página de inicio |
|
160
|
|
|
$message = $this->get('translator')->trans('form.reset.message', [], 'security'); |
|
161
|
|
|
$this->addFlash('success', $message); |
|
162
|
|
|
return new RedirectResponse( |
|
163
|
|
|
$this->generateUrl('frontpage') |
|
164
|
|
|
); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
return $this->render( |
|
168
|
|
|
':security:login_password_new.html.twig', [ |
|
169
|
|
|
'user' => $user, |
|
170
|
|
|
'form' => $form->createView(), |
|
171
|
|
|
'error' => $error |
|
172
|
|
|
] |
|
173
|
|
|
); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @Route("/comprobar", name="login_check", methods={"POST"}) |
|
178
|
|
|
* @Route("/salir", name="logout", methods={"GET"}) |
|
179
|
|
|
*/ |
|
180
|
|
|
public function logInOutCheckAction() |
|
181
|
|
|
{ |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|