RoleSecurityHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. $allRole = sprintf($this->getBaseRole($admin), 'ALL');
  56. try {
  57. return $this->authorizationChecker->isGranted($this->superAdminRoles)
  58. || $this->authorizationChecker->isGranted($attributes, $object)
  59. || $this->authorizationChecker->isGranted(array($allRole), $object);
  60. } catch (AuthenticationCredentialsNotFoundException $e) {
  61. return false;
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getBaseRole(AdminInterface $admin)
  68. {
  69. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function buildSecurityInformation(AdminInterface $admin)
  75. {
  76. return array();
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function createObjectSecurity(AdminInterface $admin, $object)
  82. {
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function deleteObjectSecurity(AdminInterface $admin, $object)
  88. {
  89. }
  90. }