ExpressionLanguageProvider.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\Component\Security\Core\Authorization;
  11. use Symfony\Component\ExpressionLanguage\ExpressionFunction;
  12. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  13. /**
  14. * Define some ExpressionLanguage functions.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class ExpressionLanguageProvider implements ExpressionFunctionProviderInterface
  19. {
  20. public function getFunctions()
  21. {
  22. return [
  23. new ExpressionFunction('is_anonymous', function () {
  24. return '$token && $auth_checker->isGranted("IS_ANONYMOUS")';
  25. }, function (array $variables) {
  26. return $variables['token'] && $variables['auth_checker']->isGranted('IS_ANONYMOUS');
  27. }),
  28. new ExpressionFunction('is_authenticated', function () {
  29. return '$token && !$auth_checker->isGranted("IS_ANONYMOUS")';
  30. }, function (array $variables) {
  31. return $variables['token'] && !$variables['auth_checker']->isGranted('IS_ANONYMOUS');
  32. }),
  33. new ExpressionFunction('is_fully_authenticated', function () {
  34. return '$token && $auth_checker->isGranted("IS_AUTHENTICATED_FULLY")';
  35. }, function (array $variables) {
  36. return $variables['token'] && $variables['auth_checker']->isGranted('IS_AUTHENTICATED_FULLY');
  37. }),
  38. new ExpressionFunction('is_granted', function ($attributes, $object = 'null') {
  39. return sprintf('$auth_checker->isGranted(%s, %s)', $attributes, $object);
  40. }, function (array $variables, $attributes, $object = null) {
  41. return $variables['auth_checker']->isGranted($attributes, $object);
  42. }),
  43. new ExpressionFunction('is_remember_me', function () {
  44. return '$token && $auth_checker->isGranted("IS_REMEMBERED")';
  45. }, function (array $variables) {
  46. return $variables['token'] && $variables['auth_checker']->isGranted('IS_REMEMBERED');
  47. }),
  48. ];
  49. }
  50. }