Completed
Push — master ( 9d6d0f...ec565a )
by Marcel
04:21
created

KioskUserGuardAuthenticator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 21
c 2
b 0
f 0
dl 0
loc 74
ccs 26
cts 26
cp 1
rs 10
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 2 1
A onAuthenticationSuccess() 0 2 1
A checkCredentials() 0 2 1
A getUser() 0 14 3
A getCredentials() 0 6 1
A supportsRememberMe() 0 2 1
A getLoginUrl() 0 2 1
1
<?php
2
3
namespace App\Security;
4
5
use App\Repository\KioskUserRepositoryInterface;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Exception\AuthenticationException;
11
use Symfony\Component\Security\Core\User\UserInterface;
12
use Symfony\Component\Security\Core\User\UserProviderInterface;
13
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
14
15
class KioskUserGuardAuthenticator extends AbstractFormLoginAuthenticator {
16
17
    private $repository;
18
    private $urlGenerator;
19
20 9
    public function __construct(KioskUserRepositoryInterface $repository, UrlGeneratorInterface $urlGenerator) {
21 9
        $this->repository = $repository;
22 9
        $this->urlGenerator = $urlGenerator;
23 9
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28 8
    public function supports(Request $request) {
29 8
        return $request->query->has('token');
30
    }
31
32
    /**
33
     * @inheritDoc
34
     */
35 3
    public function getCredentials(Request $request) {
36 3
        $request->request->set('_remember_me', 'true'); // Force remember me
37
38
        return [
39 3
            'token' => $request->query->get('token'),
40 3
            'ip' => $request->getClientIp()
41
        ];
42
    }
43
44
    /**
45
     * @inheritDoc
46
     */
47 3
    public function getUser($credentials, UserProviderInterface $userProvider) {
48 3
        $user = $this->repository->findOneByToken($credentials['token']);
49
50 3
        if($user === null) {
51 1
            return null;
52
        }
53
54
        // Check IPs
55 2
        $ips = explode(',', $user->getIpAddresses());
56 2
        if(!in_array($credentials['ip'], $ips)) {
57 1
            return null;
58
        }
59
60 1
        return $user->getUser();
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 1
    public function checkCredentials($credentials, UserInterface $user) {
67 1
        return true;
68
    }
69
70
    /**
71
     * @inheritDoc
72
     */
73 1
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) {
74 1
        return new RedirectResponse($this->urlGenerator->generate('dashboard'));
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 1
    public function supportsRememberMe() {
81 1
        return true;
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87 5
    protected function getLoginUrl() {
88 5
        return $this->urlGenerator->generate('login');
89
    }
90
}