2016-08-06 13:18:27 +02:00
|
|
|
<?php
|
2019-10-05 17:26:10 +02:00
|
|
|
|
2017-10-12 10:13:20 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2016-08-06 13:18:27 +02:00
|
|
|
namespace Shlinkio\Shlink\Rest\Service;
|
|
|
|
|
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
2021-03-14 09:59:35 +01:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2023-09-21 09:29:59 +02:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Repository\ApiKeyRepositoryInterface;
|
2016-08-06 13:18:27 +02:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
2019-02-26 22:56:43 +01:00
|
|
|
|
2024-11-05 11:26:39 +01:00
|
|
|
readonly class ApiKeyService implements ApiKeyServiceInterface
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
public function __construct(private EntityManagerInterface $em, private ApiKeyRepositoryInterface $repo)
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-19 09:10:17 +02:00
|
|
|
public function create(ApiKeyMeta $apiKeyMeta): ApiKey
|
|
|
|
|
{
|
|
|
|
|
$apiKey = ApiKey::fromMeta($apiKeyMeta);
|
2016-08-06 13:18:27 +02:00
|
|
|
|
2023-09-19 09:10:17 +02:00
|
|
|
$this->em->persist($apiKey);
|
|
|
|
|
$this->em->flush();
|
2016-08-06 13:18:27 +02:00
|
|
|
|
2023-09-19 09:10:17 +02:00
|
|
|
return $apiKey;
|
2021-03-14 09:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
2024-10-28 22:27:30 +01:00
|
|
|
public function createInitial(string $key): ApiKey|null
|
2023-09-21 09:29:59 +02:00
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
return $this->repo->createInitialApiKey($key);
|
2023-09-21 09:29:59 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-08 11:28:27 +01:00
|
|
|
public function check(string $key): ApiKeyCheckResult
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
$apiKey = $this->findByKey($key);
|
2020-11-08 11:28:27 +01:00
|
|
|
return new ApiKeyCheckResult($apiKey);
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-06 20:10:06 +01:00
|
|
|
* @inheritDoc
|
2016-08-06 13:18:27 +02:00
|
|
|
*/
|
2024-11-06 20:10:06 +01:00
|
|
|
public function disableByName(string $apiKeyName): ApiKey
|
|
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
return $this->disableApiKey($this->findByName($apiKeyName));
|
2024-11-06 20:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*/
|
|
|
|
|
public function disableByKey(string $key): ApiKey
|
|
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
return $this->disableApiKey($this->findByKey($key));
|
2024-11-06 20:10:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function disableApiKey(ApiKey|null $apiKey): ApiKey
|
2016-08-06 13:18:27 +02:00
|
|
|
{
|
2017-12-27 16:23:54 +01:00
|
|
|
if ($apiKey === null) {
|
2024-11-05 11:26:39 +01:00
|
|
|
throw new InvalidArgumentException('Provided API key does not exist and can\'t be disabled');
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$apiKey->disable();
|
|
|
|
|
$this->em->flush();
|
2024-11-05 11:26:39 +01:00
|
|
|
|
2016-08-06 13:18:27 +02:00
|
|
|
return $apiKey;
|
|
|
|
|
}
|
2016-08-06 18:08:09 +02:00
|
|
|
|
2018-10-28 15:24:41 +01:00
|
|
|
/**
|
|
|
|
|
* @return ApiKey[]
|
|
|
|
|
*/
|
2018-07-31 19:53:59 +02:00
|
|
|
public function listKeys(bool $enabledOnly = false): array
|
2016-08-06 18:08:09 +02:00
|
|
|
{
|
|
|
|
|
$conditions = $enabledOnly ? ['enabled' => true] : [];
|
2024-11-07 09:34:42 +01:00
|
|
|
return $this->repo->findBy($conditions);
|
2016-08-06 18:08:09 +02:00
|
|
|
}
|
2016-08-07 19:13:40 +02:00
|
|
|
|
2024-11-07 09:34:42 +01:00
|
|
|
private function findByKey(string $key): ApiKey|null
|
2016-08-07 19:13:40 +02:00
|
|
|
{
|
2024-11-07 09:34:42 +01:00
|
|
|
return $this->repo->findOneBy(['key' => ApiKey::hashKey($key)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function findByName(string $name): ApiKey|null
|
|
|
|
|
{
|
|
|
|
|
return $this->repo->findOneBy(['name' => $name]);
|
2016-08-07 19:13:40 +02:00
|
|
|
}
|
2016-08-06 13:18:27 +02:00
|
|
|
}
|