ObjectAclManipulator.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode()));
  31. $objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner';
  32. $em = $admin->getModelManager()->getEntityManager($admin->getClass());
  33. $qb = $em->createQueryBuilder();
  34. $qb->select('o')->from($admin->getClass(), 'o');
  35. $count = 0;
  36. $countUpdated = 0;
  37. $countAdded = 0;
  38. try {
  39. $batchSize = 20;
  40. $batchSizeOutput = 200;
  41. $i = 0;
  42. $oids = array();
  43. foreach ($qb->getQuery()->iterate() as $row) {
  44. $oids[] = ObjectIdentity::fromDomainObject($row[0]);
  45. // detach from Doctrine, so that it can be Garbage-Collected immediately
  46. $em->detach($row[0]);
  47. $count++;
  48. if (($count % $batchSize) == 0) {
  49. list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $oids, $securityIdentity);
  50. $countAdded += $batchAdded;
  51. $countUpdated += $batchUpdated;
  52. $oids = array();
  53. }
  54. if (($count % $batchSizeOutput) == 0) {
  55. $output->writeln(sprintf(' - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
  56. }
  57. }
  58. if (count($oids) > 0) {
  59. list($batchAdded, $batchUpdated) = $this->configureAcls($output, $admin, $oids, $securityIdentity);
  60. $countAdded += $batchAdded;
  61. $countUpdated += $batchUpdated;
  62. }
  63. } catch (\PDOException $e) {
  64. throw new ModelManagerException('', 0, $e);
  65. }
  66. $output->writeln(sprintf(' - [TOTAL] generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
  67. }
  68. }