ObjectAclManipulator.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project 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\Util;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  15. /**
  16. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  17. */
  18. abstract class ObjectAclManipulator implements ObjectAclManipulatorInterface
  19. {
  20. /**
  21. * Configure the object ACL for the passed object identities.
  22. *
  23. * @param OutputInterface $output
  24. * @param AdminInterface $admin
  25. * @param \Traversable $oids a collection of ObjectIdentityInterface implementations
  26. * @param UserSecurityIdentity $securityIdentity
  27. *
  28. * @throws \Exception
  29. *
  30. * @return array [countAdded, countUpdated]
  31. */
  32. public function configureAcls(OutputInterface $output, AdminInterface $admin, \Traversable $oids, UserSecurityIdentity $securityIdentity = null)
  33. {
  34. $countAdded = 0;
  35. $countUpdated = 0;
  36. $securityHandler = $admin->getSecurityHandler();
  37. if (!$securityHandler instanceof AclSecurityHandlerInterface) {
  38. $output->writeln(sprintf('Admin `%s` is not configured to use ACL : <info>ignoring</info>', $admin->getCode()));
  39. return array(0, 0);
  40. }
  41. $acls = $securityHandler->findObjectAcls($oids);
  42. foreach ($oids as $oid) {
  43. if ($acls->contains($oid)) {
  44. $acl = $acls->offsetGet($oid);
  45. ++$countUpdated;
  46. } else {
  47. $acl = $securityHandler->createAcl($oid);
  48. ++$countAdded;
  49. }
  50. if (!is_null($securityIdentity)) {
  51. // add object owner
  52. $securityHandler->addObjectOwner($acl, $securityIdentity);
  53. }
  54. $securityHandler->addObjectClassAces($acl, $securityHandler->buildSecurityInformation($admin));
  55. try {
  56. $securityHandler->updateAcl($acl);
  57. } catch (\Exception $e) {
  58. $output->writeln(sprintf('Error saving ObjectIdentity (%s, %s) ACL : %s <info>ignoring</info>', $oid->getIdentifier(), $oid->getType(), $e->getMessage()));
  59. }
  60. }
  61. return array($countAdded, $countUpdated);
  62. }
  63. }