Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. How to use Expressions in Security, Routing, Services, and Validation
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia

Table of Contents

  • Security: Complex Access Controls with Expressions
  • Learn more

How to use Expressions in Security, Routing, Services, and Validation

Edit this page

Warning: You are browsing the documentation for Symfony 3.2, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

How to use Expressions in Security, Routing, Services, and Validation

Symfony comes with a powerful ExpressionLanguage component. It allows you to add highly customized logic inside configuration.

The Symfony Framework leverages expressions out of the box in the following ways:

  • Configuring services;
  • Route matching conditions;
  • Checking security (explained below) and access controls with allow_if;
  • Validation.

For more information about how to create and work with expressions, see The Expression Syntax.

Security: Complex Access Controls with Expressions

In addition to a role like ROLE_ADMIN, the isGranted() method also accepts an Expression object:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\ExpressionLanguage\Expression;
// ...

public function indexAction()
{
    $this->denyAccessUnlessGranted(new Expression(
        '"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())'
    ));

    // ...
}

In this example, if the current user has ROLE_ADMIN or if the current user object's isSuperAdmin() method returns true, then access will be granted (note: your User object may not have an isSuperAdmin() method, that method is invented for this example).

This uses an expression and you can learn more about the expression language syntax, see The Expression Syntax.

Inside the expression, you have access to a number of variables:

user
The user object (or the string anon if you're not authenticated).
roles
The array of roles the user has, including from the role hierarchy but not including the IS_AUTHENTICATED_* attributes (see the functions below).
object
The object (if any) that's passed as the second argument to isGranted().
token
The token object.
trust_resolver
The AuthenticationTrustResolverInterface, object: you'll probably use the is_*() functions below instead.

Additionally, you have access to a number of functions inside the expression:

is_authenticated
Returns true if the user is authenticated via "remember-me" or authenticated "fully" - i.e. returns true if the user is "logged in".
is_anonymous
Equal to using IS_AUTHENTICATED_ANONYMOUSLY with the isGranted() function.
is_remember_me
Similar, but not equal to IS_AUTHENTICATED_REMEMBERED, see below.
is_fully_authenticated
Similar, but not equal to IS_AUTHENTICATED_FULLY, see below.
has_role
Checks to see if the user has the given role - equivalent to an expression like 'ROLE_ADMIN' in roles.

is_remember_me is different than checking IS_AUTHENTICATED_REMEMBERED

The is_remember_me() and is_authenticated_fully() functions are similar to using IS_AUTHENTICATED_REMEMBERED and IS_AUTHENTICATED_FULLY with the isGranted() function - but they are not the same. The following shows the difference:

1
2
3
4
5
6
7
8
9
use Symfony\Component\ExpressionLanguage\Expression;
// ...

$ac = $this->get('security.authorization_checker');
$access1 = $ac->isGranted('IS_AUTHENTICATED_REMEMBERED');

$access2 = $ac->isGranted(new Expression(
    'is_remember_me() or is_fully_authenticated()'
));

Here, $access1 and $access2 will be the same value. Unlike the behavior of IS_AUTHENTICATED_REMEMBERED and IS_AUTHENTICATED_FULLY, the is_remember_me() function only returns true if the user is authenticated via a remember-me cookie and is_fully_authenticated only returns true if the user has actually logged in during this session (i.e. is full-fledged).

Learn more

  • How to Inject Values Based on Complex Expressions
  • Expression
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of mimol91, a Symfony contributor

Thanks mimol91 for being a Symfony contributor

1 commit • 2 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia