Completed
Push — development ( 9ee667...757133 )
by Thomas
19s queued 11s
created

LegacyAuthenticator::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OcLegacy\Security;
4
5
use OcLegacy\User\LegacyUser;
6
use Symfony\Component\HttpFoundation\RedirectResponse;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
9
use Symfony\Component\Security\Core\Exception\AuthenticationException;
10
use Symfony\Component\Security\Core\User\UserInterface;
11
use Symfony\Component\Security\Core\User\UserProviderInterface;
12
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
13
14
class LegacyAuthenticator extends AbstractGuardAuthenticator
15
{
16
    /**
17
     * Called on every request. Return whatever credentials you want,
18
     * or null to stop authentication.
19
     *
20
     *
21
     * @return mixed|null
22
     */
23
    public function getCredentials(Request $request)
24
    {
25
        // What you return here will be passed to getUser() as $credentials
26
        $login = $GLOBALS['login'];
27
28
        if (!$login->logged_in()) {
29
            return false;
30
        }
31
32
        return [
33
            'id' => $login->userid,
34
            'username' => $login->username,
35
        ];
36
    }
37
38
    /**
39
     * Returns a UserInterface object based on the credentials.
40
     *
41
     * @param mixed $credentials
42
     */
43
    public function getUser($credentials, UserProviderInterface $userProvider): ?LegacyUser
44
    {
45
        if (!$credentials['id'] || !$credentials['username']) {
46
            return null;
47
        }
48
49
        return new LegacyUser($credentials['id'], $credentials['username']);
50
    }
51
52
    /**
53
     * Returns true if the credentials are valid.
54
     *
55
     * @param mixed $credentials
56
     *
57
     * @return bool
58
     */
59
    public function checkCredentials($credentials, UserInterface $user)
60
    {
61
        // check credentials - e.g. make sure the password is valid
62
        // no credential check is needed in this case
63
64
        // return true to cause authentication success
65
        return true;
66
    }
67
68
    /**
69
     * Called when the authentication is successful.
70
     *
71
     * @param string $providerKey
72
     */
73
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
74
    {
75
        // on success, let the request continue
76
        return null;
77
    }
78
79
    /**
80
     * Called when the authentication fails.
81
     */
82
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
83
    {
84
        return null;
85
    }
86
87
    /**
88
     * Called when authentication is needed, but it's not sent
89
     *
90
     *
91
     * @return RedirectResponse
92
     */
93
    public function start(Request $request, AuthenticationException $authException = null)
94
    {
95
        $currentUri = $request->getUri();
96
        $url = '/login.php?target=' . rawurlencode($currentUri);
97
98
        return new RedirectResponse($url);
99
    }
100
101
    /**
102
     * @return bool
103
     */
104
    public function supportsRememberMe()
105
    {
106
        return false;
107
    }
108
109
    public function supports(Request $request): bool
110
    {
111
        return true;
112
    }
113
}
114