RoleSecurityHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. * NEXT_MAJOR: Go back to signature class check when bumping requirements to SF 2.6+.
  32. *
  33. * @param AuthorizationCheckerInterface|SecurityContextInterface $authorizationChecker
  34. * @param array $superAdminRoles
  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. $attributes[] = sprintf($this->getBaseRole($admin), 'ALL');
  56. try {
  57. return $this->authorizationChecker->isGranted($this->superAdminRoles)
  58. || $this->authorizationChecker->isGranted($attributes, $object);
  59. } catch (AuthenticationCredentialsNotFoundException $e) {
  60. return false;
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getBaseRole(AdminInterface $admin)
  67. {
  68. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function buildSecurityInformation(AdminInterface $admin)
  74. {
  75. return array();
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function createObjectSecurity(AdminInterface $admin, $object)
  81. {
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function deleteObjectSecurity(AdminInterface $admin, $object)
  87. {
  88. }
  89. }