SecurityExtension.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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\Bridge\Twig\Extension;
  11. use Symfony\Component\Security\Acl\Voter\FieldVote;
  12. use Symfony\Component\Security\Core\SecurityContextInterface;
  13. /**
  14. * SecurityExtension exposes security context features.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class SecurityExtension extends \Twig_Extension
  19. {
  20. private $context;
  21. public function __construct(SecurityContextInterface $context = null)
  22. {
  23. $this->context = $context;
  24. }
  25. public function isGranted($role, $object = null, $field = null)
  26. {
  27. if (null === $this->context) {
  28. return false;
  29. }
  30. if (null !== $field) {
  31. $object = new FieldVote($object, $field);
  32. }
  33. return $this->context->isGranted($role, $object);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getFunctions()
  39. {
  40. return array(
  41. new \Twig_SimpleFunction('is_granted', array($this, 'isGranted')),
  42. );
  43. }
  44. /**
  45. * Returns the name of the extension.
  46. *
  47. * @return string The extension name
  48. */
  49. public function getName()
  50. {
  51. return 'security';
  52. }
  53. }