SecurityHelper.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Bundle\SecurityBundle\Templating\Helper;
  11. use Symfony\Component\Security\Acl\Voter\FieldVote;
  12. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  13. use Symfony\Component\Templating\Helper\Helper;
  14. /**
  15. * SecurityHelper provides read-only access to the security checker.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class SecurityHelper extends Helper
  20. {
  21. private $securityChecker;
  22. public function __construct(AuthorizationCheckerInterface $securityChecker = null)
  23. {
  24. $this->securityChecker = $securityChecker;
  25. }
  26. public function isGranted($role, $object = null, $field = null)
  27. {
  28. if (null === $this->securityChecker) {
  29. return false;
  30. }
  31. if (null !== $field) {
  32. $object = new FieldVote($object, $field);
  33. }
  34. return $this->securityChecker->isGranted($role, $object);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getName()
  40. {
  41. return 'security';
  42. }
  43. }