Passed
Pull Request — master (#346)
by Mirko
08:32
created

LegacyAuthenticator::onAuthenticationSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 2
c 2
b 1
f 0
nc 1
nop 3
dl 0
loc 5
rs 9.4285
1
<?php
2
3
namespace AppBundle\Legacy\Security;
4
5
use AppBundle\Legacy\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
     * @param \Symfony\Component\HttpFoundation\Request $request
21
     *
22
     * @return array
23
     */
24
    public function getCredentials(Request $request)
0 ignored issues
show
Coding Style introduced by
getCredentials uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
25
    {
26
        // What you return here will be passed to getUser() as $credentials
27
        $login = $GLOBALS['login'];
28
29
        if (!$login->logged_in()) {
30
            return false;
31
        }
32
33
        return [
34
            'id' => $login->userid,
35
            'username' => $login->username,
36
        ];
37
    }
38
39
    /**
40
     * @param mixed $credentials
41
     * @param \Symfony\Component\Security\Core\User\UserProviderInterface $userProvider
42
     *
43
     * @return \AppBundle\Legacy\User\LegacyUser|null
44
     */
45
    public function getUser($credentials, UserProviderInterface $userProvider)
46
    {
47
        if (!$credentials['id']) {
48
            return null;
49
        }
50
51
        return new LegacyUser($credentials['id'], $credentials['username']);
52
    }
53
54
    /**
55
     * @param mixed $credentials
56
     * @param \Symfony\Component\Security\Core\User\UserInterface $user
57
     *
58
     * @return bool
59
     */
60
    public function checkCredentials($credentials, UserInterface $user)
61
    {
62
        // check credentials - e.g. make sure the password is valid
63
        // no credential check is needed in this case
64
65
        // return true to cause authentication success
66
        return true;
67
    }
68
69
    /**
70
     * @param \Symfony\Component\HttpFoundation\Request $request
71
     * @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
72
     * @param string $providerKey
73
     *
74
     * @return null
75
     */
76
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
77
    {
78
        // on success, let the request continue
79
        return null;
80
    }
81
82
    /**
83
     * @param \Symfony\Component\HttpFoundation\Request $request
84
     * @param \Symfony\Component\Security\Core\Exception\AuthenticationException $exception
85
     *
86
     * @return null
87
     */
88
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
89
    {
90
        return null;
91
    }
92
93
    /**
94
     * Called when authentication is needed, but it's not sent
95
     *
96
     * @param \Symfony\Component\HttpFoundation\Request $request
97
     * @param \Symfony\Component\Security\Core\Exception\AuthenticationException|null $authException
98
     *
99
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
100
     */
101
    public function start(Request $request, AuthenticationException $authException = null)
102
    {
103
        $currentUri = $request->getUri();
104
        $url = '/login.php?target=' . rawurlencode($currentUri);
105
106
        return new RedirectResponse($url);
107
    }
108
109
    /**
110
     * @return bool
111
     */
112
    public function supportsRememberMe()
113
    {
114
        return false;
115
    }
116
}
117