Completed
Pull Request — development (#673)
by Nick
12:54 queued 04:44
created

LegacyAuthenticator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCredentials() 0 14 2
A getUser() 0 8 3
A checkCredentials() 0 8 1
A onAuthenticationSuccess() 0 5 1
A onAuthenticationFailure() 0 4 1
A start() 0 7 1
A supportsRememberMe() 0 4 1
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
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
21
     *
22
     * @return mixed|null
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
     * Returns a UserInterface object based on the credentials.
41
     *
42
     * @param mixed $credentials
43
     * @param UserProviderInterface $userProvider
0 ignored issues
show
introduced by
UserProviderInterface => \Symfony\Component\Security\Core\User\UserProviderInterface
Loading history...
44
     *
45
     * @return \OcLegacy\User\LegacyUser|null
46
     */
47
    public function getUser($credentials, UserProviderInterface $userProvider)
48
    {
49
        if (!$credentials['id'] || !$credentials['username']) {
50
            return null;
51
        }
52
53
        return new LegacyUser($credentials['id'], $credentials['username']);
54
    }
55
56
    /**
57
     * Returns true if the credentials are valid.
58
     *
59
     * @param mixed $credentials
60
     * @param UserInterface $user
0 ignored issues
show
introduced by
UserInterface => \Symfony\Component\Security\Core\User\UserInterface
Loading history...
61
     *
62
     * @return bool
63
     */
64
    public function checkCredentials($credentials, UserInterface $user)
65
    {
66
        // check credentials - e.g. make sure the password is valid
67
        // no credential check is needed in this case
68
69
        // return true to cause authentication success
70
        return true;
71
    }
72
73
    /**
74
     * Called when the authentication is successful.
75
     *
76
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
77
     * @param TokenInterface $token
0 ignored issues
show
introduced by
TokenInterface => \Symfony\Component\Security\Core\Authentication\Token\TokenInterface
Loading history...
78
     * @param string $providerKey
79
     *
80
     * @return null
81
     */
82
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
83
    {
84
        // on success, let the request continue
85
        return null;
86
    }
87
88
    /**
89
     * Called when the authentication fails.
90
     *
91
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
92
     * @param AuthenticationException $exception
0 ignored issues
show
introduced by
AuthenticationException => \Symfony\Component\Security\Core\Exception\AuthenticationException
Loading history...
93
     *
94
     * @return null
95
     */
96
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
97
    {
98
        return null;
99
    }
100
101
    /**
102
     * Called when authentication is needed, but it's not sent
103
     *
104
     * @param Request $request
0 ignored issues
show
introduced by
Request => \Symfony\Component\HttpFoundation\Request
Loading history...
105
     * @param AuthenticationException|null $authException
0 ignored issues
show
introduced by
AuthenticationException => \Symfony\Component\Security\Core\Exception\AuthenticationException
Loading history...
106
     *
107
     * @return RedirectResponse
0 ignored issues
show
introduced by
RedirectResponse => \Symfony\Component\HttpFoundation\RedirectResponse
Loading history...
108
     */
109
    public function start(Request $request, AuthenticationException $authException = null)
110
    {
111
        $currentUri = $request->getUri();
112
        $url = '/login.php?target=' . rawurlencode($currentUri);
113
114
        return new RedirectResponse($url);
115
    }
116
117
    /**
118
     * @return bool
119
     */
120
    public function supportsRememberMe()
121
    {
122
        return false;
123
    }
124
}
125