SetupAclCommand.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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\Command;
  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. class SetupAclCommand extends Command
  24. {
  25. public function configure()
  26. {
  27. $this->setName('sonata:admin:setup-acl');
  28. $this->setDescription('Install ACL for Admin Classes');
  29. }
  30. public function execute(InputInterface $input, OutputInterface $output)
  31. {
  32. $aclProvider = $this->container->get('security.acl.provider');
  33. $output->writeln('Starting ACL AdminBundle configuration');
  34. $builder = new MaskBuilder();
  35. foreach ($this->container->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
  36. $output->writeln(sprintf(' > install ACL for %s', $id));
  37. try {
  38. $admin = $this->container->get($id);
  39. } catch (\Exception $e) {
  40. $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
  41. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  42. continue;
  43. }
  44. $objectIdentity = ObjectIdentity::fromDomainObject($admin);
  45. try {
  46. $acl = $aclProvider->findAcl($objectIdentity);
  47. } catch(AclNotFoundException $e) {
  48. $acl = $aclProvider->createAcl($objectIdentity);
  49. }
  50. $this->configureACL($output, $acl, $builder, $admin->getAclInformation());
  51. $aclProvider->updateAcl($acl);
  52. }
  53. }
  54. public function configureACL(OutputInterface $output, AclInterface $acl, MaskBuilder $builder, array $aclInformations = array())
  55. {
  56. foreach ($aclInformations as $name => $masks) {
  57. foreach ($masks as $mask) {
  58. $builder->add($mask);
  59. }
  60. $acl->insertClassAce(new RoleSecurityIdentity($name), $builder->get());
  61. $output->writeln(sprintf(' - add role: %s, ACL: %s', $name, json_encode($masks)));
  62. $builder->reset();
  63. }
  64. }
  65. }