ObjectAclManipulator.php 2.5 KB

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