RoleSecurityHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Security\Handler;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  14. use Symfony\Component\Security\Core\SecurityContextInterface;
  15. /**
  16. * Class RoleSecurityHandler.
  17. *
  18. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  19. */
  20. class RoleSecurityHandler implements SecurityHandlerInterface
  21. {
  22. /**
  23. * @var AuthorizationCheckerInterface|SecurityContextInterface
  24. */
  25. protected $authorizationChecker;
  26. /**
  27. * @var array
  28. */
  29. protected $superAdminRoles;
  30. /**
  31. * @param AuthorizationCheckerInterface|SecurityContextInterface $authorizationChecker
  32. * @param array $superAdminRoles
  33. *
  34. * @todo Go back to signature class check when bumping requirements to SF 2.6+
  35. */
  36. public function __construct($authorizationChecker, array $superAdminRoles)
  37. {
  38. if (!$authorizationChecker instanceof AuthorizationCheckerInterface && !$authorizationChecker instanceof SecurityContextInterface) {
  39. throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface or Symfony\Component\Security\Core\SecurityContextInterface');
  40. }
  41. $this->authorizationChecker = $authorizationChecker;
  42. $this->superAdminRoles = $superAdminRoles;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  48. {
  49. if (!is_array($attributes)) {
  50. $attributes = array($attributes);
  51. }
  52. foreach ($attributes as $pos => $attribute) {
  53. $attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
  54. }
  55. try {
  56. return $this->authorizationChecker->isGranted($this->superAdminRoles)
  57. || $this->authorizationChecker->isGranted($attributes, $object);
  58. } catch (AuthenticationCredentialsNotFoundException $e) {
  59. return false;
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getBaseRole(AdminInterface $admin)
  66. {
  67. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function buildSecurityInformation(AdminInterface $admin)
  73. {
  74. return array();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function createObjectSecurity(AdminInterface $admin, $object)
  80. {
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function deleteObjectSecurity(AdminInterface $admin, $object)
  86. {
  87. }
  88. }