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