1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Manager; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
7
|
|
|
use Yokai\SecurityTokenBundle\Factory\TokenFactoryInterface; |
8
|
|
|
use Yokai\SecurityTokenBundle\InformationGuesser\InformationGuesserInterface; |
9
|
|
|
use Yokai\SecurityTokenBundle\Repository\TokenRepositoryInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Yann Eugoné <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class TokenManager implements TokenManagerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var TokenFactoryInterface |
18
|
|
|
*/ |
19
|
|
|
private $factory; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var TokenRepositoryInterface |
23
|
|
|
*/ |
24
|
|
|
private $repository; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var InformationGuesserInterface |
28
|
|
|
*/ |
29
|
|
|
private $informationGuesser; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var UserManagerInterface |
33
|
|
|
*/ |
34
|
|
|
private $userManager; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param TokenFactoryInterface $factory |
38
|
|
|
* @param TokenRepositoryInterface $repository |
39
|
|
|
* @param InformationGuesserInterface $informationGuesser |
40
|
|
|
* @param UserManagerInterface $userManager |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct( |
43
|
|
|
TokenFactoryInterface $factory, |
44
|
|
|
TokenRepositoryInterface $repository, |
45
|
|
|
InformationGuesserInterface $informationGuesser, |
46
|
|
|
UserManagerInterface $userManager |
47
|
|
|
) { |
48
|
4 |
|
$this->factory = $factory; |
49
|
4 |
|
$this->repository = $repository; |
50
|
4 |
|
$this->informationGuesser = $informationGuesser; |
51
|
4 |
|
$this->userManager = $userManager; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
1 |
|
public function get($purpose, $value) |
58
|
|
|
{ |
59
|
1 |
|
return $this->repository->get($value, $purpose); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
1 |
|
public function create($purpose, $user, array $payload = []) |
66
|
|
|
{ |
67
|
|
|
do { |
68
|
1 |
|
$token = $this->factory->create($user, $purpose, $payload); |
69
|
1 |
|
} while ($this->repository->exists($token->getValue(), $purpose)); |
70
|
|
|
|
71
|
1 |
|
$this->repository->create($token); |
72
|
|
|
|
73
|
1 |
|
return $token; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritdoc |
78
|
|
|
*/ |
79
|
1 |
|
public function setUsed(Token $token, DateTime $at = null) |
80
|
|
|
{ |
81
|
1 |
|
@trigger_error( |
|
|
|
|
82
|
|
|
'The '.__METHOD__ |
83
|
1 |
|
.' method is deprecated since version 2.2 and will be removed in 3.0. Use the consume() method instead.', |
84
|
1 |
|
E_USER_DEPRECATED |
85
|
|
|
); |
86
|
|
|
|
87
|
1 |
|
$this->consume($token, $at); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
1 |
|
public function consume(Token $token, DateTime $at = null) |
94
|
|
|
{ |
95
|
1 |
|
$token->consume($this->informationGuesser->get(), $at); |
96
|
|
|
|
97
|
1 |
|
$this->repository->update($token); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritdoc |
102
|
|
|
*/ |
103
|
1 |
|
public function getUser(Token $token) |
104
|
|
|
{ |
105
|
1 |
|
return $this->userManager->get($token->getUserClass(), $token->getUserId()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: