SetupAclCommand.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\Model\MutableAclProviderInterface;
  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 Symfony\Component\Security\Acl\Model\AclInterface;
  19. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  20. use Symfony\Component\Security\Acl\Exception\AclAlreadyExistsException;
  21. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  22. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  23. use Sonata\AdminBundle\Admin\AdminInterface;
  24. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  25. use Sonata\AdminBundle\Security\Handler\AclSecurityHandler;
  26. class SetupAclCommand extends ContainerAwareCommand
  27. {
  28. public function configure()
  29. {
  30. $this->setName('sonata:admin:setup-acl');
  31. $this->setDescription('Install ACL for Admin Classes');
  32. }
  33. public function execute(InputInterface $input, OutputInterface $output)
  34. {
  35. $aclProvider = $this->getContainer()->get('security.acl.provider');
  36. $output->writeln('Starting ACL AdminBundle configuration');
  37. foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
  38. try {
  39. $admin = $this->getContainer()->get($id);
  40. } catch (\Exception $e) {
  41. $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
  42. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  43. continue;
  44. }
  45. $securityHandler = $admin->getSecurityHandler();
  46. if (!$securityHandler instanceof AclSecurityHandler) {
  47. $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
  48. continue;
  49. }
  50. $objectIdentity = ObjectIdentity::fromDomainObject($admin);
  51. $newAcl = false;
  52. try {
  53. $acl = $aclProvider->findAcl($objectIdentity);
  54. } catch(AclNotFoundException $e) {
  55. $acl = $aclProvider->createAcl($objectIdentity);
  56. $newAcl = true;
  57. }
  58. // create admin ACL
  59. $output->writeln(sprintf(' > install ACL for %s', $id));
  60. $configResult = $securityHandler->addAdminClassAces($acl, $securityHandler->buildSecurityInformation($admin), $output);
  61. if ($configResult) {
  62. $aclProvider->updateAcl($acl);
  63. } elseif ($aclProvider instanceof MutableAclProviderInterface) {
  64. $output->writeln(sprintf(' - %s , no roles and permissions found', ($newAcl ? 'skip' : 'removed')));
  65. $aclProvider->deleteAcl($objectIdentity);
  66. }
  67. }
  68. }
  69. }