ObjectAclManipulator.php 2.6 KB

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