LogoutDisabledUserSubscriber   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 46
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onRequest() 0 9 3
A getSubscribedEvents() 0 3 1
A __construct() 0 5 1
1
<?php
2
/*
3
 * Copyright (C)  2020-2022  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\EventSubscriber\UserSystem;
20
21
use App\Entity\User;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
use Symfony\Component\HttpFoundation\RedirectResponse;
25
use Symfony\Component\HttpKernel\Event\RequestEvent;
26
use Symfony\Component\HttpKernel\KernelEvents;
27
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
28
use Symfony\Component\Security\Core\Security;
29
30
final class LogoutDisabledUserSubscriber implements EventSubscriberInterface
31
{
32
    private $security;
33
    private $urlGenerator;
34
    private $logger;
35
36
    public function __construct(Security $security, UrlGeneratorInterface $urlGenerator, LoggerInterface $logger)
37
    {
38
        $this->security = $security;
39
        $this->logger = $logger;
40
        $this->urlGenerator = $urlGenerator;
41
    }
42
43
    public function onRequest(RequestEvent $event): void
44
    {
45
        $user = $this->security->getUser();
46
        if ($user instanceof User && $user->isDisabled()) {
47
            $this->logger->notice(sprintf('Disabled user %s tries to login, log him out...', $user->getUsername()));
48
49
            //Redirect to login
50
            $response = new RedirectResponse($this->urlGenerator->generate('app_logout'));
51
            $event->setResponse($response);
52
        }
53
    }
54
55
    /**
56
     * Returns an array of event names this subscriber wants to listen to.
57
     *
58
     * The array keys are event names and the value can be:
59
     *
60
     *  * The method name to call (priority defaults to 0)
61
     *  * An array composed of the method name to call and the priority
62
     *  * An array of arrays composed of the method names to call and respective
63
     *    priorities, or 0 if unset
64
     *
65
     * For instance:
66
     *
67
     *  * ['eventName' => 'methodName']
68
     *  * ['eventName' => ['methodName', $priority]]
69
     *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
70
     *
71
     * @return array The event names to listen to
72
     */
73
    public static function getSubscribedEvents(): array
74
    {
75
        return [KernelEvents::REQUEST => 'onRequest'];
76
    }
77
}