ObjectAclManipulator.php 2.5 KB

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