-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security][SecurityBundle] Dump role hierarchy as mermaid chart #61034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 7.4
Are you sure you want to change the base?
Changes from 1 commit
02f2761
dcfe489
d1a118c
c120b3c
bac240e
8e135fe
a61751b
c39867b
cfc4e24
b21d28d
0b86dfa
File filter
Filter by extension
Conversations
Jump to
Diff view
10000
div>
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Completion\CompletionInput; | ||
use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Security\Core\Dumper\MermaidDumper; | ||
use Symfony\Co 10000 mponent\Security\Core\Role\RoleHierarchyInterface; | ||
|
||
/** | ||
* Command to dump the role hierarchy as a Mermaid flowchart. | ||
* | ||
* @author Your Name <your.email@example.com> | ||
* | ||
* @final | ||
*/ | ||
#[AsCommand(name: 'debug:security:role-hierarchy', description: 'Dump the role hierarchy as a Mermaid flowchart')] | ||
class SecurityRoleHierarchyDumpCommand extends Command | ||
{ | ||
private const DIRECTION_OPTIONS = [ | ||
'TB', | ||
'TD', | ||
'BT', | ||
'RL', | ||
'LR', | ||
]; | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function __construct( | ||
private readonly ?RoleHierarchyInterface $roleHierarchy = null, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how can it be null via service declaration There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We remove the role hierarchy service if you don't configure any hierarchy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we then better remove this command as well ? useless to register a command if its to dump nothing |
||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDefinition([ | ||
new InputOption('direction', 'd', InputOption::VALUE_REQUIRED, 'The direction of the flowchart ['.implode('|', self::DIRECTION_OPTIONS).']', 'TB'), | ||
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The output format', 'mermaid'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The command implementation being strongly tight to mermaid at the moment, I suggest not to add this format option until we consider supporting another format. |
||
]) | ||
->setHelp(<<<'USAGE' | ||
The <info>%command.name%</info> command dumps the role hierarchy in different formats. | ||
|
||
<info>Mermaid</info>: %command.full_name% > roles.mmd | ||
<info>Mermaid with direction</info>: %command.full_name% --direction=BT > roles.mmd | ||
USAGE | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
if (null === $this->roleHierarchy) { | ||
$output->writeln('<comment>No role hierarchy is configured.</comment>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be written to STDERR as it is about human-readable message. this is especially important for that command which will generally send the output to a file. When using the |
||
return Command::SUCCESS; | ||
} | ||
|
||
$direction = $input->getOption('direction'); | ||
$format = $input->getOption('format'); | ||
|
||
if ('mermaid' !== $format) { | ||
$output->writeln('<error>Only "mermaid" format is currently supported.</error>'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should also be written to STDERR. |
||
return Command::FAILURE; | ||
} | ||
|
||
if (!in_array($direction, self::DIRECTION_OPTIONS, true)) { | ||
$output->writeln('<error>Invalid direction. Available options: '.implode(', ', self::DIRECTION_OPTIONS).'</error>'); | ||
return Command::FAILURE; | ||
} | ||
|
||
// Map console direction options to dumper constants | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$directionMap = [ | ||
'TB' => MermaidDumper::DIRECTION_TOP_TO_BOTTOM, | ||
'TD' => MermaidDumper::DIRECTION_TOP_DOWN, | ||
'BT' => MermaidDumper::DIRECTION_BOTTOM_TO_TOP, | ||
'RL' => MermaidDumper::DIRECTION_RIGHT_TO_LEFT, | ||
'LR' => MermaidDumper::DIRECTION_LEFT_TO_RIGHT, | ||
]; | ||
|
||
$dumper = new MermaidDumper($directionMap[$direction]); | ||
$mermaidOutput = $dumper->dump($this->roleHierarchy); | ||
|
||
$output->writeln($mermaidOutput); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void | ||
{ | ||
if ($input->mustSuggestOptionValuesFor('direction')) { | ||
$suggestions->suggestValues(self::DIRECTION_OPTIONS); | ||
} | ||
|
||
if ($input->mustSuggestOptionValuesFor('format')) { | ||
$suggestions->suggestValues(['mermaid']); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bundle\SecurityBundle\Command\SecurityRoleHierarchyDumpCommand; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Security\Core\Role\RoleHierarchy; | ||
|
||
class SecurityRoleHierarchyDumpCommandTest extends TestCase | ||
{ | ||
public function testExecuteWithNoRoleHierarchy() | ||
{ | ||
$command = new SecurityRoleHierarchyDumpCommand(); | ||
$commandTester = new CommandTester($command); | ||
|
||
$exitCode = $commandTester->execute([]); | ||
|
||
$this->assertEquals(Command::SUCCESS, $exitCode); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertStringContainsString('No role hierarchy is configured', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteWithRoleHierarchy() | ||
{ | ||
$hierarchy = [ | ||
'ROLE_ADMIN' => ['ROLE_USER'], | ||
'ROLE_SUPER_ADMIN' => ['ROLE_ADMIN', 'ROLE_USER'], | ||
]; | ||
|
||
$roleHierarchy = new RoleHierarchy($hierarchy); | ||
$command = new SecurityRoleHierarchyDumpCommand($roleHierarchy); | ||
$commandTester = new CommandTester($command); | ||
|
||
$exitCode = $commandTester->execute([]); | ||
|
||
$this->assertEquals(Command::SUCCESS, $exitCode); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$output = $commandTester->getDisplay(); | ||
$this->assertStringContainsString('graph TB', $output); | ||
$this->assertStringContainsString('ROLE_ADMIN --> ROLE_USER', $output); | ||
$this->assertStringContainsString('ROLE_SUPER_ADMIN --> ROLE_ADMIN', $output); | ||
$this->assertStringContainsString('ROLE_SUPER_ADMIN --> ROLE_USER', $output); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public function testExecuteWithCustomDirection() | ||
{ | ||
$hierarchy = [ | ||
'ROLE_ADMIN' => ['ROLE_USER'], | ||
]; | ||
|
||
$roleHierarchy = new RoleHierarchy($hierarchy); | ||
$command = new SecurityRoleHierarchyDumpCommand($roleHierarchy); | ||
$commandTester = new CommandTester($command); | ||
|
||
$exitCode = $commandTester->execute(['--direction' => 'BT']); | ||
|
||
$this->assertEquals(Command::SUCCESS, $exitCode); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$output = $commandTester->getDisplay(); | ||
$this->assertStringContainsString('graph BT', $output); | ||
} | ||
|
||
public function testExecuteWithInvalidDirection() | ||
{ | ||
$hierarchy = [ | ||
'ROLE_ADMIN' => ['ROLE_USER'], | ||
]; | ||
|
||
$roleHierarchy = new RoleHierarchy($hierarchy); | ||
$command = new SecurityRoleHierarchyDumpCommand($roleHierarchy); | ||
$commandTester = new CommandTester($command); | ||
|
||
$exitCode = $commandTester->execute(['--direction' => 'INVALID']); | ||
|
||
$this->assertEquals(Command::FAILURE, $exitCode); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertStringContainsString('Invalid direction', $commandTester->getDisplay()); | ||
} | ||
|
||
public function testExecuteWithInvalidFormat() | ||
{ | ||
$hierarchy = [ | ||
'ROLE_ADMIN' => ['ROLE_USER'], | ||
]; | ||
|
||
$roleHierarchy = new RoleHierarchy($hierarchy); | ||
$command = new SecurityRoleHierarchyDumpCommand($roleHierarchy); | ||
$commandTester = new CommandTester($command); | ||
|
||
$exitCode = $commandTester->execute(['--format' => 'dot']); | ||
|
||
$this->assertEquals(Command::FAILURE, $exitCode); | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$this->assertStringContainsString('Only "mermaid" format is currently supported', $commandTester->getDisplay()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Dumper; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this dumper really "core" inside this component ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the role hierarchy belongs to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i was seeing this class tooling more like a "debug or dev" class, that is what I wanted to comment (perhaps poorly ^^) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, it's the only "debug" class of security component for now, and it is quite tied to roles and role hierarchy. IMO, it can stay here for now. |
||
|
||
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; | ||
|
||
/** | ||
* MermaidDumper dumps a Mermaid flowchart describing role hierarchy. | ||
* | ||
* @author Damien Fernandes <your.email@example.com> | ||
*/ | ||
class MermaidDumper | ||
{ | ||
public const DIRECTION_TOP_TO_BOTTOM = 'TB'; | ||
public const DIRECTION_TOP_DOWN = 'TD'; | ||
public const DIRECTION_BOTTOM_TO_TOP = 'BT'; | ||
public const DIRECTION_RIGHT_TO_LEFT = 'RL'; | ||
public const DIRECTION_LEFT_TO_RIGHT = 'LR'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those look like a perfect use case for a backed enum instead of using string constants in that class (and then redoing lots of manual validation) |
||
|
||
private const VALID_DIRECTIONS = [ | ||
self::DIRECTION_TOP_TO_BOTTOM, | ||
self::DIRECTION_TOP_DOWN, | ||
self::DIRECTION_BOTTOM_TO_TOP, | ||
self::DIRECTION_RIGHT_TO_LEFT, | ||
self::DIRECTION_LEFT_TO_RIGHT, | ||
]; | ||
|
||
public function __construct( | ||
private readonly string $direction = self::DIRECTION_TOP_TO_BOTTOM, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moving |
||
) { | ||
if (!in_array($direction, self::VALID_DIRECTIONS, true)) { | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
throw new \InvalidArgumentException(sprintf( | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'Direction "%s" is not valid, valid directions are: "%s".', | ||
$direction, | ||
implode('", "', self::VALID_DIRECTIONS) | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* Dumps the role hierarchy as a Mermaid flowchart. | ||
* | ||
* @param RoleHierarchyInterface $roleHierarchy The role hierarchy to dump | ||
*/ | ||
public function dump(RoleHierarchyInterface $roleHierarchy): string | ||
{ | ||
$hierarchy = $this->extractHierarchy($roleHierarchy); | ||
|
||
if ([] === $hierarchy) { | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return "graph {$this->direction}\n classDef default fill:#e1f5fe;"; | ||
} | ||
|
||
$output = ["graph {$this->direction}"]; | ||
$allRoles = $this->getAllRoles($hierarchy); | ||
|
||
// Add role nodes | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach ($allRoles as $role) { | ||
$output[] = $this->formatRoleNode($role); | ||
} | ||
|
||
// Add hierarchy relationships (parent -> child) | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
foreach ($hierarchy as $parentRole => $childRoles) { | ||
foreach ($childRoles as $childRole) { | ||
$output[] = " {$this->escapeRoleName($parentRole)} --> {$this->escapeRoleName($childRole)}"; | ||
} | ||
} | ||
|
||
return implode("\n", array_filter($output)); | ||
} | ||
|
||
/** | ||
* Extracts the role hierarchy from the RoleHierarchyInterface. | ||
*/ | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private function extractHierarchy(RoleHierarchyInterface $roleHierarchy): array | ||
{ | ||
$reflection = new \ReflectionClass($roleHierarchy); | ||
|
||
if ($reflection->hasProperty('hierarchy')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the RoleHierarchyInterface is not a child of RoleHierarchy, it could have a And if the RoleHierarchyInterface is a child of RoleHierarchy, this check will return You should check |
||
$hierarchyProperty = $reflection->getProperty('hierarchy'); | ||
return $hierarchyProperty->getValue($roleHierarchy); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Gets all unique roles from the hierarchy. | ||
*/ | ||
private function getAllRoles(array $hierarchy): array | ||
{ | ||
$allRoles = []; | ||
|
||
foreach ($hierarchy as $parentRole => $childRoles) { | ||
$allRoles[] = $parentRole; | ||
foreach ($childRoles as $childRole) { | ||
$allRoles[] = $childRole; | ||
} | ||
} | ||
|
||
return array_unique($allRoles); | ||
} | ||
|
||
/** | ||
* Formats a role node for Mermaid. | ||
*/ | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private function formatRoleNode(string $role): string | ||
{ | ||
$escapedRole = $this->escapeRoleName($role); | ||
return " {$escapedRole}"; | ||
} | ||
|
||
/** | ||
* Escapes role name for use as Mermaid node ID. | ||
*/ | ||
private function escapeRoleName(string $role): string | ||
{ | ||
// Replace any non-alphanumeric characters with underscores | ||
damienfern marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return preg_replace('/[^a-zA-Z0-9_]/', '_', $role); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.