Completed
Push — master ( 8f86ae...511c48 )
by Wouter
02:18
created

FormAuthenticator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 66
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCredentials() 0 14 3
A getUser() 0 4 1
A checkCredentials() 0 9 2
A getLoginUrl() 0 4 1
A getDefaultSuccessRedirectURL() 0 4 1
1
<?php
2
3
namespace SumoCoders\FrameworkMultiUserBundle\Security;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Core\User\UserProviderInterface;
7
use Symfony\Component\Security\Core\User\UserInterface;
8
use Symfony\Component\Security\Core\Exception\AuthenticationException;
9
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
10
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
11
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
12
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
13
14
class FormAuthenticator extends AbstractFormLoginAuthenticator
15
{
16
    /**
17
     * @var UserPasswordEncoderInterface
18
     */
19
    private $passwordDecoder;
20
21
    /**
22
     * @param UserPasswordEncoderInterface $passwordDecoder
23
     */
24
    public function __construct(UserPasswordEncoderInterface $passwordDecoder)
25
    {
26
        $this->passwordDecoder = $passwordDecoder;
27
    }
28
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @return FormCredentials
33
     */
34
    public function getCredentials(Request $request)
35
    {
36
        // @todo: there will probably be a better way te determine if the login
37
        // form has been submitted
38
        if (!$request->request->has('_username')
39
            || !$request->request->has('_password')) {
40
            return;
41
        }
42
43
        return new FormCredentials(
44
            $request->request->get('_username'),
45
            $request->request->get('_password')
46
        );
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function getUser($credentials, UserProviderInterface $userProvider)
53
    {
54
        return $userProvider->loadUserByUsername($credentials->getUsername());
55
    }
56
57
    /**
58
     * {@inheritDoc}
59
     */
60
    public function checkCredentials($credentials, UserInterface $user)
61
    {
62
        $plainPassword = $credentials->getPlainPassword();
63
        $encoder = $this->passwordEncoder;
64
65
        if (!$encoder->isPasswordValid($user, $plainPassword)) {
66
            throw new BadCredentialsException();
67
        }
68
    }
69
70
    protected function getLoginUrl()
71
    {
72
        return '/login';
73
    }
74
75
    protected function getDefaultSuccessRedirectURL()
76
    {
77
        return '/success';
78
    }
79
}
80