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

FormAuthenticator::checkCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
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