SecuritySubscriber::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Smart\AuthenticationBundle\Security\EventSubscriber;
4
5
use Smart\AuthenticationBundle\Security\LastLoginInterface;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
use Symfony\Component\HttpFoundation\Session\Session;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
10
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
11
use Symfony\Component\Security\Http\SecurityEvents;
12
use Smart\AuthenticationBundle\Security\Processor\LastLoginProcessor;
13
use Symfony\Contracts\Translation\TranslatorInterface;
14
15
/**
16
 * @author Mathieu Ducrot <[email protected]>
17
 */
18
class SecuritySubscriber implements EventSubscriberInterface
19
{
20
    /**
21
     * @var LastLoginProcessor
22
     */
23
    private $lastLoginProcessor;
24
25
    /**
26
     * @var TranslatorInterface
27
     */
28
    private $translator;
29
30
    /**
31
     * @param LastLoginProcessor $lastLoginProcessor
32
     */
33
    public function __construct(LastLoginProcessor $lastLoginProcessor, TranslatorInterface $translator)
34
    {
35
        $this->lastLoginProcessor = $lastLoginProcessor;
36
        $this->translator = $translator;
37
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45
            SecurityEvents::SWITCH_USER => 'onSwitchUser',
46
            SecurityEvents::INTERACTIVE_LOGIN => 'onInteractiveLogin',
47
        ];
48
    }
49
50
    /**
51
     * @param InteractiveLoginEvent $event
52
     *
53
     * @return void
54
     */
55
    public function onInteractiveLogin(InteractiveLoginEvent $event)
56
    {
57
        $token = $event->getAuthenticationToken();
58
59
        if (!$token instanceof TokenInterface) {
0 ignored issues
show
introduced by
$token is always a sub-type of Symfony\Component\Securi...on\Token\TokenInterface.
Loading history...
60
            return;
61
        }
62
63
        $user = $token->getUser();
64
65
        if (!$user instanceof LastLoginInterface) {
66
            return;
67
        }
68
69
        // Impersonnate user must not update last_login date
70
        if ($event->getRequest()->get('switch_user') !== null) {
71
            return;
72
        }
73
74
        $this->lastLoginProcessor->process($user);
75
    }
76
77
    /**
78
     * @param SwitchUserEvent $event
79
     *
80
     * @return void
81
     */
82
    public function onSwitchUser(SwitchUserEvent $event)
83
    {
84
        /** @var Session $session */
85
        $session = $event->getRequest()->getSession();
86
        $flashBag = $session->getFlashBag();
87
88
        if ($event->getRequest()->query->get('_switch_user') == '_exit') {
89
            $flashBag->add('sonata_flash_success', $this->translator->trans('impersonate.exit_message'));
90
        } else {
91
            $flashBag->add('sonata_flash_success', $this->translator->trans('impersonate.switch_message'));
92
        }
93
    }
94
}
95