RoleSecurityHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  17. */
  18. class RoleSecurityHandler implements SecurityHandlerInterface
  19. {
  20. /**
  21. * @var AuthorizationCheckerInterface|SecurityContextInterface
  22. */
  23. protected $authorizationChecker;
  24. /**
  25. * @var array
  26. */
  27. protected $superAdminRoles;
  28. /**
  29. * NEXT_MAJOR: Go back to signature class check when bumping requirements to SF 2.6+.
  30. *
  31. * @param AuthorizationCheckerInterface|SecurityContextInterface $authorizationChecker
  32. * @param array $superAdminRoles
  33. */
  34. public function __construct($authorizationChecker, array $superAdminRoles)
  35. {
  36. if (!$authorizationChecker instanceof AuthorizationCheckerInterface && !$authorizationChecker instanceof SecurityContextInterface) {
  37. throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface or Symfony\Component\Security\Core\SecurityContextInterface');
  38. }
  39. $this->authorizationChecker = $authorizationChecker;
  40. $this->superAdminRoles = $superAdminRoles;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  46. {
  47. if (!is_array($attributes)) {
  48. $attributes = array($attributes);
  49. }
  50. foreach ($attributes as $pos => $attribute) {
  51. $attributes[$pos] = sprintf($this->getBaseRole($admin), $attribute);
  52. }
  53. $allRole = sprintf($this->getBaseRole($admin), 'ALL');
  54. try {
  55. return $this->authorizationChecker->isGranted($this->superAdminRoles)
  56. || $this->authorizationChecker->isGranted($attributes, $object)
  57. || $this->authorizationChecker->isGranted(array($allRole), $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. }