RoleSecurityHandler.php 3.0 KB

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