mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 19:59:04 +03:00
Added list of roles to print after an API is generated
This commit is contained in:
parent
1f2e16184c
commit
c49a0ca040
5 changed files with 38 additions and 10 deletions
|
@ -7,6 +7,7 @@ namespace Shlinkio\Shlink\CLI\Command\Api;
|
||||||
use Cake\Chronos\Chronos;
|
use Cake\Chronos\Chronos;
|
||||||
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
|
use Shlinkio\Shlink\CLI\ApiKey\RoleResolverInterface;
|
||||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
|
use Shlinkio\Shlink\CLI\Util\ShlinkTable;
|
||||||
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
use Shlinkio\Shlink\Rest\ApiKey\Role;
|
||||||
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
use Shlinkio\Shlink\Rest\Service\ApiKeyServiceInterface;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
@ -15,6 +16,7 @@ use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
|
use function Shlinkio\Shlink\Core\arrayToString;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
|
|
||||||
class GenerateKeyCommand extends Command
|
class GenerateKeyCommand extends Command
|
||||||
|
@ -81,7 +83,17 @@ class GenerateKeyCommand extends Command
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO Print permissions that have been set
|
// TODO Print permissions that have been set
|
||||||
(new SymfonyStyle($input, $output))->success(sprintf('Generated API key: "%s"', $apiKey->toString()));
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
$io->success(sprintf('Generated API key: "%s"', $apiKey->toString()));
|
||||||
|
|
||||||
|
if (! $apiKey->isAdmin()) {
|
||||||
|
ShlinkTable::fromOutput($io)->render(
|
||||||
|
['Role name', 'Role metadata'],
|
||||||
|
$apiKey->mapRoles(fn (string $name, array $meta) => [$name, arrayToString($meta, 0)]),
|
||||||
|
null,
|
||||||
|
'Roles',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return ExitCodes::EXIT_SUCCESS;
|
return ExitCodes::EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,7 @@ use function Functional\reduce_left;
|
||||||
use function is_array;
|
use function is_array;
|
||||||
use function print_r;
|
use function print_r;
|
||||||
use function sprintf;
|
use function sprintf;
|
||||||
|
use function str_repeat;
|
||||||
|
|
||||||
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
|
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
|
||||||
const DEFAULT_SHORT_CODES_LENGTH = 5;
|
const DEFAULT_SHORT_CODES_LENGTH = 5;
|
||||||
|
@ -79,11 +80,20 @@ function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldN
|
||||||
return $value !== null ? (bool) $value : null;
|
return $value !== null ? (bool) $value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function arrayToString(array $array): string
|
function arrayToString(array $array, int $indentSize = 4): string
|
||||||
{
|
{
|
||||||
return reduce_left($array, fn ($messages, string $name, $_, string $acc) => $acc . sprintf(
|
$indent = str_repeat(' ', $indentSize);
|
||||||
"\n '%s' => %s",
|
$index = 0;
|
||||||
$name,
|
|
||||||
is_array($messages) ? print_r($messages, true) : $messages,
|
return reduce_left($array, static function ($messages, string $name, $_, string $acc) use (&$index, $indent) {
|
||||||
), '');
|
$index++;
|
||||||
|
|
||||||
|
return $acc . sprintf(
|
||||||
|
"%s%s'%s' => %s",
|
||||||
|
$index === 1 ? '' : "\n",
|
||||||
|
$indent,
|
||||||
|
$name,
|
||||||
|
is_array($messages) ? print_r($messages, true) : $messages,
|
||||||
|
);
|
||||||
|
}, '');
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,12 @@ class ValidationException extends InvalidArgumentException implements ProblemDet
|
||||||
public function __toString(): string
|
public function __toString(): string
|
||||||
{
|
{
|
||||||
return sprintf(
|
return sprintf(
|
||||||
'%s %s in %s:%s%s%sStack trace:%s%s',
|
'%s %s in %s:%s%s%s%sStack trace:%s%s',
|
||||||
__CLASS__,
|
__CLASS__,
|
||||||
$this->getMessage(),
|
$this->getMessage(),
|
||||||
$this->getFile(),
|
$this->getFile(),
|
||||||
$this->getLine(),
|
$this->getLine(),
|
||||||
|
PHP_EOL,
|
||||||
arrayToString($this->getInvalidElements()),
|
arrayToString($this->getInvalidElements()),
|
||||||
PHP_EOL,
|
PHP_EOL,
|
||||||
PHP_EOL,
|
PHP_EOL,
|
||||||
|
|
|
@ -39,7 +39,7 @@ class ValidationExceptionTest extends TestCase
|
||||||
$inputFilter = $this->prophesize(InputFilterInterface::class);
|
$inputFilter = $this->prophesize(InputFilterInterface::class);
|
||||||
$getMessages = $inputFilter->getMessages()->willReturn($invalidData);
|
$getMessages = $inputFilter->getMessages()->willReturn($invalidData);
|
||||||
|
|
||||||
$e = ValidationException::fromInputFilter($inputFilter->reveal());
|
$e = ValidationException::fromInputFilter($inputFilter->reveal(), $prev);
|
||||||
|
|
||||||
self::assertEquals($invalidData, $e->getInvalidElements());
|
self::assertEquals($invalidData, $e->getInvalidElements());
|
||||||
self::assertEquals(['invalidElements' => array_keys($invalidData)], $e->getAdditionalData());
|
self::assertEquals(['invalidElements' => array_keys($invalidData)], $e->getAdditionalData());
|
||||||
|
@ -52,6 +52,6 @@ class ValidationExceptionTest extends TestCase
|
||||||
|
|
||||||
public function provideExceptions(): iterable
|
public function provideExceptions(): iterable
|
||||||
{
|
{
|
||||||
return [[null, new RuntimeException(), new LogicException()]];
|
return [[null], [new RuntimeException()], [new LogicException()]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,6 +115,11 @@ class ApiKey extends AbstractEntity
|
||||||
return $role === null ? [] : $role->meta();
|
return $role === null ? [] : $role->meta();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function mapRoles(callable $fun): array
|
||||||
|
{
|
||||||
|
return $this->roles->map(fn (ApiKeyRole $role) => $fun($role->name(), $role->meta()))->getValues();
|
||||||
|
}
|
||||||
|
|
||||||
public function registerRole(RoleDefinition $roleDefinition): void
|
public function registerRole(RoleDefinition $roleDefinition): void
|
||||||
{
|
{
|
||||||
$roleName = $roleDefinition->roleName();
|
$roleName = $roleDefinition->roleName();
|
||||||
|
|
Loading…
Add table
Reference in a new issue