Repository navigation
Expand file tree
/
Copy pathUserIdentityProvider.php
More file actions
76 lines (64 loc) · 2.16 KB
/
Copy pathUserIdentityProvider.php
File metadata and controls
76 lines (64 loc) · 2.16 KB
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
73
74
75
76
<?php
declare(strict_types=1);
namespace MsgPhp\User\Infrastructure\Security;
use MsgPhp\Domain\Exception\EntityNotFound;
use MsgPhp\User\Repository\UserRepository;
use MsgPhp\User\Role\RoleProvider;
use MsgPhp\User\User;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class UserIdentityProvider implements UserProviderInterface
{
private $repository;
private $roleProvider;
public function __construct(UserRepository $repository, ?RoleProvider $roleProvider = null)
{
$this->repository = $repository;
$this->roleProvider = $roleProvider;
}
/**
* @param string $username
*
* @return UserIdentity
*/
public function loadUserByUsername($username): UserInterface
{
try {
$user = $this->repository->findByUsername($username);
} catch (EntityNotFound $e) {
throw new UsernameNotFoundException($e->getMessage());
}
return $this->fromUser($user, $username);
}
/**
* @return UserIdentity
*/
public function refreshUser(UserInterface $identity): UserInterface
{
if (!$identity instanceof UserIdentity) {
throw new UnsupportedUserException('Unsupported user "'.\get_class($identity).'".');
}
try {
$user = $this->repository->find($identity->getUserId());
} catch (EntityNotFound $e) {
throw new UsernameNotFoundException($e->getMessage());
}
return $this->fromUser($user, $identity->getOriginUsername());
}
/**
* @param string $class
*/
public function supportsClass($class): bool
{
return UserIdentity::class === $class;
}
public function fromUser(User $user, ?string $originUsername = null): UserIdentity
{
return new UserIdentity($user, $originUsername, $this->roleProvider ? $this->roleProvider->getRoles($user) : []);
}
}