|
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 Application\Service\MessageQueuer; |
|
12
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
|
13
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
14
|
|
|
use Ecodev\Felix\Api\Scalar\LoginType; |
|
15
|
|
|
use Ecodev\Felix\Service\Mailer; |
|
16
|
|
|
use GraphQL\Type\Definition\Type; |
|
17
|
|
|
use Mezzio\Session\SessionInterface; |
|
18
|
|
|
|
|
19
|
|
|
abstract class RequestPasswordReset implements FieldInterface |
|
20
|
|
|
{ |
|
21
|
4 |
|
public static function build(): array |
|
22
|
|
|
{ |
|
23
|
|
|
return [ |
|
24
|
1 |
|
'name' => 'requestPasswordReset', |
|
25
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
|
26
|
1 |
|
'description' => 'Request to send an email to reset the password for the given user. It will **always** return a successful response, even if the user is not found. Returns `true` if the email was sent to the family owner.', |
|
27
|
|
|
'args' => [ |
|
28
|
1 |
|
'login' => Type::nonNull(_types()->get(LoginType::class)), |
|
29
|
|
|
], |
|
30
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
|
31
|
|
|
global $container; |
|
32
|
|
|
|
|
33
|
4 |
|
_log()->info(LogRepository::REQUEST_PASSWORD_RESET); |
|
34
|
|
|
|
|
35
|
|
|
/** @var LogRepository $logRepository */ |
|
36
|
4 |
|
$logRepository = _em()->getRepository(Log::class); |
|
37
|
4 |
|
if ($logRepository->requestPasswordResetOften()) { |
|
38
|
|
|
throw new ExceptionWithoutMailLogging('Trop de tentatives de changement de mot de passe. Veuillez réessayer plus tard.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @var Mailer $mailer */ |
|
42
|
4 |
|
$mailer = $container->get(Mailer::class); |
|
43
|
|
|
|
|
44
|
|
|
/** @var MessageQueuer $messageQueuer */ |
|
45
|
4 |
|
$messageQueuer = $container->get(MessageQueuer::class); |
|
46
|
|
|
|
|
47
|
|
|
/** @var UserRepository $repository */ |
|
48
|
4 |
|
$repository = _em()->getRepository(User::class); |
|
49
|
|
|
|
|
50
|
|
|
/** @var null|User $user */ |
|
51
|
4 |
|
$user = $repository->getOneByLogin($args['login']); |
|
52
|
|
|
|
|
53
|
4 |
|
if ($user) { |
|
54
|
|
|
$message = $messageQueuer->queueResetPassword($user); |
|
55
|
3 |
|
|
|
56
|
1 |
|
if ($message) { |
|
57
|
|
|
$mailer->sendMessageAsync($message); |
|
58
|
2 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $messageQueuer->wasToFamilyOwner(); |
|
61
|
3 |
|
} |
|
62
|
3 |
|
|
|
63
|
|
|
return false; |
|
64
|
|
|
}, |
|
65
|
3 |
|
]; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|