1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Model\Log; |
8
|
|
|
use Application\Model\User; |
9
|
|
|
use Application\Repository\LogRepository; |
10
|
|
|
use Application\Repository\UserRepository; |
11
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
12
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
13
|
|
|
use Ecodev\Felix\Api\Scalar\PasswordType; |
14
|
|
|
use Ecodev\Felix\Api\Scalar\TokenType; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Mezzio\Session\SessionInterface; |
17
|
|
|
|
18
|
|
|
abstract class UpdatePassword implements FieldInterface |
19
|
|
|
{ |
20
|
2 |
|
public static function build(): iterable |
21
|
|
|
{ |
22
|
1 |
|
yield 'updatePassword' => fn () => [ |
23
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
24
|
1 |
|
'description' => 'Update the password for the user identified by the token. Return false if token is invalid', |
25
|
1 |
|
'args' => [ |
26
|
1 |
|
'token' => Type::nonNull(_types()->get(TokenType::class)), |
27
|
1 |
|
'password' => Type::nonNull(_types()->get(PasswordType::class)), |
28
|
1 |
|
], |
29
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
30
|
|
|
/** @var LogRepository $logRepository */ |
31
|
2 |
|
$logRepository = _em()->getRepository(Log::class); |
32
|
2 |
|
if ($logRepository->updatePasswordFailedOften()) { |
33
|
|
|
throw new ExceptionWithoutMailLogging('Trop de tentatives de changement de mot de passe ont échouées. Veuillez ressayer plus tard.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** @var UserRepository $repository */ |
37
|
2 |
|
$repository = _em()->getRepository(User::class); |
38
|
|
|
|
39
|
|
|
/** @var null|User $user */ |
40
|
2 |
|
$user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token'])); |
41
|
|
|
|
42
|
2 |
|
if (!$user || !$user->isTokenValid()) { |
43
|
1 |
|
_log()->info(LogRepository::UPDATE_PASSWORD_FAILED); |
44
|
|
|
|
45
|
1 |
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
$user->setPassword($args['password']); |
49
|
1 |
|
_em()->flush(); |
50
|
1 |
|
_log()->info(LogRepository::UPDATE_PASSWORD); |
51
|
|
|
|
52
|
1 |
|
return true; |
53
|
1 |
|
}, |
54
|
1 |
|
]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|