SetupAclCommand.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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\ContainerAwareCommand;
  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\Security\Acl\Model\AclInterface;
  18. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  19. use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
  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\Security\Handler\AclSecurityHandler;
  24. class SetupAclCommand extends ContainerAwareCommand
  25. {
  26. public function configure()
  27. {
  28. $this->setName('sonata:admin:setup-acl');
  29. $this->setDescription('Install ACL for Admin Classes');
  30. }
  31. public function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. $aclProvider = $this->getContainer()->get('security.acl.provider');
  34. $output->writeln('Starting ACL AdminBundle configuration');
  35. $builder = new MaskBuilder();
  36. foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
  37. $output->writeln(sprintf(' > install ACL for %s', $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. try {
  52. $acl = $aclProvider->findAcl($objectIdentity);
  53. } catch(AclNotFoundException $e) {
  54. $acl = $aclProvider->createAcl($objectIdentity);
  55. }
  56. $this->configureACL($output, $acl, $builder, $securityHandler->buildSecurityInformation($admin));
  57. $aclProvider->updateAcl($acl);
  58. }
  59. }
  60. public function configureACL(OutputInterface $output, AclInterface $acl, MaskBuilder $builder, array $aclInformations = array())
  61. {
  62. foreach ($aclInformations as $name => $masks) {
  63. foreach ($masks as $mask) {
  64. $builder->add($mask);
  65. }
  66. $acl->insertClassAce(new RoleSecurityIdentity($name), $builder->get());
  67. $output->writeln(sprintf(' - add role: %s, ACL: %s', $name, json_encode($masks)));
  68. $builder->reset();
  69. }
  70. }
  71. }