-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
UserProvider.php
72 lines (58 loc) · 2.24 KB
/
UserProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace App\Security\Core;
use App\Entity\User;
use App\Repository\UserRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\AttributesBasedUserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Uid\Uuid;
/**
* @implements AttributesBasedUserProviderInterface<UserInterface|User>
*/
final readonly class UserProvider implements AttributesBasedUserProviderInterface
{
public function __construct(private ManagerRegistry $registry, private UserRepository $repository)
{
}
public function refreshUser(UserInterface $user): UserInterface
{
$manager = $this->registry->getManagerForClass($user::class);
if (!$manager) {
throw new UnsupportedUserException(sprintf('User class "%s" not supported.', $user::class));
}
$manager->refresh($user);
return $user;
}
public function supportsClass(string $class): bool
{
return User::class === $class;
}
/**
* Create or update User on login.
*/
public function loadUserByIdentifier(string $identifier, array $attributes = []): UserInterface
{
$user = $this->repository->findOneBy(['email' => $identifier]) ?: new User();
$user->email = $identifier;
if (!isset($attributes['sub'])) {
throw new UnsupportedUserException('Property "sub" is missing in token attributes.');
}
try {
$user->sub = Uuid::fromString($attributes['sub']);
} catch (\Throwable $e) {
throw new UnsupportedUserException($e->getMessage(), $e->getCode(), $e);
}
if (!isset($attributes['given_name'])) {
throw new UnsupportedUserException('Property "given_name" is missing in token attributes.');
}
$user->firstName = $attributes['given_name'];
if (!isset($attributes['family_name'])) {
throw new UnsupportedUserException('Property "family_name" is missing in token attributes.');
}
$user->lastName = $attributes['family_name'];
$this->repository->save($user);
return $user;
}
}