ObjectAclManipulator.php 3.1 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. $output->writeln(sprintf(' > generate ACLs for %s', $admin->getCode()));
  31. $objectOwnersMsg = is_null($securityIdentity) ? '' : ' and set the object owner';
  32. $em = $admin->getModelManager()->getEntityManager();
  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. if ((++$i % $batchSize) == 0) {
  48. list($batchAdded, $batchUpdated) = $this->configureAcls($admin, $oids, $securityIdentity);
  49. $countAdded += $batchAdded;
  50. $countUpdated += $batchUpdated;
  51. $oids = array();
  52. }
  53. if ((++$i % $batchSizeOutput) == 0) {
  54. $output->writeln(sprintf(' - generated class ACEs%s for %s objects (added %s, updated %s)', $objectOwnersMsg, $count, $countAdded, $countUpdated));
  55. }
  56. $count++;
  57. }
  58. if (count($oids) > 0) {
  59. list($batchAdded, $batchUpdated) = $this->configureAcls($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. }