|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Daniel West <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\Action\User; |
|
15
|
|
|
|
|
16
|
|
|
use Silverback\ApiComponentsBundle\Exception\UnexpectedValueException; |
|
17
|
|
|
use Silverback\ApiComponentsBundle\Helper\User\PasswordManager; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\Serializer\SerializeFormatResolver; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
21
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
22
|
|
|
use Symfony\Component\Serializer\Encoder\DecoderInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Daniel West <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class PasswordUpdateAction |
|
28
|
|
|
{ |
|
29
|
|
|
private PasswordManager $passwordManager; |
|
30
|
|
|
private SerializeFormatResolver $serializeFormatResolver; |
|
31
|
|
|
private DecoderInterface $decoder; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(PasswordManager $passwordManager, SerializeFormatResolver $serializeFormatResolver, DecoderInterface $decoder) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->passwordManager = $passwordManager; |
|
36
|
|
|
$this->serializeFormatResolver = $serializeFormatResolver; |
|
37
|
|
|
$this->decoder = $decoder; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function __invoke(Request $request): Response |
|
41
|
|
|
{ |
|
42
|
|
|
$format = $this->serializeFormatResolver->getFormatFromRequest($request); |
|
43
|
|
|
$data = $this->decoder->decode($request->getContent(), $format); |
|
44
|
|
|
$requiredKeys = ['username', 'token', 'password']; |
|
45
|
|
|
foreach ($requiredKeys as $requiredKey) { |
|
46
|
|
|
if (!isset($data[$requiredKey])) { |
|
47
|
|
|
throw new BadRequestHttpException(sprintf('the key `%s` was not found in POST data', $requiredKey)); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
try { |
|
51
|
|
|
$this->passwordManager->passwordReset($data['username'], $data['token'], $data['password']); |
|
52
|
|
|
|
|
53
|
|
|
return new Response(null, Response::HTTP_OK); |
|
54
|
|
|
} catch (UnexpectedValueException $exception) { |
|
55
|
|
|
return new Response(null, Response::HTTP_NOT_FOUND); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|