|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Query; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Model\User; |
|
8
|
|
|
use Application\Repository\UserRepository; |
|
9
|
|
|
use Ecodev\Felix\Api\Exception; |
|
10
|
|
|
use Ecodev\Felix\Api\ExceptionWithoutMailLogging; |
|
11
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
12
|
|
|
use GraphQL\Type\Definition\Type; |
|
13
|
|
|
|
|
14
|
|
|
abstract class UserByToken implements FieldInterface |
|
15
|
|
|
{ |
|
16
|
1 |
|
public static function build(): array |
|
17
|
|
|
{ |
|
18
|
|
|
return |
|
19
|
|
|
[ |
|
20
|
1 |
|
'name' => 'userByToken', |
|
21
|
1 |
|
'type' => Type::nonNull(_types()->getOutput(User::class)), |
|
22
|
1 |
|
'description' => 'Get a user by its temporary token', |
|
23
|
|
|
'args' => [ |
|
24
|
1 |
|
'token' => Type::nonNull(_types()->get('Token')), |
|
25
|
|
|
], |
|
26
|
1 |
|
'resolve' => function ($root, array $args): User { |
|
27
|
|
|
/** @var UserRepository $repository */ |
|
28
|
|
|
$repository = _em()->getRepository(User::class); |
|
29
|
|
|
|
|
30
|
|
|
/** @var null|User $user */ |
|
31
|
|
|
$user = $repository->getAclFilter()->runWithoutAcl(function () use ($repository, $args) { |
|
32
|
|
|
return $repository->findOneByToken($args['token']); |
|
33
|
|
|
}); |
|
34
|
|
|
|
|
35
|
|
|
if (!$user) { |
|
36
|
|
|
throw new ExceptionWithoutMailLogging("Le lien que vous avez suivi n'est pas valable ou a déjà été utilisé. Veuillez effectuer une nouvelle demande de changement de mot de passe ou création de compte. Une nouvelle demande invalide les précédentes."); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (!$user->isTokenValid()) { |
|
40
|
|
|
throw new ExceptionWithoutMailLogging('Le lien que vous avez suivi est périmé. Veuillez effectuer une nouvelle demande.'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Set current user for his ACL, but not in persisted session, only for the remaining execution time. |
|
44
|
|
|
// He will have to go through a proper login to persist the session. |
|
45
|
|
|
User::setCurrent($user); |
|
46
|
|
|
|
|
47
|
|
|
return $user; |
|
48
|
1 |
|
}, |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|