DumpActionRolesCommand.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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('');
  26. $this->addOption('format', null, InputOption::VALUE_OPTIONAL, 'define the output format (default: yaml)', 'yaml');
  27. $this->addOption('prefix', null, InputOption::VALUE_OPTIONAL, 'define the admin route prefix (default: /admin)', '/admin');
  28. }
  29. public function execute(InputInterface $input, OutputInterface $output)
  30. {
  31. $infos = array();
  32. foreach ($this->getAdminRoutesCollection($input->getOption('prefix'))->all() as $route) {
  33. $compiledRoute = $route->compile();
  34. $regex = str_replace(array("\n", ' '), '', $compiledRoute->getRegex());
  35. if ($pos = strpos($regex, '/$')) {
  36. $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
  37. }
  38. $defaults = $route->getDefaults();
  39. $controllerInfos = explode(':', $defaults['_controller']);
  40. $group = strtoupper(sprintf('ROLE_%s', str_replace(array('.','|'), '_', strtoupper($defaults['_sonata_admin']))));
  41. if (!isset($infos[$group])) {
  42. $infos[$group] = array();
  43. }
  44. $name = strtoupper(sprintf('ROLE_%s_%s',
  45. str_replace(array('.','|'), '_', strtoupper($defaults['_sonata_admin'])),
  46. $controllerInfos[2]
  47. ));
  48. $infos[$group][] = array(
  49. 'path' => substr($regex, 1, -2),
  50. 'roles' => $name
  51. );
  52. }
  53. $this->dumpYaml($output, $infos);
  54. }
  55. public function dumpYaml(OutputInterface $output, array $infos)
  56. {
  57. $output->writeln('# ----');
  58. $output->writeln('# PLEASE DO NOT EDIT THIS FILE');
  59. $output->writeln('# ----');
  60. $output->writeln('sonata_admin:');
  61. $output->writeln(' access_control:');
  62. foreach ($infos as $groups) {
  63. foreach ($groups as $group) {
  64. $output->writeln(sprintf(' - { path: %s, roles: [%s], methods: null }', $group['path'], $group['roles']));
  65. }
  66. }
  67. $output->writeln('');
  68. $output->writeln(' role_hierarchy:');
  69. $superAdmin = array();
  70. foreach ($infos as $groupName => $groups) {
  71. $roles = array();
  72. foreach ($groups as $group) {
  73. $roles[] = $group['roles'];
  74. }
  75. $output->writeln(sprintf(' %s: [%s] ', $groupName, implode(', ', $roles)));
  76. $superAdmin[] = $groupName;
  77. }
  78. $output->writeln(sprintf(' ROLE_SONATA_ADMIN_ROOT: [%s] ', implode(', ', $superAdmin)));
  79. }
  80. public function getAdminRoutesCollection($prefix)
  81. {
  82. $pool = $this->container->get('sonata.admin.pool');
  83. $collection = new RouteCollection;
  84. foreach ($pool->getAdminServiceIds() as $id) {
  85. $admin = $pool->getInstance($id);
  86. foreach ($admin->getRoutes()->getElements() as $code => $route) {
  87. $collection->add($route->getDefault('_sonata_name'), $route);
  88. }
  89. $reflection = new \ReflectionObject($admin);
  90. $collection->addResource(new FileResource($reflection->getFileName()));
  91. }
  92. $collection->addPrefix($prefix);
  93. return $collection;
  94. }
  95. }