1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Controller; |
23
|
|
|
|
24
|
|
|
use App\Services\PasswordResetManager; |
25
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
26
|
|
|
use Gregwar\CaptchaBundle\Type\CaptchaType; |
27
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
28
|
|
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType; |
29
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; |
30
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
31
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
32
|
|
|
use Symfony\Component\HttpFoundation\Request; |
33
|
|
|
use Symfony\Component\Mailer\MailerInterface; |
34
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
35
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
36
|
|
|
use Symfony\Component\Validator\Constraints\Length; |
37
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
38
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
39
|
|
|
|
40
|
|
|
class SecurityController extends AbstractController |
41
|
|
|
{ |
42
|
|
|
protected $translator; |
43
|
|
|
|
44
|
|
|
public function __construct(TranslatorInterface $translator) |
45
|
|
|
{ |
46
|
|
|
$this->translator = $translator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @Route("/login", name="login", methods={"GET", "POST"}) |
51
|
|
|
*/ |
52
|
|
|
public function login(AuthenticationUtils $authenticationUtils) |
53
|
|
|
{ |
54
|
|
|
// get the login error if there is one |
55
|
|
|
$error = $authenticationUtils->getLastAuthenticationError(); |
56
|
|
|
|
57
|
|
|
// last username entered by the user |
58
|
|
|
$lastUsername = $authenticationUtils->getLastUsername(); |
59
|
|
|
|
60
|
|
|
return $this->render('security/login.html.twig', [ |
61
|
|
|
'last_username' => $lastUsername, |
62
|
|
|
'error' => $error, |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @Route("/pw_reset/request", name="pw_reset_request") |
68
|
|
|
*/ |
69
|
|
|
public function requestPwReset(PasswordResetManager $passwordReset, Request $request) |
70
|
|
|
{ |
71
|
|
|
$builder = $this->createFormBuilder(); |
72
|
|
|
$builder->add('user', TextType::class, [ |
73
|
|
|
'label' => $this->translator->trans('pw_reset.user_or_password'), |
74
|
|
|
'constraints' => [new NotBlank()] |
75
|
|
|
]); |
76
|
|
|
$builder->add('captcha', CaptchaType::class, [ |
77
|
|
|
'width' => 200, |
78
|
|
|
'height' => 50, |
79
|
|
|
'length' => 6, |
80
|
|
|
]); |
81
|
|
|
$builder->add('submit', SubmitType::class, [ |
82
|
|
|
'label' => 'pw_reset.submit' |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
$form = $builder->getForm(); |
86
|
|
|
$form->handleRequest($request); |
87
|
|
|
|
88
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
89
|
|
|
$passwordReset->request($form->getData()['user']); |
90
|
|
|
$this->addFlash('success', $this->translator->trans('pw_reset.request.success')); |
91
|
|
|
//return $this->redirectToRoute('login'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $this->render('security/pw_reset_request.html.twig', [ |
95
|
|
|
'form' => $form->createView() |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @Route("/pw_reset/new_pw/{user}/{token}", name="pw_reset_new_pw") |
101
|
|
|
*/ |
102
|
|
|
public function pwResetNewPw(PasswordResetManager $passwordReset, Request $request, string $user = null, string $token = null) |
103
|
|
|
{ |
104
|
|
|
$data = ['username' => $user, 'token' => $token]; |
105
|
|
|
$builder = $this->createFormBuilder($data); |
106
|
|
|
$builder->add('username', TextType::class, [ |
107
|
|
|
'label' => $this->translator->trans('pw_reset.username') |
108
|
|
|
]); |
109
|
|
|
$builder->add('token', TextType::class, [ |
110
|
|
|
'label' => $this->translator->trans('pw_reset.token') |
111
|
|
|
]); |
112
|
|
|
$builder->add('new_password', RepeatedType::class, [ |
113
|
|
|
'type' => PasswordType::class, |
114
|
|
|
'first_options' => ['label' => 'user.settings.pw_new.label'], |
115
|
|
|
'second_options' => ['label' => 'user.settings.pw_confirm.label'], |
116
|
|
|
'invalid_message' => 'password_must_match', |
117
|
|
|
'constraints' => [new Length([ |
118
|
|
|
'min' => 6, |
119
|
|
|
'max' => 128, |
120
|
|
|
])], |
121
|
|
|
]); |
122
|
|
|
|
123
|
|
|
$builder->add('submit', SubmitType::class, [ |
124
|
|
|
'label' => 'pw_reset.submit' |
125
|
|
|
]); |
126
|
|
|
|
127
|
|
|
$form = $builder->getForm(); |
128
|
|
|
$form->handleRequest($request); |
129
|
|
|
|
130
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
131
|
|
|
$data = $form->getData(); |
132
|
|
|
//Try to set the new password |
133
|
|
|
$success = $passwordReset->setNewPassword($data['username'], $data['token'], $data['new_password']); |
134
|
|
|
if (!$success) { |
135
|
|
|
$this->addFlash('error', $this->translator->trans('pw_reset.new_pw.error')); |
136
|
|
|
} else { |
137
|
|
|
$this->addFlash('success', $this->translator->trans('pw_reset.new_pw.success')); |
138
|
|
|
return $this->redirectToRoute('login'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
return $this->render('security/pw_reset_new_pw.html.twig', [ |
144
|
|
|
'form' => $form->createView() |
145
|
|
|
]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @Route("/logout", name="logout") |
150
|
|
|
*/ |
151
|
|
|
public function logout() |
152
|
|
|
{ |
153
|
|
|
throw new \RuntimeException('Will be intercepted before getting here'); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|