-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/Symfony/Bundle/SecurityBundle/Command/SecurityRoleHierarchyDumpCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| <?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\Input\InputInterface; | ||
| use Symfony\Component\Console\Input\InputOption; | ||
| use Symfony\Component\Console\Output\OutputInterface; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\Security\Core\Dumper\MermaidDirectionEnum; | ||
| use Symfony\Component\Security\Core\Dumper\MermaidDumper; | ||
| use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; | ||
|
|
||
| /** | ||
| * Command to dump the role hierarchy as a Mermaid flowchart. | ||
| * | ||
| * @author Damien Fernandes <damien.fernandes24@gmail.com> | ||
| */ | ||
| #[AsCommand(name: 'debug:security:role-hierarchy', description: 'Dump the role hierarchy as a Mermaid flowchart')] | ||
| class SecurityRoleHierarchyDumpCommand extends Command | ||
| { | ||
| public function __construct( | ||
| private readonly RoleHierarchyInterface $roleHierarchy, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| protected function configure(): void | ||
| { | ||
| $this | ||
| ->setDefinition([ | ||
| new InputOption( | ||
| 'direction', | ||
| 'd', | ||
| InputOption::VALUE_REQUIRED, | ||
| 'The direction of the flowchart ['.implode('|', $this->getAvailableDirections()).']', | ||
| MermaidDirectionEnum::TOP_TO_BOTTOM->value, | ||
| $this->getAvailableDirections() | ||
| ), | ||
| ]) | ||
| ->setHelp(<<<'USAGE' | ||
| The <info>%command.name%</info> command dumps the role hierarchy in Mermaid format. | ||
|
|
||
| <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 | ||
| { | ||
| $io = new SymfonyStyle($input, $output); | ||
|
|
||
| if (null === $this->roleHierarchy) { | ||
| $io->getErrorStyle()->writeln('<comment>No role hierarchy is configured.</comment>'); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| $direction = $input->getOption('direction'); | ||
|
|
||
| if (!MermaidDirectionEnum::tryFrom($direction)) { | ||
| $io->getErrorStyle()->writeln(\sprintf('<error>Invalid direction, available options are "%s"</error>', implode('"', $this->getAvailableDirections()))); | ||
|
|
||
| return Command::FAILURE; | ||
| } | ||
|
|
||
| $dumper = new MermaidDumper(); | ||
| $mermaidOutput = $dumper->dump($this->roleHierarchy, MermaidDirectionEnum::from($direction)); | ||
|
|
||
| $output->writeln($mermaidOutput, OutputInterface::OUTPUT_RAW); | ||
|
|
||
| return Command::SUCCESS; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| private function getAvailableDirections(): array | ||
| { | ||
| return array_map(fn ($case) => $case->value, MermaidDirectionEnum::cases()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Symfony/Bundle/SecurityBundle/Resources/config/security_role_hierarchy_dump_command.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?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\DependencyInjection\Loader\Configurator; | ||
|
|
||
| use Symfony\Bundle\SecurityBundle\Command\SecurityRoleHierarchyDumpCommand; | ||
|
|
||
| return static function (ContainerConfigurator $container): void { | ||
| $container->services() | ||
| ->set('security.command.role_hierarchy_dump', SecurityRoleHierarchyDumpCommand::class) | ||
| ->args([ | ||
| service('security.role_hierarchy'), | ||
| ]) | ||
| ->tag('console.command') | ||
| ; | ||
| }; |
83 changes: 83 additions & 0 deletions
83
src/Symfony/Bundle/SecurityBundle/Tests/Command/SecurityRoleHierarchyDumpCommandTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?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\Command\Command; | ||
| use Symfony\Component\Console\Tester\CommandTester; | ||
| use Symfony\Component\Security\Core\Role\RoleHierarchy; | ||
|
|
||
| class SecurityRoleHierarchyDumpCommandTest extends TestCase | ||
| { | ||
| 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->assertSame(Command::SUCCESS, $exitCode); | ||
| $output = $commandTester->getDisplay(); | ||
| $expectedOutput = <<<EXPECTED | ||
| graph TB | ||
| ROLE_ADMIN | ||
| ROLE_USER | ||
| ROLE_SUPER_ADMIN | ||
| ROLE_ADMIN --> ROLE_USER | ||
| ROLE_SUPER_ADMIN --> ROLE_ADMIN | ||
| ROLE_SUPER_ADMIN --> ROLE_USER | ||
|
|
||
| EXPECTED; | ||
|
|
||
| $this->assertSame($expectedOutput, $output); | ||
| } | ||
|
|
||
| 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->assertSame(Command::SUCCESS, $exitCode); | ||
| $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->assertSame(Command::FAILURE, $exitCode); | ||
| $this->assertStringContainsString('Invalid direction', $commandTester->getDisplay()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Security/Core/Dumper/MermaidDirectionEnum.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?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; | ||
|
|
||
| enum MermaidDirectionEnum: string | ||
| { | ||
| case TOP_TO_BOTTOM = 'TB'; | ||
| case TOP_DOWN = 'TD'; | ||
| case BOTTOM_TO_TOP = 'BT'; | ||
| case RIGHT_TO_LEFT = 'RL'; | ||
| case LEFT_TO_RIGHT = 'LR'; | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/Symfony/Component/Security/Core/Dumper/MermaidDumper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| <?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; | ||
|
|
||
| use Symfony\Component\Security\Core\Role\RoleHierarchy; | ||
| use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; | ||
|
|
||
| /** | ||
| * MermaidDumper dumps a Mermaid flowchart describing role hierarchy. | ||
| * | ||
| * @author Damien Fernandes <damien.fernandes24@gmail.com> | ||
| */ | ||
| class MermaidDumper | ||
| { | ||
| /** | ||
| * Dumps the role hierarchy as a Mermaid flowchart. | ||
| * | ||
| * @param RoleHierarchyInterface $roleHierarchy The role hierarchy to dump | ||
| * @param MermaidDirectionEnum $direction The direction of the flowchart | ||
| */ | ||
| public function dump(RoleHierarchyInterface $roleHierarchy, MermaidDirectionEnum $direction = MermaidDirectionEnum::TOP_TO_BOTTOM): string | ||
| { | ||
| $hierarchy = $this->extractHierarchy($roleHierarchy); | ||
|
|
||
| if (!$hierarchy) { | ||
| return "graph {$direction->value}\n classDef default fill:#e1f5fe;"; | ||
| } | ||
|
|
||
| $output = ["graph {$direction->value}"]; | ||
| $allRoles = $this->getAllRoles($hierarchy); | ||
|
|
||
| foreach ($allRoles as $role) { | ||
| $output[] = $this->formatRoleNode($role); | ||
| } | ||
|
|
||
| foreach ($hierarchy as $parentRole => $childRoles) { | ||
| foreach ($childRoles as $childRole) { | ||
| $output[] = " {$this->normalizeRoleName($parentRole)} --> {$this->normalizeRoleName($childRole)}"; | ||
| } | ||
| } | ||
|
|
||
| return implode("\n", array_filter($output)); | ||
| } | ||
|
|
||
| private function extractHierarchy(RoleHierarchyInterface $roleHierarchy): array | ||
| { | ||
| if (!$roleHierarchy instanceof RoleHierarchy) { | ||
| return []; | ||
| } | ||
|
|
||
| $reflection = new \ReflectionClass(RoleHierarchy::class); | ||
|
|
||
| $hierarchyProperty = $reflection->getProperty('hierarchy'); | ||
|
|
||
| return $hierarchyProperty->getValue($roleHierarchy); | ||
| } | ||
|
|
||
| private function getAllRoles(array $hierarchy): array | ||
| { | ||
| $allRoles = []; | ||
|
|
||
| foreach ($hierarchy as $parentRole => $childRoles) { | ||
| $allRoles[] = $parentRole; | ||
| foreach ($childRoles as $childRole) { | ||
| $allRoles[] = $childRole; | ||
| } | ||
| } | ||
|
|
||
| return array_unique($allRoles); | ||
| } | ||
|
|
||
| private function formatRoleNode(string $role): string | ||
| { | ||
| $escapedRole = $this->normalizeRoleName($role); | ||
|
|
||
| return " {$escapedRole}"; | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes the role name by replacing non-alphanumeric characters with underscores. | ||
| */ | ||
| private function normalizeRoleName(string $role): ?string | ||
| { | ||
| return preg_replace('/[^a-zA-Z0-9_]/', '_', $role); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.