1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
8
|
|
|
use Application\Model\Organization; |
9
|
|
|
use Application\Model\User; |
10
|
|
|
use Application\Repository\OrganizationRepository; |
11
|
|
|
use Application\Repository\UserRepository; |
12
|
|
|
use Application\Service\MessageQueuer; |
13
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
14
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
15
|
|
|
use Ecodev\Felix\Service\Mailer; |
16
|
|
|
use GraphQL\Type\Definition\Type; |
17
|
|
|
use Mezzio\Session\SessionInterface; |
18
|
|
|
|
19
|
|
|
abstract class ConfirmRegistration implements FieldInterface |
20
|
|
|
{ |
21
|
2 |
|
public static function build(): array |
22
|
|
|
{ |
23
|
|
|
return [ |
24
|
1 |
|
'name' => 'confirmRegistration', |
25
|
1 |
|
'type' => Type::nonNull(Type::boolean()), |
26
|
|
|
'description' => 'Second step to register as a new user.', |
27
|
|
|
'args' => [ |
28
|
1 |
|
'token' => Type::nonNull(_types()->get('Token')), |
29
|
1 |
|
'input' => Type::nonNull(_types()->get('ConfirmRegistrationInput')), |
30
|
|
|
], |
31
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): bool { |
32
|
|
|
global $container; |
33
|
|
|
/** @var Mailer $mailer */ |
34
|
2 |
|
$mailer = $container->get(Mailer::class); |
35
|
|
|
|
36
|
|
|
/** @var MessageQueuer $messageQueuer */ |
37
|
2 |
|
$messageQueuer = $container->get(MessageQueuer::class); |
38
|
|
|
|
39
|
|
|
/** @var UserRepository $repository */ |
40
|
2 |
|
$repository = _em()->getRepository(User::class); |
41
|
|
|
|
42
|
|
|
/** @var null|User $user */ |
43
|
2 |
|
$user = $repository->getAclFilter()->runWithoutAcl(fn () => $repository->findOneByToken($args['token'])); |
44
|
|
|
|
45
|
2 |
|
if (!$user) { |
46
|
1 |
|
throw new ExceptionWithoutMailLogging('La session a expiré ou le lien n\'est pas valable. Veuillez effectuer une nouvelle demande.'); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if (!$user->isTokenValid()) { |
50
|
|
|
throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
$input = $args['input']; |
54
|
|
|
|
55
|
|
|
// Do it |
56
|
1 |
|
Helper::hydrate($user, $input); |
57
|
|
|
|
58
|
|
|
// Active the member |
59
|
1 |
|
$user->initialize(); |
60
|
|
|
|
61
|
|
|
// Login |
62
|
1 |
|
User::setCurrent($user); |
63
|
|
|
|
64
|
1 |
|
_em()->flush(); |
65
|
|
|
|
66
|
|
|
// Give user automatic access via organization |
67
|
|
|
/** @var OrganizationRepository $organizationRepository */ |
68
|
1 |
|
$organizationRepository = _em()->getRepository(Organization::class); |
69
|
1 |
|
$organizationRepository->applyOrganizationAccesses(); |
70
|
|
|
|
71
|
1 |
|
foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) { |
72
|
|
|
$message = $messageQueuer->queueConfirmedRegistration($adminEmail, $user); |
73
|
|
|
$mailer->sendMessageAsync($message); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return true; |
77
|
|
|
}, |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|