GenerateObjectAclCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Component\Security\Acl\Domain\UserSecurityIdentity;
  12. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Output\Output;
  18. use Sonata\AdminBundle\Admin\AdminInterface;
  19. use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
  20. class GenerateObjectAclCommand extends ContainerAwareCommand
  21. {
  22. /**
  23. * @var string
  24. */
  25. protected $userEntityClass = '';
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function configure()
  30. {
  31. $this
  32. ->setName('sonata:admin:generate-object-acl')
  33. ->setDescription('Install ACL for the objects of the Admin Classes.')
  34. ->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.')
  35. ->addOption('user_entity', null, InputOption::VALUE_OPTIONAL, 'Shortcut notation like <comment>AcmeDemoBundle:User</comment>. If not set, it will be asked the first time an object owner is set.')
  36. ->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and if and what object owner to set.')
  37. ;
  38. }
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. $dialog = $this->getHelperSet()->get('dialog');
  45. $output->writeln('Welcome to the AdminBundle object ACL generator');
  46. $output->writeln(array(
  47. '',
  48. 'This command helps you generate ACL entities for the objects handled by the AdminBundle.',
  49. '',
  50. 'If the step option is used, you will be asked for each Admin to generate the object ACL entities.',
  51. 'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
  52. ''
  53. ));
  54. if ($input->getOption('user_entity')) {
  55. try {
  56. $this->getUserEntityClass($input, $output);
  57. } catch (\Exception $e) {
  58. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  59. return;
  60. }
  61. }
  62. foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
  63. try {
  64. $admin = $this->getContainer()->get($id);
  65. } catch (\Exception $e) {
  66. $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
  67. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  68. continue;
  69. }
  70. if ($input->getOption('step') && !$dialog->askConfirmation($output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), false)) {
  71. continue;
  72. }
  73. $securityIdentity = null;
  74. if ($input->getOption('step') && $dialog->askConfirmation($output,"<question>Set an object owner?</question>\n", false)) {
  75. $username = $dialog->askAndValidate($output, 'Please enter the username: ', 'Sonata\AdminBundle\Command\Validators::validateUsername');
  76. $securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output));
  77. }
  78. if (!$input->getOption('step') && $input->getOption('object_owner')) {
  79. $securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output));
  80. }
  81. $manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType());
  82. if (!$this->getContainer()->has($manipulatorId)) {
  83. $output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>');
  84. continue;
  85. }
  86. $manipulator = $this->getContainer()->get($manipulatorId);
  87. if (!$manipulator instanceof ObjectAclManipulatorInterface) {
  88. $output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', get_class($manipulator)));
  89. continue;
  90. }
  91. $manipulator->batchConfigureAcls($output, $admin, $securityIdentity);
  92. }
  93. }
  94. /**
  95. * @param \Symfony\Component\Console\Input\InputInterface $input
  96. * @param \Symfony\Component\Console\Output\OutputInterface $output
  97. *
  98. * @return string
  99. */
  100. protected function getUserEntityClass(InputInterface $input, OutputInterface $output)
  101. {
  102. if ($this->userEntityClass === '') {
  103. if ($input->getOption('user_entity')) {
  104. list($userBundle, $userEntity) = Validators::validateEntityName($input->getOption('user_entity'));
  105. $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
  106. } else {
  107. list($userBundle, $userEntity) = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
  108. // Entity exists?
  109. $this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
  110. }
  111. }
  112. return $this->userEntityClass;
  113. }
  114. }