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\Service\Mailer; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Mezzio\Session\SessionInterface; |
17
|
|
|
|
18
|
|
|
abstract class RequestPasswordReset implements FieldInterface |
19
|
|
|
{ |
20
|
4 |
|
public static function build(): array |
21
|
|
|
{ |
22
|
1 |
|
return [ |
23
|
1 |
|
'name' => 'requestPasswordReset', |
24
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
25
|
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.', |
26
|
1 |
|
'args' => [ |
27
|
1 |
|
'login' => Type::nonNull(Type::string()), |
28
|
1 |
|
], |
29
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
30
|
|
|
global $container; |
31
|
|
|
|
32
|
4 |
|
_log()->info(LogRepository::REQUEST_PASSWORD_RESET); |
33
|
|
|
|
34
|
|
|
/** @var LogRepository $logRepository */ |
35
|
4 |
|
$logRepository = _em()->getRepository(Log::class); |
36
|
4 |
|
if ($logRepository->requestPasswordResetOften()) { |
37
|
|
|
throw new ExceptionWithoutMailLogging('Trop de tentatives de changement de mot de passe. Veuillez réessayer plus tard.'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** @var Mailer $mailer */ |
41
|
4 |
|
$mailer = $container->get(Mailer::class); |
42
|
|
|
|
43
|
|
|
/** @var MessageQueuer $messageQueuer */ |
44
|
4 |
|
$messageQueuer = $container->get(MessageQueuer::class); |
45
|
|
|
|
46
|
|
|
/** @var UserRepository $repository */ |
47
|
4 |
|
$repository = _em()->getRepository(User::class); |
48
|
|
|
|
49
|
|
|
/** @var null|User $user */ |
50
|
4 |
|
$user = $repository->getOneByLoginOrEmail($args['login']); |
51
|
|
|
|
52
|
4 |
|
if ($user) { |
53
|
3 |
|
$message = $messageQueuer->queueResetPassword($user); |
54
|
|
|
|
55
|
3 |
|
if ($message) { |
56
|
3 |
|
$mailer->sendMessageAsync($message); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
return $messageQueuer->wasToFamilyOwner(); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return false; |
63
|
1 |
|
}, |
64
|
1 |
|
]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|