Completed
Push — master ( 42da58...861c5b )
by Jafar
02:38
created

LoginFormAuthenticator::loadUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * This file is part of the Guarded Authentication package.
4
 *
5
 * (c) Jafar Jabr <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Jafar\Bundle\GuardedAuthenticationBundle\Guard;
12
13
use Jafar\Bundle\GuardedAuthenticationBundle\Form\GuardedLoginForm;
14
use Symfony\Component\Form\FormFactoryInterface;
15
use Symfony\Component\HttpFoundation\RedirectResponse;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
20
use Symfony\Component\Security\Core\Exception\AuthenticationException;
21
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
22
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
23
use Symfony\Component\Security\Core\Security;
24
use Symfony\Component\Security\Core\User\UserInterface;
25
use Symfony\Component\Security\Core\User\UserProviderInterface;
26
use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticator;
27
28
/**
29
 * {@inheritdoc}
30
 *
31
 * Class LoginFormAuthenticator
32
 *
33
 * @author Jafar Jabr <[email protected]>
34
 */
35
class LoginFormAuthenticator extends AbstractFormLoginAuthenticator
36
{
37
    /**
38
     * @var FormFactoryInterface
39
     */
40
    private $formFactory;
41
42
    /**
43
     * @var RouterInterface
44
     */
45
    private $router;
46
47
    /**
48
     * @var UserPasswordEncoderInterface
49
     */
50
    private $passwordEncoder;
51
52
    /**
53
     * @var string
54
     */
55
    private $loginRoute;
56
57
    /**
58
     * @var string
59
     */
60
    private $homeRoute;
61
62
    /**
63
     * @var string
64
     */
65
    private $wrongEmail = 'Incorrect Email Provided!';
0 ignored issues
show
introduced by
The private property $wrongEmail is not used, and could be removed.
Loading history...
66
67
    /**
68
     * @var string
69
     */
70
    private $wrongPassword = 'Incorrect Password Provided!';
71
72
    /**
73
     * LoginFormAuthenticator constructor.
74
     *
75
     * @param FormFactoryInterface $formFactory
76
     * @param RouterInterface $router
77
     * @param UserPasswordEncoderInterface $passwordEncoder
78
     * @param string $loginRoute
79
     * @param string $homeRoute
80
     */
81
    public function __construct(
82
        FormFactoryInterface $formFactory,
83
        RouterInterface $router,
84
        UserPasswordEncoderInterface $passwordEncoder,
85
        string $loginRoute,
86
        string $homeRoute
87
    )
88
    {
89
        $this->formFactory = $formFactory;
90
        $this->router = $router;
91
        $this->passwordEncoder = $passwordEncoder;
92
        $this->loginRoute = $loginRoute;
93
        $this->homeRoute = $homeRoute;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getCredentials(Request $request)
100
    {
101
        $loginRoute = $this->loginRoute;
102
        $isLoginSubmit = $request->attributes->get('_route') == $loginRoute && $request->isMethod('POST');
103
        if (!$isLoginSubmit) {
104
            return null;
105
        }
106
        $form = $this->formFactory->create(GuardedLoginForm::class);
107
        $form->handleRequest($request);
108
        $data = $form->getData();
109
        if ($request->getSession()) {
110
            $request->getSession()->set(
111
                Security::LAST_USERNAME,
112
                $data['_username']
113
            );
114
        }
115
116
        return $data;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getUser($credentials, UserProviderInterface $userProvider)
123
    {
124
        $username = $credentials['_username'];
125
        return $this->loadUser($userProvider, $username);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function checkCredentials($credentials, UserInterface $user)
132
    {
133
        $password = $credentials['_password'];
134
        if ($this->passwordEncoder->isPasswordValid($user, $password)) {
135
            return true;
136
        }
137
138
        throw new CustomUserMessageAuthenticationException($this->wrongPassword);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
145
    {
146
        $homeRoute = $this->homeRoute;
147
        $url = $this->router->generate($homeRoute);
148
149
        return new RedirectResponse($url);
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
156
    {
157
        return parent::onAuthenticationFailure($request, $exception);
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function supportsRememberMe()
164
    {
165
        return true;
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function start(Request $request, AuthenticationException $authException = null)
172
    {
173
        return parent::start($request, $authException);
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    protected function getLoginUrl()
180
    {
181
        $loginRoute = $this->loginRoute;
182
183
        return $this->router->generate($loginRoute);
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function supports(Request $request)
190
    {
191
        return (bool)$this->getCredentials($request);
192
    }
193
194
    /**
195
     * @param UserProviderInterface $userProvider
196
     * @param string $username
197
     * @return UserInterface
198
     */
199
    private function loadUser(UserProviderInterface $userProvider, string $username)
200
    {
201
        try {
202
            $user = $userProvider->loadUserByUsername($username);
203
        } catch (UsernameNotFoundException $e) {
204
            throw new CustomUserMessageAuthenticationException($e->getMessage());
205
        }
206
        return $user;
207
    }
208
}
209