Passed
Push — master ( e8f83f...f116c2 )
by Jan
05:37
created

LogoutDisabledUserSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program 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 for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
/**
24
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
25
 *
26
 * Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
41
 */
42
43
namespace App\EventSubscriber\UserSystem;
44
45
use App\Entity\UserSystem\User;
46
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
47
use Symfony\Component\HttpFoundation\RedirectResponse;
48
use Symfony\Component\HttpKernel\Event\RequestEvent;
49
use Symfony\Component\HttpKernel\KernelEvents;
50
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
51
use Symfony\Component\Security\Core\Security;
52
53
/**
54
 * This subscriber is used to log out a disabled user, as soon as he to do an request.
55
 * It is not possible for him to login again, afterwards.
56
 * @package App\EventSubscriber\UserSystem
57
 */
58
final class LogoutDisabledUserSubscriber implements EventSubscriberInterface
59
{
60
    private $security;
61
    private $urlGenerator;
62
63
    public function __construct(Security $security, UrlGeneratorInterface $urlGenerator)
64
    {
65
        $this->security = $security;
66
67
        $this->urlGenerator = $urlGenerator;
68
    }
69
70
    public function onRequest(RequestEvent $event): void
71
    {
72
        $user = $this->security->getUser();
73
        if ($user instanceof User && $user->isDisabled()) {
74
            //Redirect to login
75
            $response = new RedirectResponse($this->urlGenerator->generate('logout'));
76
            $event->setResponse($response);
77
        }
78
    }
79
80
    /**
81
     * Returns an array of event names this subscriber wants to listen to.
82
     *
83
     * The array keys are event names and the value can be:
84
     *
85
     *  * The method name to call (priority defaults to 0)
86
     *  * An array composed of the method name to call and the priority
87
     *  * An array of arrays composed of the method names to call and respective
88
     *    priorities, or 0 if unset
89
     *
90
     * For instance:
91
     *
92
     *  * ['eventName' => 'methodName']
93
     *  * ['eventName' => ['methodName', $priority]]
94
     *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
95
     *
96
     * @return array The event names to listen to
97
     */
98
    public static function getSubscribedEvents()
99
    {
100
        return [KernelEvents::REQUEST => 'onRequest'];
101
    }
102
}
103