SecurityExtension.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Symfony\Bundle\TwigBundle\Extension;
  3. use Symfony\Bundle\TwigBundle\TokenParser\IfRoleTokenParser;
  4. use Symfony\Component\Security\SecurityContext;
  5. /*
  6. * This file is part of the Symfony package.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. *
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class SecurityExtension extends \Twig_Extension
  18. {
  19. protected $context;
  20. public function __construct(SecurityContext $context = null)
  21. {
  22. $this->context = $context;
  23. }
  24. public function vote($role, $object = null)
  25. {
  26. if (null === $this->context) {
  27. return false;
  28. }
  29. return $this->context->vote($role, $object);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getGlobals()
  35. {
  36. return array(
  37. 'fn_has_role' => new \Twig_Function($this, 'vote'),
  38. );
  39. }
  40. /**
  41. * Returns the name of the extension.
  42. *
  43. * @return string The extension name
  44. */
  45. public function getName()
  46. {
  47. return 'security';
  48. }
  49. }