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