2019-01-27 14:14:18 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
2019-01-27 14:14:18 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioApiTest\Shlink\Rest\Fixtures;
|
|
|
|
|
|
|
|
use Cake\Chronos\Chronos;
|
2021-01-10 10:40:18 +03:00
|
|
|
use Doctrine\Common\DataFixtures\AbstractFixture;
|
|
|
|
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
|
2019-12-16 23:46:27 +03:00
|
|
|
use Doctrine\Persistence\ObjectManager;
|
2021-03-14 11:59:35 +03:00
|
|
|
use ReflectionObject;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Domain\Entity\Domain;
|
2021-03-14 11:59:35 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\ApiKeyMeta;
|
2021-01-10 10:40:18 +03:00
|
|
|
use Shlinkio\Shlink\Rest\ApiKey\Model\RoleDefinition;
|
2019-01-27 14:14:18 +03:00
|
|
|
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
|
|
|
|
2021-01-10 10:40:18 +03:00
|
|
|
class ApiKeyFixture extends AbstractFixture implements DependentFixtureInterface
|
2019-01-27 14:14:18 +03:00
|
|
|
{
|
2021-01-10 10:40:18 +03:00
|
|
|
public function getDependencies(): array
|
|
|
|
{
|
|
|
|
return [DomainFixture::class];
|
|
|
|
}
|
|
|
|
|
2019-01-27 14:14:18 +03:00
|
|
|
public function load(ObjectManager $manager): void
|
|
|
|
{
|
2023-05-31 10:22:40 +03:00
|
|
|
$manager->persist($this->buildApiKey('valid_api_key', enabled: true));
|
|
|
|
$manager->persist($this->buildApiKey('disabled_api_key', enabled: false));
|
|
|
|
$manager->persist($this->buildApiKey(
|
|
|
|
'expired_api_key',
|
|
|
|
enabled: true,
|
|
|
|
expiresAt: Chronos::now()->subDay()->startOfDay(),
|
|
|
|
));
|
2021-01-10 10:40:18 +03:00
|
|
|
|
2023-05-31 10:22:40 +03:00
|
|
|
$authorApiKey = $this->buildApiKey('author_api_key', enabled: true);
|
2021-01-10 10:40:18 +03:00
|
|
|
$authorApiKey->registerRole(RoleDefinition::forAuthoredShortUrls());
|
|
|
|
$manager->persist($authorApiKey);
|
|
|
|
$this->addReference('author_api_key', $authorApiKey);
|
|
|
|
|
|
|
|
/** @var Domain $exampleDomain */
|
|
|
|
$exampleDomain = $this->getReference('example_domain');
|
2023-05-31 10:22:40 +03:00
|
|
|
$domainApiKey = $this->buildApiKey('domain_api_key', enabled: true);
|
2021-01-11 18:32:59 +03:00
|
|
|
$domainApiKey->registerRole(RoleDefinition::forDomain($exampleDomain));
|
2021-01-10 10:40:18 +03:00
|
|
|
$manager->persist($domainApiKey);
|
|
|
|
|
2023-05-31 10:22:40 +03:00
|
|
|
$authorApiKey = $this->buildApiKey('no_orphans_api_key', enabled: true);
|
|
|
|
$authorApiKey->registerRole(RoleDefinition::forNoOrphanVisits());
|
|
|
|
$manager->persist($authorApiKey);
|
|
|
|
|
2019-01-27 14:14:18 +03:00
|
|
|
$manager->flush();
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:49:54 +03:00
|
|
|
private function buildApiKey(string $key, bool $enabled, ?Chronos $expiresAt = null): ApiKey
|
2019-01-27 14:14:18 +03:00
|
|
|
{
|
2021-03-14 11:59:35 +03:00
|
|
|
$apiKey = $expiresAt !== null ? ApiKey::fromMeta(ApiKeyMeta::withExpirationDate($expiresAt)) : ApiKey::create();
|
|
|
|
$ref = new ReflectionObject($apiKey);
|
|
|
|
$keyProp = $ref->getProperty('key');
|
|
|
|
$keyProp->setAccessible(true);
|
|
|
|
$keyProp->setValue($apiKey, $key);
|
2019-01-27 14:14:18 +03:00
|
|
|
|
|
|
|
if (! $enabled) {
|
|
|
|
$apiKey->disable();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $apiKey;
|
|
|
|
}
|
|
|
|
}
|