1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JwPersistentUser\Service; |
4
|
|
|
|
5
|
|
|
use JwPersistentUser\Model\ModuleOptions; |
6
|
|
|
use JwPersistentUser\Model\SerieTokenInterface; |
7
|
|
|
use JwPersistentUser\Mapper\SerieTokenMapperInterface; |
8
|
|
|
|
9
|
|
|
use Zend\Math\Rand; |
10
|
|
|
|
11
|
|
|
class RememberMeService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var SerieTokenMapperInterface |
15
|
|
|
*/ |
16
|
|
|
protected $mapper; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ModuleOptions |
20
|
|
|
*/ |
21
|
|
|
protected $moduleOptions; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var UserValidityInterface |
25
|
|
|
*/ |
26
|
|
|
protected $userValidityInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param int $userId |
30
|
|
|
* @return SerieTokenInterface |
31
|
|
|
*/ |
32
|
2 |
|
public function createNew($userId) |
33
|
|
|
{ |
34
|
2 |
|
$serieTokenClass = $this->getModuleOptions()->getSerieTokenEntityClass(); |
35
|
2 |
|
$serieToken = new $serieTokenClass; |
36
|
|
|
|
37
|
|
|
/** @var $serieToken SerieTokenInterface */ |
38
|
2 |
|
$serieToken->setUserId($userId); |
39
|
2 |
|
$serieToken->setSerie($this->generateRandom()); |
40
|
2 |
|
$serieToken->setToken($this->generateRandom()); |
41
|
2 |
|
$serieToken->setExpiresAt($this->getNewExpireDate()); |
42
|
2 |
|
$serieToken->setValidUntil($this->getUserValidityInterface()->getValidUntilForUser($userId)); |
43
|
|
|
|
44
|
2 |
|
$this->getMapper()->persist($serieToken); |
45
|
|
|
|
46
|
2 |
|
return $serieToken; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param SerieTokenInterface $serieToken |
51
|
|
|
* @return SerieTokenInterface|null |
52
|
|
|
*/ |
53
|
6 |
|
public function getNextInSerie(SerieTokenInterface $serieToken) |
54
|
|
|
{ |
55
|
6 |
|
$matchingSerieToken = $this->getMapper()->find($serieToken->getUserId(), $serieToken->getSerie()); |
56
|
6 |
|
if (!$matchingSerieToken) { |
57
|
1 |
|
return null; |
58
|
5 |
|
} elseif ($matchingSerieToken->getValidUntil() && $matchingSerieToken->getValidUntil() < new \DateTime) { |
59
|
1 |
|
return null; |
60
|
4 |
|
} elseif (!$matchingSerieToken->getExpiresAt() || $matchingSerieToken->getExpiresAt() < new \DateTime) { |
61
|
1 |
|
return null; |
62
|
3 |
|
} elseif ($matchingSerieToken->getToken() !== $serieToken->getToken()) { |
63
|
1 |
|
$this->removeSerie($serieToken->getUserId(), $serieToken->getSerie()); |
64
|
1 |
|
return null; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
if ($this->moduleOptions->isTokenRefreshedAfterLogin()) { |
68
|
|
|
// Generate new token in the serie |
69
|
1 |
|
$matchingSerieToken->setToken($this->generateRandom()); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
$matchingSerieToken->setExpiresAt($this->getNewExpireDate()); |
73
|
|
|
|
74
|
2 |
|
$this->getMapper()->persist($matchingSerieToken); |
75
|
|
|
|
76
|
2 |
|
return $matchingSerieToken; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \DateTime |
81
|
|
|
*/ |
82
|
4 |
|
protected function getNewExpireDate() |
83
|
|
|
{ |
84
|
4 |
|
return new \DateTime('+1 year'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $userId |
89
|
|
|
* @param string $serieId |
90
|
|
|
*/ |
91
|
2 |
|
public function removeSerie($userId, $serieId) |
92
|
|
|
{ |
93
|
2 |
|
$serieToken = $this->getMapper()->find($userId, $serieId); |
94
|
2 |
|
if ($serieToken) { |
95
|
2 |
|
$this->getMapper()->remove($serieToken); |
96
|
|
|
} |
97
|
2 |
|
} |
98
|
|
|
|
99
|
|
|
public function removeAllSeries(int $userId) |
100
|
|
|
{ |
101
|
|
|
$serieTokens = $this->getMapper()->findAllByUser($userId); |
102
|
|
|
foreach ($serieTokens as $serieToken) { |
103
|
|
|
$this->getMapper()->remove($serieToken); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @note Should NOT contain semicolons. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
3 |
|
private function generateRandom() |
113
|
|
|
{ |
114
|
3 |
|
return Rand::getString(10); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return SerieTokenMapper |
119
|
|
|
*/ |
120
|
9 |
|
public function getMapper() |
121
|
|
|
{ |
122
|
9 |
|
return $this->mapper; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param SerieTokenMapper $mapper |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
9 |
|
public function setMapper($mapper) |
130
|
|
|
{ |
131
|
9 |
|
$this->mapper = $mapper; |
|
|
|
|
132
|
9 |
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return ModuleOptions |
137
|
|
|
*/ |
138
|
2 |
|
public function getModuleOptions() |
139
|
|
|
{ |
140
|
2 |
|
return $this->moduleOptions; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param ModuleOptions $moduleOptions |
145
|
|
|
* @return $this |
146
|
|
|
*/ |
147
|
9 |
|
public function setModuleOptions($moduleOptions) |
148
|
|
|
{ |
149
|
9 |
|
$this->moduleOptions = $moduleOptions; |
150
|
9 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return UserValidityInterface |
155
|
|
|
*/ |
156
|
2 |
|
public function getUserValidityInterface() |
157
|
|
|
{ |
158
|
2 |
|
return $this->userValidityInterface; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param UserValidityInterface $userValidityInterface |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
9 |
|
public function setUserValidityInterface($userValidityInterface) |
166
|
|
|
{ |
167
|
9 |
|
$this->userValidityInterface = $userValidityInterface; |
168
|
9 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..