Repository navigation
Expand file tree
/
Copy pathSecurityUserProvider.php
More file actions
67 lines (54 loc) · 2.08 KB
/
Copy pathSecurityUserProvider.php
File metadata and controls
67 lines (54 loc) · 2.08 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
<?php
declare(strict_types=1);
namespace MsgPhp\User\Infra\Security;
use MsgPhp\Domain\Exception\EntityNotFoundException;
use MsgPhp\User\Entity\User;
use MsgPhp\User\Repository\UserRepositoryInterface;
use MsgPhp\User\Role\RoleProviderInterface;
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 SecurityUserProvider implements UserProviderInterface
{
private $repository;
private $roleProvider;
public function __construct(UserRepositoryInterface $repository, RoleProviderInterface $roleProvider = null)
{
$this->repository = $repository;
$this->roleProvider = $roleProvider;
}
public function loadUserByUsername($username): UserInterface
{
try {
$user = $this->repository->findByUsername($username);
} catch (EntityNotFoundException $e) {
throw new UsernameNotFoundException($e->getMessage());
}
return $this->fromUser($user, $username);
}
public function refreshUser(UserInterface $user): UserInterface
{
if (!$user instanceof SecurityUser) {
throw new UnsupportedUserException(sprintf('Unsupported user "%s".', \get_class($user)));
}
$securityUser = $user;
try {
$user = $this->repository->find($securityUser->getUserId());
} catch (EntityNotFoundException $e) {
throw new UsernameNotFoundException($e->getMessage());
}
return $this->fromUser($user, $securityUser->getOriginUsername());
}
public function supportsClass($class): bool
{
return SecurityUser::class === $class;
}
public function fromUser(User $user, string $originUsername = null): SecurityUser
{
return new SecurityUser($user, $originUsername, $this->roleProvider ? $this->roleProvider->getRoles($user) : []);
}
}