ObjectAclManipulator.php 3.0 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\DoctrineORMAdminBundle\Util;
  11. use Sonata\AdminBundle\Exception\ModelManagerException;
  12. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  13. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  16. use Sonata\AdminBundle\Admin\AdminInterface;
  17. use Sonata\AdminBundle\Util\ObjectAclManipulator as BaseObjectAclManipulator;
  18. class ObjectAclManipulator extends BaseObjectAclManipulator
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function batchConfigureAcls(OutputInterface $output, AdminInterface $admin, UserSecurityIdentity $securityIdentity = null)
  24. {
  25. $securityHandler = $admin->getSecurityHandler();
  26. if (!$securityHandler instanceof AclSecurityHandlerInterface) {
  27. $output->writeln('Admin class is not configured to use ACL : <info>ignoring</info>');
  28. return;
  29. }
  30. $em = $admin->getModelManager()->getEntityManager();
  31. $datagrid = $admin->getDatagrid();
  32. $datagrid->buildPager();
  33. $query = $datagrid->getQuery();
  34. $query->setFirstResult(null);
  35. $query->setMaxResults(null);
  36. $count = 0;
  37. $countUpdated = 0;
  38. $countAdded = 0;
  39. try {
  40. $batchSize = 20;
  41. $batchSizeOutput = 200;
  42. $i = 0;
  43. $oids = array();
  44. foreach ($query->getQuery()->iterate() as $row) {
  45. $oids[] = ObjectIdentity::fromDomainObject($row[0]);
  46. // detach from Doctrine, so that it can be Garbage-Collected immediately
  47. $em->detach($row[0]);
  48. if ((++$i % $batchSize) == 0) {
  49. list($batchAdded, $batchUpdated) = $this->configureAcls($admin, $oids, $securityIdentity);
  50. $countAdded += $batchAdded;
  51. $countUpdated += $batchUpdated;
  52. $oids = array();
  53. }
  54. if ((++$i % $batchSizeOutput) == 0) {
  55. $output->writeln(sprintf(' - generated class ACEs for %s objects (added %s, updated %s)', $count, $countAdded, $countUpdated));
  56. }
  57. $count++;
  58. }
  59. if (count($oids) > 0) {
  60. list($batchAdded, $batchUpdated) = $this->configureAcls($admin, $oids, $securityIdentity);
  61. $countAdded += $batchAdded;
  62. $countUpdated += $batchUpdated;
  63. }
  64. } catch ( \PDOException $e ) {
  65. throw new ModelManagerException('', 0, $e);
  66. }
  67. $output->writeln(sprintf(' - [TOTAL] generated class ACEs for %s objects (added %s, updated %s)', $count, $countAdded, $countUpdated));
  68. }
  69. }