|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
/** |
|
4
|
|
|
* /src/Entity/DateDimension.php |
|
5
|
|
|
* |
|
6
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace App\Entity; |
|
9
|
|
|
|
|
10
|
|
|
use App\Entity\Traits\Blameable; |
|
11
|
|
|
use App\Entity\Traits\Timestampable; |
|
12
|
|
|
use App\Security\RolesService; |
|
13
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
|
15
|
|
|
use Doctrine\ORM\Mapping as ORM; |
|
16
|
|
|
use Ramsey\Uuid\Uuid; |
|
17
|
|
|
use Symfony\Bridge\Doctrine\Validator\Constraints as AssertCollection; |
|
18
|
|
|
use Symfony\Component\Serializer\Annotation\Groups; |
|
19
|
|
|
use Symfony\Component\Validator\Constraints as Assert; |
|
20
|
|
|
use function array_merge; |
|
21
|
|
|
use function array_unique; |
|
22
|
|
|
use function mb_strlen; |
|
23
|
|
|
use function random_int; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class ApiKey |
|
27
|
|
|
* |
|
28
|
|
|
* @AssertCollection\UniqueEntity("token") |
|
29
|
|
|
* |
|
30
|
|
|
* @ORM\Table( |
|
31
|
|
|
* name="api_key", |
|
32
|
|
|
* uniqueConstraints={ |
|
33
|
|
|
* @ORM\UniqueConstraint(name="uq_token", columns={"token"}), |
|
34
|
|
|
* }, |
|
35
|
|
|
* ) |
|
36
|
|
|
* @ORM\Entity() |
|
37
|
|
|
* |
|
38
|
|
|
* @package App\Entity |
|
39
|
|
|
* @author TLe, Tarmo Leppänen <[email protected]> |
|
40
|
|
|
*/ |
|
41
|
|
|
class ApiKey implements EntityInterface |
|
42
|
|
|
{ |
|
43
|
|
|
// Traits |
|
44
|
|
|
use Blameable; |
|
45
|
|
|
use Timestampable; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
* |
|
50
|
|
|
* @Groups({ |
|
51
|
|
|
* "ApiKey", |
|
52
|
|
|
* "ApiKey.id", |
|
53
|
|
|
* }) |
|
54
|
|
|
* |
|
55
|
|
|
* @ORM\Column( |
|
56
|
|
|
* name="id", |
|
57
|
|
|
* type="guid", |
|
58
|
|
|
* nullable=false, |
|
59
|
|
|
* ) |
|
60
|
|
|
* @ORM\Id() |
|
61
|
|
|
*/ |
|
62
|
|
|
private $id; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @var string |
|
66
|
|
|
* |
|
67
|
|
|
* @Groups({ |
|
68
|
|
|
* "ApiKey", |
|
69
|
|
|
* "ApiKey.token", |
|
70
|
|
|
* }) |
|
71
|
|
|
* |
|
72
|
|
|
* @Assert\NotBlank() |
|
73
|
|
|
* @Assert\NotNull() |
|
74
|
|
|
* @Assert\Length(min = 40, max = 40) |
|
75
|
|
|
* |
|
76
|
|
|
* @ORM\Column( |
|
77
|
|
|
* name="token", |
|
78
|
|
|
* type="string", |
|
79
|
|
|
* length=40, |
|
80
|
|
|
* nullable=false |
|
81
|
|
|
* ) |
|
82
|
|
|
*/ |
|
83
|
|
|
private $token = ''; |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var string |
|
87
|
|
|
* |
|
88
|
|
|
* @Groups({ |
|
89
|
|
|
* "ApiKey", |
|
90
|
|
|
* "ApiKey.description", |
|
91
|
|
|
* }) |
|
92
|
|
|
* |
|
93
|
|
|
* @ORM\Column( |
|
94
|
|
|
* name="description", |
|
95
|
|
|
* type="text", |
|
96
|
|
|
* ) |
|
97
|
|
|
*/ |
|
98
|
|
|
private $description = ''; |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @var Collection<UserGroup>|ArrayCollection<UserGroup> |
|
102
|
|
|
* |
|
103
|
|
|
* @Groups({ |
|
104
|
|
|
* "ApiKey.userGroups", |
|
105
|
|
|
* }) |
|
106
|
|
|
* |
|
107
|
|
|
* @ORM\ManyToMany( |
|
108
|
|
|
* targetEntity="UserGroup", |
|
109
|
|
|
* inversedBy="apiKeys", |
|
110
|
|
|
* ) |
|
111
|
|
|
* @ORM\JoinTable( |
|
112
|
|
|
* name="api_key_has_user_group" |
|
113
|
|
|
* ) |
|
114
|
|
|
*/ |
|
115
|
|
|
private $userGroups; |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @var Collection<LogRequest> |
|
119
|
|
|
* |
|
120
|
|
|
* @Groups({ |
|
121
|
|
|
* "ApiKey.logsRequest", |
|
122
|
|
|
* }) |
|
123
|
|
|
* |
|
124
|
|
|
* @ORM\OneToMany( |
|
125
|
|
|
* targetEntity="App\Entity\LogRequest", |
|
126
|
|
|
* mappedBy="apiKey", |
|
127
|
|
|
* ) |
|
128
|
|
|
*/ |
|
129
|
|
|
private $logsRequest; |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* ApiKey constructor. |
|
133
|
|
|
*/ |
|
134
|
72 |
|
public function __construct() |
|
135
|
|
|
{ |
|
136
|
72 |
|
$this->id = Uuid::uuid4()->toString(); |
|
137
|
72 |
|
$this->userGroups = new ArrayCollection(); |
|
138
|
72 |
|
$this->logsRequest = new ArrayCollection(); |
|
139
|
|
|
|
|
140
|
72 |
|
$this->generateToken(); |
|
141
|
72 |
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
3 |
|
public function getId(): string |
|
147
|
|
|
{ |
|
148
|
3 |
|
return $this->id; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
35 |
|
public function getToken(): string |
|
155
|
|
|
{ |
|
156
|
35 |
|
return $this->token; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param string $token |
|
161
|
|
|
* |
|
162
|
|
|
* @return ApiKey |
|
163
|
|
|
*/ |
|
164
|
72 |
|
public function setToken(string $token): self |
|
165
|
|
|
{ |
|
166
|
72 |
|
$this->token = $token; |
|
167
|
|
|
|
|
168
|
72 |
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return ApiKey |
|
173
|
|
|
*/ |
|
174
|
72 |
|
public function generateToken(): self |
|
175
|
|
|
{ |
|
176
|
72 |
|
$random = ''; |
|
177
|
72 |
|
$chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
178
|
72 |
|
$max = mb_strlen($chars, '8bit') - 1; |
|
179
|
|
|
|
|
180
|
72 |
|
for ($i = 0; $i < 40; ++$i) { |
|
181
|
72 |
|
$random .= $chars[random_int(0, $max)]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
72 |
|
return $this->setToken($random); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
8 |
|
public function getDescription(): string |
|
191
|
|
|
{ |
|
192
|
8 |
|
return $this->description; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $description |
|
197
|
|
|
* |
|
198
|
|
|
* @return ApiKey |
|
199
|
|
|
*/ |
|
200
|
4 |
|
public function setDescription(string $description): self |
|
201
|
|
|
{ |
|
202
|
4 |
|
$this->description = $description; |
|
203
|
|
|
|
|
204
|
4 |
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @return Collection<App\Entity\UserGroup>|ArrayCollection<App\Entity\UserGroup> |
|
209
|
|
|
*/ |
|
210
|
36 |
|
public function getUserGroups(): Collection |
|
211
|
|
|
{ |
|
212
|
36 |
|
return $this->userGroups; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Getter for user request log collection. |
|
217
|
|
|
* |
|
218
|
|
|
* @return Collection<App\Entity\LogRequest>|ArrayCollection<App\Entity\LogRequest> |
|
219
|
|
|
*/ |
|
220
|
2 |
|
public function getLogsRequest(): Collection |
|
221
|
|
|
{ |
|
222
|
2 |
|
return $this->logsRequest; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Getter for roles. |
|
227
|
|
|
* |
|
228
|
|
|
* @Groups({ |
|
229
|
|
|
* "ApiKey.roles", |
|
230
|
|
|
* }) |
|
231
|
|
|
* |
|
232
|
|
|
* @return string[] |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getRoles(): array |
|
235
|
|
|
{ |
|
236
|
|
|
/** |
|
237
|
|
|
* Lambda iterator to get user group role information. |
|
238
|
|
|
* |
|
239
|
|
|
* @param UserGroup $userGroup |
|
240
|
|
|
* |
|
241
|
|
|
* @return string |
|
242
|
|
|
*/ |
|
243
|
12 |
|
$iterator = function (UserGroup $userGroup) { |
|
244
|
10 |
|
return $userGroup->getRole()->getId(); |
|
245
|
12 |
|
}; |
|
246
|
|
|
|
|
247
|
12 |
|
return array_unique(array_merge([RolesService::ROLE_API], $this->userGroups->map($iterator)->toArray())); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Method to attach new userGroup to current api key. |
|
252
|
|
|
* |
|
253
|
|
|
* @param UserGroup $userGroup |
|
254
|
|
|
* |
|
255
|
|
|
* @return ApiKey |
|
256
|
|
|
*/ |
|
257
|
5 |
|
public function addUserGroup(UserGroup $userGroup): self |
|
258
|
|
|
{ |
|
259
|
5 |
|
if (!$this->userGroups->contains($userGroup)) { |
|
260
|
5 |
|
$this->userGroups->add($userGroup); |
|
261
|
5 |
|
$userGroup->addApiKey($this); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
5 |
|
return $this; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Method to remove specified userGroup from current api key. |
|
269
|
|
|
* |
|
270
|
|
|
* @param UserGroup $userGroup |
|
271
|
|
|
* |
|
272
|
|
|
* @return ApiKey |
|
273
|
|
|
*/ |
|
274
|
3 |
|
public function removeUserGroup(UserGroup $userGroup): self |
|
275
|
|
|
{ |
|
276
|
3 |
|
if ($this->userGroups->removeElement($userGroup)) { |
|
277
|
2 |
|
$userGroup->removeApiKey($this); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
3 |
|
return $this; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Method to remove all many-to-many userGroup relations from current api key. |
|
285
|
|
|
* |
|
286
|
|
|
* @return ApiKey |
|
287
|
|
|
*/ |
|
288
|
2 |
|
public function clearUserGroups(): self |
|
289
|
|
|
{ |
|
290
|
2 |
|
$this->userGroups->clear(); |
|
291
|
|
|
|
|
292
|
2 |
|
return $this; |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|