RoleSecurityHandler.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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 Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\SecurityContextInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  14. use Sonata\AdminBundle\Admin\AdminInterface;
  15. /**
  16. * Class RoleSecurityHandler
  17. *
  18. * @package Sonata\AdminBundle\Security\Handler
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class RoleSecurityHandler implements SecurityHandlerInterface
  22. {
  23. /**
  24. * @var AuthorizationCheckerInterface|SecurityContextInterface
  25. */
  26. protected $authorizationChecker;
  27. protected $superAdminRoles;
  28. /**
  29. * @param AuthorizationCheckerInterface|SecurityContextInterface $authorizationChecker
  30. * @param array $superAdminRoles
  31. *
  32. * @todo Go back to signature class check when bumping requirements to SF 2.6+
  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. try {
  54. return $this->authorizationChecker->isGranted($this->superAdminRoles)
  55. || $this->authorizationChecker->isGranted($attributes, $object);
  56. } catch (AuthenticationCredentialsNotFoundException $e) {
  57. return false;
  58. } catch (\Exception $e) {
  59. throw $e;
  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. }