1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2015 Michael Giesler |
4
|
|
|
* |
5
|
|
|
* This file is part of Dembelo. |
6
|
|
|
* |
7
|
|
|
* Dembelo is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Dembelo 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 Affero General Public License 3 for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License 3 |
18
|
|
|
* along with Dembelo. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package DembeloMain |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace DembeloMain\Controller; |
27
|
|
|
|
28
|
|
|
use DembeloMain\Document\User; |
29
|
|
|
use DembeloMain\Form\Login; |
30
|
|
|
use DembeloMain\Form\Registration; |
31
|
|
|
use DembeloMain\Model\Repository\UserRepositoryInterface; |
32
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
33
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
34
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
35
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
36
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
37
|
|
|
use Symfony\Component\HttpFoundation\Request; |
38
|
|
|
use Symfony\Component\HttpFoundation\Response; |
39
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; |
40
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; |
41
|
|
|
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface as Templating; |
42
|
|
|
use Symfony\Bundle\FrameworkBundle\Routing\Router; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Class DefaultController |
46
|
|
|
* @Route(service="app.controller_user") |
47
|
|
|
*/ |
48
|
|
|
class UserController extends Controller |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* @var AuthenticationUtils |
52
|
|
|
*/ |
53
|
|
|
private $authenticationUtils; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var UserRepositoryInterface |
57
|
|
|
*/ |
58
|
|
|
private $userRepository; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var DocumentManager |
62
|
|
|
*/ |
63
|
|
|
private $documentManager; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Templating |
67
|
|
|
*/ |
68
|
|
|
private $templating; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var Router |
72
|
|
|
*/ |
73
|
|
|
private $router; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var FormFactoryInterface |
77
|
|
|
*/ |
78
|
|
|
private $formFactory; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var UserPasswordEncoder |
82
|
|
|
*/ |
83
|
|
|
private $passwordEncoder; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* UserController constructor. |
87
|
|
|
* @param AuthenticationUtils $authenticationUtils |
88
|
|
|
* @param UserRepositoryInterface $userRepository |
89
|
|
|
* @param DocumentManager $documentManager |
90
|
|
|
* @param Templating $templating |
91
|
|
|
* @param Router $router |
92
|
|
|
* @param FormFactoryInterface $formFactory |
93
|
|
|
* @param UserPasswordEncoder $passwordEncoder |
94
|
|
|
*/ |
95
|
7 |
|
public function __construct(AuthenticationUtils $authenticationUtils, UserRepositoryInterface $userRepository, DocumentManager $documentManager, Templating $templating, Router $router, FormFactoryInterface $formFactory, UserPasswordEncoder $passwordEncoder) |
96
|
|
|
{ |
97
|
7 |
|
$this->authenticationUtils = $authenticationUtils; |
98
|
7 |
|
$this->userRepository = $userRepository; |
99
|
7 |
|
$this->documentManager = $documentManager; |
100
|
7 |
|
$this->templating = $templating; |
101
|
7 |
|
$this->router = $router; |
102
|
7 |
|
$this->formFactory = $formFactory; |
103
|
7 |
|
$this->passwordEncoder = $passwordEncoder; |
104
|
7 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @Route("/login", name="login_route") |
108
|
|
|
* |
109
|
|
|
* @return Response |
110
|
|
|
*/ |
111
|
1 |
|
public function loginAction(): Response |
112
|
|
|
{ |
113
|
1 |
|
$error = $this->authenticationUtils->getLastAuthenticationError(); |
114
|
|
|
|
115
|
|
|
// last username entered by the user |
116
|
1 |
|
$lastUsername = $this->authenticationUtils->getLastUsername(); |
117
|
|
|
|
118
|
1 |
|
$user = new User(); |
119
|
1 |
|
$user->setEmail($lastUsername); |
120
|
|
|
|
121
|
1 |
|
$url = $this->router->generate('login_check'); |
122
|
|
|
|
123
|
1 |
|
$form = $this->formFactory->create(Login::class, $user, [ |
124
|
1 |
|
'action' => $url, |
125
|
|
|
]); |
126
|
|
|
|
127
|
1 |
|
return $this->templating->renderResponse( |
128
|
1 |
|
'DembeloMain::user/login.html.twig', |
129
|
|
|
[ |
130
|
1 |
|
'error' => $error, |
131
|
1 |
|
'form' => $form, |
132
|
|
|
] |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @Route("/login_check", name="login_check") |
138
|
|
|
* |
139
|
|
|
* @return void |
140
|
|
|
*/ |
141
|
1 |
|
public function loginCheckAction(): void |
142
|
|
|
{ |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @Route("/registration", name="register") |
147
|
|
|
* |
148
|
|
|
* @param Request $request request object |
149
|
|
|
* |
150
|
|
|
* @return Response |
151
|
|
|
* |
152
|
|
|
* @throws \Exception |
153
|
|
|
*/ |
154
|
2 |
|
public function registrationAction(Request $request): Response |
155
|
|
|
{ |
156
|
2 |
|
$user = new User(); |
157
|
2 |
|
$user->setRoles(['ROLE_USER']); |
158
|
2 |
|
$user->setStatus(0); |
159
|
|
|
|
160
|
2 |
|
$form = $this->formFactory->create(Registration::class, $user); |
161
|
|
|
|
162
|
2 |
|
$form->handleRequest($request); |
163
|
|
|
|
164
|
2 |
|
if ($form->isSubmitted() && $form->isValid()) { |
165
|
1 |
|
$password = $this->passwordEncoder->encodePassword($user, $user->getPassword()); |
166
|
1 |
|
$user->setPassword($password); |
167
|
1 |
|
$user->setMetadata('created', time()); |
168
|
1 |
|
$user->setMetadata('updated', time()); |
169
|
1 |
|
$this->documentManager->persist($user); |
170
|
1 |
|
$this->documentManager->flush(); |
171
|
|
|
|
172
|
1 |
|
return $this->redirectToRoute('registration_success'); |
173
|
|
|
} |
174
|
|
|
|
175
|
1 |
|
return $this->templating->renderResponse( |
176
|
1 |
|
'DembeloMain::user/register.html.twig', |
177
|
|
|
[ |
178
|
1 |
|
'form' => $form->createView(), |
179
|
|
|
] |
180
|
|
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @Route("/registrationSuccess", name="registration_success") |
185
|
|
|
* |
186
|
|
|
* @return Response |
187
|
|
|
*/ |
188
|
1 |
|
public function registrationsuccessAction(): Response |
189
|
|
|
{ |
190
|
1 |
|
return $this->templating->renderResponse('DembeloMain::user/registrationSuccess.html.twig'); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @Route("/activation/{hash}", name="emailactivation") |
195
|
|
|
* |
196
|
|
|
* @param string $hash activation hash |
197
|
|
|
* |
198
|
|
|
* @return Response |
199
|
|
|
*/ |
200
|
2 |
|
public function activateemailAction(string $hash): Response |
201
|
|
|
{ |
202
|
2 |
|
$user = $this->userRepository->findOneBy(['activationHash' => $hash]); |
203
|
2 |
|
if (null === $user) { |
204
|
1 |
|
throw new \InvalidArgumentException('no user found for hash'); |
205
|
|
|
} |
206
|
1 |
|
$user->setActivationHash(''); |
207
|
1 |
|
$user->setStatus(1); |
208
|
1 |
|
$this->documentManager->persist($user); |
209
|
1 |
|
$this->documentManager->flush(); |
210
|
|
|
|
211
|
1 |
|
return $this->templating->renderResponse('DembeloMain::user/activationSuccess.html.twig'); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string $route |
216
|
|
|
* @param array $parameters |
217
|
|
|
* @param int $status |
218
|
|
|
* |
219
|
|
|
* @return RedirectResponse |
220
|
|
|
*/ |
221
|
1 |
|
protected function redirectToRoute($route, array $parameters = array(), $status = 302): RedirectResponse |
222
|
|
|
{ |
223
|
1 |
|
$url = $this->router->generate($route, $parameters); |
224
|
|
|
|
225
|
1 |
|
return new RedirectResponse($url, $status); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|