-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSymfonyUserProvider.php
More file actions
50 lines (39 loc) · 1.25 KB
/
Copy pathSymfonyUserProvider.php
File metadata and controls
50 lines (39 loc) · 1.25 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
<?php
namespace AppBundle\Security\User;
use AppBundle\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
class DefaultProvider implements UserProviderInterface
{
protected $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function loadUserByUsername($username)
{
$repository = $this->em->getRepository(User::class);
if (!$entity = $repository->find($username)) {
throw new UsernameNotFoundException(
sprintf('Username "%s" does not exist.', $username)
);
}
return $entity;
}
public function refreshUser(UserInterface $user)
{
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(
sprintf('Instances of "%s" are not supported.', $class)
);
}
return $this->loadUserByUsername($user->getId());
}
public function supportsClass($class)
{
return ($class === User::class);
}
}