-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTokenUserProvider.php
83 lines (71 loc) · 1.91 KB
/
TokenUserProvider.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
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace App\Security;
use App\Entity\User;
use App\Entity\UserToken;
use App\Repository\UserRepository;
use App\Repository\UserTokenRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
/**
* @author Martin PAUCOT <contact@martin-paucot.fr>
*/
class TokenUserProvider implements UserProviderInterface
{
/**
* @var UserTokenRepository
*/
private $userTokenRepository;
/**
* @var UserRepository
*/
private $userRepository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->userRepository = $entityManager->getRepository(User::class);
$this->userTokenRepository = $entityManager->getRepository(UserToken::class);
}
/**
* Returns the UserToken with the given token.
*
* @param string $token
* @return null|UserToken
*/
public function getUserToken(string $token)
{
return $this->userTokenRepository->findOneBy(['token' => $token]);
}
/**
* Returns the User with the given username (email).
*
* @param string $username
* @return UserInterface|null
*/
public function loadUserByUsername($username)
{
return $this->userRepository->findOneBy(['email', $username]);
}
/**
* Refreshes the user.
* We do not use this.
*
* @param UserInterface $user
*
* @return void
*/
public function refreshUser(UserInterface $user): void
{
throw new UnsupportedUserException();
}
/**
* Whether this provider supports the given user class.
*
* @param string $class
* @return bool
*/
public function supportsClass($class): bool
{
return 'App\Entity\User' === $class;
}
}