SetupAclCommand.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. // create admin ACL, fe.
  57. // Comment admin ACL
  58. $this->configureACL($output, $acl, $builder, $securityHandler->buildSecurityInformation($admin));
  59. $aclProvider->updateAcl($acl);
  60. }
  61. }
  62. public function configureACL(OutputInterface $output, AclInterface $acl, MaskBuilder $builder, array $aclInformations = array())
  63. {
  64. foreach ($aclInformations as $role => $permissions) {
  65. foreach ($permissions as $permission) {
  66. $builder->add($permission);
  67. }
  68. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  69. $output->writeln(sprintf(' - add role: %s, permissions: %s', $role, json_encode($permissions)));
  70. $builder->reset();
  71. }
  72. }
  73. }