Security: Complex Access Controls with Expressions

Security: Complex Access Controls with Expressions

See also

The best solution for handling complex authorization rules is to use the Voter System.

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

  1. use Symfony\Component\ExpressionLanguage\Expression;
  2. // ...
  3. public function index(): Response
  4. {
  5. $this->denyAccessUnlessGranted(new Expression(
  6. '"ROLE_ADMIN" in roles or (not is_anonymous() and user.isSuperAdmin())'
  7. ));
  8. // ...
  9. }

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. This array includes any roles granted indirectly via the role hierarchy but it does not include the IS_AUTHENTICATED_* attributes (see the functions below).

role_names

An array with the string representation of the roles the user has. This array includes any roles granted indirectly via the role hierarchy but it does not include the IS_AUTHENTICATED_* attributes (see the functions below).

object

The object (if any) that’s passed as the second argument to isGranted().

subject

It stores the same value as object, so they are equivalent.

token

The token object.

trust_resolver

The Symfony\Component\Security\Core\Authentication\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()

Returns true if the user is anonymous. That is, the firewall confirms that it does not know this user’s identity. This is different from IS_AUTHENTICATED_ANONYMOUSLY, which is granted to all users, including authenticated ones.

is_remember_me()

Similar, but not equal to IS_AUTHENTICATED_REMEMBERED, see below.

is_fully_authenticated()

Equal to checking if the user has the IS_AUTHENTICATED_FULLY role.

is_granted()

Checks if the user has the given permission. Optionally accepts a second argument with the object where permission is checked on. It’s equivalent to using the isGranted() method from the authorization checker service.

is_remember_me() is different than checking IS_AUTHENTICATED_REMEMBERED

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

  1. use Symfony\Component\ExpressionLanguage\Expression;
  2. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  3. // ...
  4. public function index(AuthorizationCheckerInterface $authorizationChecker): Response
  5. {
  6. $access1 = $authorizationChecker->isGranted('IS_AUTHENTICATED_REMEMBERED');
  7. $access2 = $authorizationChecker->isGranted(new Expression(
  8. 'is_remember_me() or is_fully_authenticated()'
  9. ));
  10. }

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

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.