1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the "Kata 1" package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Daniel González |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @author Daniel González <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace App\Controller; |
15
|
|
|
|
16
|
|
|
use Component\Http\RedirectResponse; |
17
|
|
|
use Component\Http\Request; |
18
|
|
|
use Component\Http\Session; |
19
|
|
|
|
20
|
|
|
use App\Repository\UserRepository; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* SecurityController. |
24
|
|
|
*/ |
25
|
|
|
class SecurityController extends AppController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param Request $request |
29
|
|
|
* |
30
|
|
|
* @return Response |
31
|
|
|
*/ |
32
|
5 |
|
public function indexAction(Request $request) |
33
|
|
|
{ |
34
|
|
|
/* @var Session */ |
35
|
5 |
|
$session = $this->app['app.session']; |
36
|
5 |
|
$user = $session->getUser(); |
37
|
5 |
|
if ($user) { |
38
|
|
|
return new RedirectResponse('/'); |
39
|
|
|
} |
40
|
5 |
|
if ($request->getMethod() == 'POST') { |
41
|
|
|
/* @var UserRepository */ |
42
|
5 |
|
$repository = $this->app['app.repository.user']; |
43
|
|
|
|
44
|
5 |
|
$user = $repository->findByNameAndPassword( |
45
|
5 |
|
$request->get('post', 'name', false), |
46
|
5 |
|
$request->get('post', 'password', false) |
47
|
5 |
|
); |
48
|
5 |
|
if (!$user) { |
49
|
1 |
|
return $this->render('Login\\index.html.twig', ['error' => 'Name or password invalid']); |
50
|
|
|
} |
51
|
4 |
|
$session->setUser($user); |
52
|
4 |
|
if ($session->has('app.redirect_on_login')) { |
53
|
2 |
|
return new RedirectResponse($session->get('app.redirect_on_login')); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
return new RedirectResponse('/'); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
return $this->render('Login\\index.html.twig', []); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return RedirectResponse |
64
|
|
|
*/ |
65
|
4 |
|
public function logoutAction() |
66
|
|
|
{ |
67
|
|
|
/* @var Session */ |
68
|
4 |
|
$session = $this->app['app.session']; |
69
|
4 |
|
$session->close(); |
70
|
|
|
|
71
|
4 |
|
return new RedirectResponse('/login'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|