1
|
|
|
<?php |
2
|
|
|
namespace Shlinkio\Shlink\Rest\Service; |
3
|
|
|
|
4
|
|
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject; |
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException; |
7
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey; |
8
|
|
|
|
9
|
|
|
class ApiKeyService implements ApiKeyServiceInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var EntityManagerInterface |
13
|
|
|
*/ |
14
|
|
|
private $em; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ApiKeyService constructor. |
18
|
|
|
* @param EntityManagerInterface $em |
19
|
|
|
* |
20
|
|
|
* @Inject({"em"}) |
21
|
|
|
*/ |
22
|
10 |
|
public function __construct(EntityManagerInterface $em) |
23
|
|
|
{ |
24
|
10 |
|
$this->em = $em; |
25
|
10 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Creates a new ApiKey with provided expiration date |
29
|
|
|
* |
30
|
|
|
* @param \DateTime $expirationDate |
31
|
|
|
* @return ApiKey |
32
|
|
|
*/ |
33
|
2 |
|
public function create(\DateTime $expirationDate = null) |
34
|
|
|
{ |
35
|
2 |
|
$key = new ApiKey(); |
36
|
2 |
|
if (isset($expirationDate)) { |
37
|
1 |
|
$key->setExpirationDate($expirationDate); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
2 |
|
$this->em->persist($key); |
41
|
2 |
|
$this->em->flush(); |
42
|
|
|
|
43
|
2 |
|
return $key; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Checks if provided key is a valid api key |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
4 |
|
public function check($key) |
53
|
|
|
{ |
54
|
|
|
/** @var ApiKey $apiKey */ |
55
|
4 |
|
$apiKey = $this->getByKey($key); |
56
|
4 |
|
if (! isset($apiKey)) { |
57
|
1 |
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
return $apiKey->isValid(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Disables provided api key |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return ApiKey |
68
|
|
|
*/ |
69
|
2 |
|
public function disable($key) |
70
|
|
|
{ |
71
|
|
|
/** @var ApiKey $apiKey */ |
72
|
2 |
|
$apiKey = $this->getByKey($key); |
73
|
2 |
|
if (! isset($apiKey)) { |
74
|
1 |
|
throw new InvalidArgumentException(sprintf('API key "%s" does not exist and can\'t be disabled', $key)); |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$apiKey->disable(); |
78
|
1 |
|
$this->em->flush(); |
79
|
1 |
|
return $apiKey; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Lists all existing appi keys |
84
|
|
|
* |
85
|
|
|
* @param bool $enabledOnly Tells if only enabled keys should be returned |
86
|
|
|
* @return ApiKey[] |
87
|
|
|
*/ |
88
|
2 |
|
public function listKeys($enabledOnly = false) |
89
|
|
|
{ |
90
|
2 |
|
$conditions = $enabledOnly ? ['enabled' => true] : []; |
91
|
2 |
|
return $this->em->getRepository(ApiKey::class)->findBy($conditions); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Tries to find one API key by its key string |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @return ApiKey|null |
99
|
|
|
*/ |
100
|
6 |
|
public function getByKey($key) |
101
|
|
|
{ |
102
|
6 |
|
return $this->em->getRepository(ApiKey::class)->findOneBy([ |
103
|
6 |
|
'key' => $key, |
104
|
6 |
|
]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|