|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\SecurityBundle\Command; |
| 13 | + |
| 14 | +use Symfony\Bundle\SecurityBundle\Debug\DebugRoleHierarchy; |
| 15 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 16 | +use Symfony\Component\Console\Command\Command; |
| 17 | +use Symfony\Component\Console\Input\InputArgument; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Input\InputOption; |
| 20 | +use Symfony\Component\Console\Output\OutputInterface; |
| 21 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 22 | +use Symfony\Component\Security\Core\Role\RoleHierarchyInterface; |
| 23 | + |
| 24 | +#[AsCommand(name: 'debug:roles', description: 'Debug the role hierarchy configuration.')] |
| 25 | +final class DebugRolesCommand extends Command |
| 26 | +{ |
| 27 | + public function __construct(private readonly RoleHierarchyInterface $roleHierarchy) |
| 28 | + { |
| 29 | + parent::__construct(); |
| 30 | + } |
| 31 | + |
| 32 | + protected function configure(): void |
| 33 | + { |
| 34 | + $this->setHelp(<<<EOF |
| 35 | +This <info>%command.name%</info> command display the current role hierarchy: |
| 36 | +
|
| 37 | + <info>php %command.full_name%</info> |
| 38 | +
|
| 39 | +You can pass one or multiple role names to display the effective roles: |
| 40 | +
|
| 41 | + <info>php %command.full_name% ROLE_USER</info> |
| 42 | +
|
| 43 | +To get a tree view of the inheritance, use the <info>tree</info> option: |
| 44 | +
|
| 45 | + <info>php %command.full_name% --tree</info> |
| 46 | + <info>php %command.full_name% ROLE_USER --tree</info> |
| 47 | +
|
| 48 | +<comment>Note:</comment> With a custom implementation for <info>security.role_hierarchy</info>, the <info>--tree</info> option is ignored and the <info>roles</info> argument is required. |
| 49 | +
|
| 50 | +EOF |
| 51 | + ) |
| 52 | + ->setDefinition([ |
| 53 | + new InputArgument('roles', ($this->isBuiltInRoleHierarchy() ? InputArgument::OPTIONAL : InputArgument::REQUIRED) | InputArgument::IS_ARRAY, 'The role(s) to resolve'), |
| 54 | + new InputOption('tree', 't', InputOption::VALUE_NONE, 'Show the hierarchy in a tree view'), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + protected function initialize(InputInterface $input, OutputInterface $output): void |
| 59 | + { |
| 60 | + if (!$this->isBuiltInRoleHierarchy()) { |
| 61 | + $io = new SymfonyStyle($input, $output); |
| 62 | + |
| 63 | + if ($input->getOption('tree')) { |
| 64 | + $io->warning('Ignoring option "--tree" because of a custom role hierarchy implementation.'); |
| 65 | + $input->setOption('tree', null); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + protected function interact(InputInterface $input, OutputInterface $output): void |
| 71 | + { |
| 72 | + if (!$this->isBuiltInRoleHierarchy() && empty($input->getArgument('roles'))) { |
| 73 | + $io = new SymfonyStyle($input, $output); |
| 74 | + |
| 75 | + $roles[] = $io->ask('Enter a role to debug', validator: function (?string $role) { |
| 76 | + $role = trim($role); |
| 77 | + if (empty($role)) { |
| 78 | + throw new \RuntimeException('You must enter a non empty role name.'); |
| 79 | + } |
| 80 | + |
| 81 | + return $role; |
| 82 | + }); |
| 83 | + while ($role = trim($io->ask('Add another role? (press enter to skip)') ?? '')) { |
| 84 | + $roles[] = $role; |
| 85 | + } |
| 86 | + |
| 87 | + $input->setArgument('roles', $roles); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 92 | + { |
| 93 | + $io = new SymfonyStyle($input, $output); |
| 94 | + |
| 95 | + $roles = $input->getArgument('roles'); |
| 96 | + |
| 97 | + if (empty($roles)) { |
| 98 | + // Full configuration output |
| 99 | + $io->title('Current role hierarchy configuration:'); |
| 100 | + |
| 101 | + if ($input->getOption('tree')) { |
| 102 | + $this->outputTree($io, $this->getBuiltInDebugHierarchy()->getHierarchy()); |
| 103 | + } else { |
| 104 | + $this->outputMap($io, $this->getBuiltInDebugHierarchy()->getMap()); |
| 105 | + } |
| 106 | + |
| 107 | + $io->comment('To show reachable roles for a given role, re-run this command with role names. (e.g. <comment>debug:roles ROLE_USER</comment>)'); |
| 108 | + |
| 109 | + return self::SUCCESS; |
| 110 | + } |
| 111 | + |
| 112 | + // Matching roles output |
| 113 | + $io->title(sprintf('Effective roles for %s:', implode(', ', array_map(fn ($v) => sprintf('<info>%s</info>', $v), $roles)))); |
| 114 | + |
| 115 | + if ($input->getOption('tree')) { |
| 116 | + $this->outputTree($io, $this->getBuiltInDebugHierarchy()->getHierarchy($roles)); |
| 117 | + } else { |
| 118 | + $io->listing($this->roleHierarchy->getReachableRoleNames($roles)); |
| 119 | + } |
| 120 | + |
| 121 | + return self::SUCCESS; |
| 122 | + } |
| 123 | + |
| 124 | + private function outputMap(OutputInterface $output, array $map): void |
| 125 | + { |
| 126 | + foreach ($map as $main => $roles) { |
| 127 | + if ($this->getBuiltInDebugHierarchy()->isPlaceholder($main)) { |
| 128 | + $main = $this->stylePlaceholder($main); |
| 129 | + } |
| 130 | + |
| 131 | + $output->writeln(sprintf('%s:', $main)); |
| 132 | + foreach ($roles as $r) { |
| 133 | + $output->writeln(sprintf(' - %s', $r)); |
| 134 | + } |
| 135 | + $output->writeln(''); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private function outputTree(OutputInterface $output, array $tree): void |
| 140 | + { |
| 141 | + foreach ($tree as $role => $hierarchy) { |
| 142 | + $output->writeln($this->generateTree($role, $hierarchy)); |
| 143 | + $output->writeln(''); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Generates a tree representation, line by line, in the tree unix style. |
| 149 | + * |
| 150 | + * Example output: |
| 151 | + * |
| 152 | + * ROLE_A |
| 153 | + * └── ROLE_B |
| 154 | + * |
| 155 | + * ROLE_C |
| 156 | + * ├── ROLE_A |
| 157 | + * │ └── ROLE_B |
| 158 | + * └── ROLE_D |
| 159 | + */ |
| 160 | + private function generateTree(string $name, array $tree, string $indent = '', bool $last = true, bool $root = true): \Generator |
| 161 | + { |
| 162 | + if ($this->getBuiltInDebugHierarchy()->isPlaceholder($name)) { |
| 163 | + $name = $this->stylePlaceholder($name); |
| 164 | + } |
| 165 | + |
| 166 | + if ($root) { |
| 167 | + // Yield root node as it is |
| 168 | + yield $name; |
| 169 | + } else { |
| 170 | + // Generate line in the tree: |
| 171 | + // Line: [indent]├── [name] |
| 172 | + // Last line: [indent]└── [name] |
| 173 | + yield sprintf('%s%s%s %s', $indent, $last ? "\u{2514}" : "\u{251c}", str_repeat("\u{2500}", 2), $name); |
| 174 | + |
| 175 | + // Update indent for next nested: |
| 176 | + // Append "| " for a nested tree |
| 177 | + // Append " " for last nested tree |
| 178 | + $indent .= ($last ? ' ' : "\u{2502}").str_repeat(' ', 3); |
| 179 | + } |
| 180 | + |
| 181 | + $i = 0; |
| 182 | + $count = \count($tree); |
| 183 | + foreach ($tree as $key => $value) { |
| 184 | + yield from $this->generateTree($key, $value, $indent, $i === $count - 1, false); |
| 185 | + ++$i; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private function stylePlaceholder(string $role): string |
| 190 | + { |
| 191 | + return sprintf('<info>%s</info>', $role); |
| 192 | + } |
| 193 | + |
| 194 | + private function isBuiltInRoleHierarchy(): bool |
| 195 | + { |
| 196 | + return $this->roleHierarchy instanceof DebugRoleHierarchy; |
| 197 | + } |
| 198 | + |
| 199 | + private function getBuiltInDebugHierarchy(): DebugRoleHierarchy |
| 200 | + { |
| 201 | + if (!$this->roleHierarchy instanceof DebugRoleHierarchy) { |
| 202 | + throw new \LogicException('Cannot use the built-in debug hierarchy with a custom implementation.'); |
| 203 | + } |
| 204 | + |
| 205 | + return $this->roleHierarchy; |
| 206 | + } |
| 207 | +} |
0 commit comments