DumpActionRolesCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Command;
  11. use Symfony\Bundle\FrameworkBundle\Command\Command;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. use Symfony\Component\Console\Output\Output;
  17. use Symfony\Component\Routing\RouteCollection;
  18. use Symfony\Component\Routing\Route;
  19. use Symfony\Component\Config\Resource\FileResource;
  20. class DumpActionRolesCommand extends Command
  21. {
  22. public function configure()
  23. {
  24. $this->setName('sonata:admin:dump-action-roles');
  25. $this->setDescription('Dumps a set of access control rules for the classes');
  26. $this->addOption('format', null, InputOption::VALUE_OPTIONAL, 'define the output format', 'yaml');
  27. $this->addOption('prefix', null, InputOption::VALUE_OPTIONAL, 'define the admin route prefix', '/admin');
  28. $this->setHelp(<<<EOF
  29. Dumps a role hierachy and a set of access control rules using a different role
  30. for each admin actions.
  31. EOF
  32. );
  33. }
  34. public function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $infos = array();
  37. foreach ($this->getAdminRoutesCollection($input->getOption('prefix'))->all() as $route) {
  38. $compiledRoute = $route->compile();
  39. $regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
  40. if ($pos = strpos($regex, '/$')) {
  41. $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
  42. }
  43. $defaults = $route->getDefaults();
  44. $controllerInfos = explode(':', $defaults['_controller']);
  45. $group = strtoupper(sprintf('ROLE_%s', str_replace(array('.','|'), '_', strtoupper($defaults['_sonata_admin']))));
  46. if (!isset($infos[$group])) {
  47. $infos[$group] = array();
  48. }
  49. $name = strtoupper(sprintf('ROLE_%s_%s',
  50. str_replace(array('.','|'), '_', strtoupper($defaults['_sonata_admin'])),
  51. $controllerInfos[2]
  52. ));
  53. $infos[$group][] = array(
  54. 'path' => substr($regex, 1, -2),
  55. 'roles' => $name
  56. );
  57. }
  58. $this->dumpYaml($output, $infos);
  59. }
  60. public function dumpYaml(OutputInterface $output, array $infos)
  61. {
  62. $output->writeln('security:');
  63. $output->writeln(' access_control:');
  64. foreach ($infos as $groups) {
  65. foreach ($groups as $group) {
  66. $output->writeln(sprintf(' - { path: %s, roles: [%s], methods: null }', $group['path'], $group['roles']));
  67. }
  68. }
  69. $output->writeln('');
  70. $output->writeln(' role_hierarchy:');
  71. $superAdmin = array();
  72. foreach ($infos as $groupName => $groups) {
  73. $roles = array();
  74. foreach ($groups as $group) {
  75. $roles[] = $group['roles'];
  76. }
  77. $output->writeln(sprintf(' %s: [%s] ', $groupName, implode(', ', $roles)));
  78. $superAdmin[] = $groupName;
  79. }
  80. $output->writeln(sprintf(' ROLE_SONATA_ADMIN_ROOT: [%s] ', implode(', ', $superAdmin)));
  81. }
  82. public function getAdminRoutesCollection($prefix)
  83. {
  84. $pool = $this->container->get('sonata.admin.pool');
  85. $collection = new RouteCollection;
  86. foreach ($pool->getAdminServiceIds() as $id) {
  87. $admin = $pool->getInstance($id);
  88. foreach ($admin->getRoutes()->getElements() as $code => $route) {
  89. $collection->add($route->getDefault('_sonata_name'), $route);
  90. }
  91. $reflection = new \ReflectionObject($admin);
  92. $collection->addResource(new FileResource($reflection->getFileName()));
  93. }
  94. $collection->addPrefix($prefix);
  95. return $collection;
  96. }
  97. }