GenerateObjectAclCommand.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\DoctrineORMAdminBundle\Command;
  11. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  12. use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
  13. use Symfony\Component\Security\Acl\Model\AclInterface;
  14. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  15. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  16. use Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException;
  17. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  18. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  19. use Symfony\Component\Console\Input\InputArgument;
  20. use Symfony\Component\Console\Input\InputOption;
  21. use Symfony\Component\Console\Input\InputInterface;
  22. use Symfony\Component\Console\Output\OutputInterface;
  23. use Symfony\Component\Console\Output\Output;
  24. use Sonata\AdminBundle\Admin\AdminInterface;
  25. use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
  26. use Sonata\AdminBundle\Security\Handler\AclSecurityHandler;
  27. use Sonata\AdminBundle\Exception\ModelManagerException;
  28. class GenerateObjectAclCommand extends ContainerAwareCommand
  29. {
  30. public function configure()
  31. {
  32. $this->setName('sonata:admin:generate-object-acl');
  33. $this->setDescription('Install ACL for the objects of the Admin Classes');
  34. }
  35. public function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $dialog = $this->getHelperSet()->get('dialog');
  38. $output->writeln('Welcome to the AdminBundle object ACL generator');
  39. $output->writeln(array(
  40. '',
  41. 'This command helps you generate ACL entities for the objects handled by the AdminBundle.',
  42. '',
  43. 'Foreach Admin, you will be asked to generate the object ACL entities',
  44. 'You must use the shortcut notation like <comment>AcmeBlogBundle:Post</comment> if you want to set an object owner.',
  45. ''
  46. ));
  47. $aclProvider = $this->getContainer()->get('security.acl.provider');
  48. foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
  49. try {
  50. $admin = $this->getContainer()->get($id);
  51. } catch (\Exception $e) {
  52. $output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
  53. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  54. continue;
  55. }
  56. if (!$dialog->askConfirmation($output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), false)) {
  57. continue;
  58. }
  59. $securityIdentity = null;
  60. if ($dialog->askConfirmation($output,"<question>Set an object owner?</question>\n", false)) {
  61. $username = $dialog->askAndValidate($output, 'Please enter the username: ', 'Sonata\DoctrineORMAdminBundle\Command\Validators::validateUsername');
  62. list($userBundle, $userEntity) = $dialog->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\DoctrineORMAdminBundle\Command\Validators::validateEntityName');
  63. // Entity exists?
  64. try {
  65. $userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
  66. } catch (\Exception $e) {
  67. $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
  68. continue;
  69. }
  70. $securityIdentity = new UserSecurityIdentity($username, $userEntityClass);
  71. }
  72. $securityHandler = $admin->getSecurityHandler();
  73. if (!$securityHandler instanceof AclSecurityHandler) {
  74. $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
  75. continue;
  76. }
  77. $em = $admin->getModelManager()->getEntityManager();
  78. $datagrid = $admin->getDatagrid();
  79. $datagrid->buildPager();
  80. $query = $datagrid->getQuery();
  81. $query->setFirstResult(null);
  82. $query->setMaxResults(null);
  83. $count = 0;
  84. $countUpdated = 0;
  85. $countAdded = 0;
  86. try {
  87. $batchSize = 20;
  88. $batchSizeOutput = 200;
  89. $i = 0;
  90. $oids = array();
  91. foreach ($query->getQuery()->iterate() as $row) {
  92. $oids[] = ObjectIdentity::fromDomainObject($row[0]);
  93. // detach from Doctrine, so that it can be Garbage-Collected immediately
  94. $em->detach($row[0]);
  95. if ((++$i % $batchSize) == 0) {
  96. list($batchAdded, $batchUpdated) = $this->configureObjectAcl($admin, $aclProvider, $oids, $securityHandler, $securityIdentity);
  97. $countAdded += $batchAdded;
  98. $countUpdated += $batchUpdated;
  99. $oids = array();
  100. }
  101. if ((++$i % $batchSizeOutput) == 0) {
  102. $output->writeln(sprintf(' - generated class ACEs for %s objects (added %s, updated %s)', $count, $countAdded, $countUpdated));
  103. }
  104. $count++;
  105. }
  106. if (count($oids) > 0) {
  107. list($batchAdded, $batchUpdated) = $this->configureObjectAcl($admin, $aclProvider, $oids, $securityHandler, $securityIdentity);
  108. $countAdded += $batchAdded;
  109. $countUpdated += $batchUpdated;
  110. }
  111. } catch ( \PDOException $e ) {
  112. throw new ModelManagerException('', 0, $e);
  113. }
  114. $output->writeln(sprintf(' - [TOTAL] generated class ACEs for %s objects (added %s, updated %s)', $count, $countAdded, $countUpdated));
  115. }
  116. }
  117. protected function configureObjectAcl(AdminInterface $admin, MutableAclProviderInterface $aclProvider, array $oids, SecurityHandlerInterface $securityHandler, UserSecurityIdentity $securityIdentity = null)
  118. {
  119. $countAdded = 0;
  120. $countUpdated = 0;
  121. // find object ACLs
  122. try {
  123. $acls = $aclProvider->findAcls($oids);
  124. } catch(\Exception $e) {
  125. if ($e instanceof NotAllAclsFoundException) {
  126. $acls = $e->getPartialResult();
  127. } elseif ($e instanceof AclNotFoundException) {
  128. // if only one oid, this error is thrown
  129. $acls = new \SplObjectStorage();
  130. } else {
  131. throw $e;
  132. }
  133. }
  134. foreach ($oids as $oid) {
  135. if ($acls->contains($oid)) {
  136. $acl = $acls->offsetGet($oid);
  137. $action = 'update';
  138. $countUpdated++;
  139. } else {
  140. $acl = $aclProvider->createAcl($oid);
  141. $action = 'add';
  142. $countAdded++;
  143. }
  144. if (!is_null($securityIdentity)) {
  145. // add object owner
  146. $securityHandler->addObjectOwner($acl, $securityIdentity);
  147. }
  148. $securityHandler->addObjectClassAces($acl, $securityHandler->buildSecurityInformation($admin));
  149. $aclProvider->updateAcl($acl);
  150. }
  151. return array($countAdded, $countUpdated);
  152. }
  153. }