AdminAclManipulator.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Security\Acl\Domain\RoleSecurityIdentity;
  12. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  13. use Symfony\Component\Security\Acl\Model\AclInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Sonata\AdminBundle\Admin\AdminInterface;
  16. use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
  17. class AdminAclManipulator implements AdminAclManipulatorInterface
  18. {
  19. protected $maskBuilderClass;
  20. /**
  21. * @param string $maskBuilderClass
  22. */
  23. public function __construct($maskBuilderClass)
  24. {
  25. $this->maskBuilderClass = $maskBuilderClass;
  26. }
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function configureAcls(OutputInterface $output, AdminInterface $admin)
  31. {
  32. $securityHandler = $admin->getSecurityHandler();
  33. if (!$securityHandler instanceof AclSecurityHandlerInterface) {
  34. $output->writeln(sprintf('Admin `%s` is not configured to use ACL : <info>ignoring</info>', $admin->getCode()));
  35. return;
  36. }
  37. $objectIdentity = ObjectIdentity::fromDomainObject($admin);
  38. $newAcl = false;
  39. if (is_null($acl = $securityHandler->getObjectAcl($objectIdentity))) {
  40. $acl = $securityHandler->createAcl($objectIdentity);
  41. $newAcl = true;
  42. }
  43. // create admin ACL
  44. $output->writeln(sprintf(' > install ACL for %s', $admin->getCode()));
  45. $configResult = $this->addAdminClassAces($output, $acl, $securityHandler, $securityHandler->buildSecurityInformation($admin));
  46. if ($configResult) {
  47. $securityHandler->updateAcl($acl);
  48. } else {
  49. $output->writeln(sprintf(' - %s , no roles and permissions found', ($newAcl ? 'skip' : 'removed')));
  50. $securityHandler->deleteAcl($objectIdentity);
  51. }
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. public function addAdminClassAces(OutputInterface $output, AclInterface $acl, AclSecurityHandlerInterface $securityHandler, array $roleInformation = array())
  57. {
  58. if (count($securityHandler->getAdminPermissions()) > 0 ) {
  59. $builder = new $this->maskBuilderClass();
  60. foreach ($roleInformation as $role => $permissions) {
  61. $aceIndex = $securityHandler->findClassAceIndexByRole($acl, $role);
  62. $roleAdminPermissions = array();
  63. foreach ($permissions as $permission) {
  64. // add only the admin permissions
  65. if (in_array($permission, $securityHandler->getAdminPermissions())) {
  66. $builder->add($permission);
  67. $roleAdminPermissions[] = $permission;
  68. }
  69. }
  70. if (count($roleAdminPermissions) > 0) {
  71. if ($aceIndex === false) {
  72. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  73. $action = 'add';
  74. } else {
  75. $acl->updateClassAce($aceIndex, $builder->get());
  76. $action = 'update';
  77. }
  78. if (!is_null($output)) {
  79. $output->writeln(sprintf(' - %s role: %s, permissions: %s', $action, $role, json_encode($roleAdminPermissions)));
  80. }
  81. $builder->reset();
  82. } elseif ($aceIndex !== false) {
  83. $acl->deleteClassAce($aceIndex);
  84. if (!is_null($output)) {
  85. $output->writeln(sprintf(' - remove role: %s', $action, $role));
  86. }
  87. }
  88. }
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. }
  94. }