GenerateObjectAclCommand.php 5.6 KB

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