|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Handler; |
|
12
|
|
|
|
|
13
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
16
|
|
|
use Symfony\Component\Security\Http\HttpUtils; |
|
17
|
|
|
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler; |
|
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
19
|
|
|
use LoginCidadao\CoreBundle\Entity\AccessSession; |
|
20
|
|
|
|
|
21
|
|
|
class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var EntityManagerInterface */ |
|
24
|
|
|
private $em; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Constructor |
|
28
|
|
|
* @param HttpUtils $httpUtils |
|
29
|
|
|
* @param EntityManagerInterface $em |
|
30
|
|
|
* @param array $options |
|
31
|
|
|
*/ |
|
32
|
1 |
|
public function __construct( |
|
33
|
|
|
HttpUtils $httpUtils, |
|
34
|
|
|
EntityManagerInterface $em, |
|
35
|
|
|
$options |
|
36
|
|
|
) { |
|
37
|
1 |
|
parent::__construct($httpUtils, $options); |
|
38
|
1 |
|
$this->em = $em; |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* This is called when an interactive authentication attempt succeeds. This |
|
43
|
|
|
* is called by authentication listeners inheriting from AbstractAuthenticationListener. |
|
44
|
|
|
* @param Request $request |
|
45
|
|
|
* @param TokenInterface $token |
|
46
|
|
|
* @return Response The response to return |
|
47
|
|
|
*/ |
|
48
|
1 |
|
function onAuthenticationSuccess(Request $request, TokenInterface $token) |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
1 |
|
$form = $request->get('login_form_type'); |
|
51
|
1 |
|
if (isset($form['username'])) { |
|
52
|
|
|
$vars = array( |
|
53
|
1 |
|
'ip' => $request->getClientIp(), |
|
54
|
1 |
|
'username' => $form['username'], |
|
55
|
|
|
); |
|
56
|
1 |
|
$accessSession = $this->em->getRepository('LoginCidadaoCoreBundle:AccessSession')->findOneBy($vars); |
|
57
|
1 |
|
if (!$accessSession) { |
|
|
|
|
|
|
58
|
1 |
|
$accessSession = new AccessSession(); |
|
59
|
1 |
|
$accessSession->fromArray($vars); |
|
60
|
|
|
} |
|
61
|
1 |
|
$accessSession->setVal(0); |
|
62
|
1 |
|
$this->em->persist($accessSession); |
|
63
|
1 |
|
$this->em->flush(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
return parent::onAuthenticationSuccess($request, $token); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.
If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with
private, and only raise it toprotectedif a sub-class needs to have access, orpublicif an external class needs access.