Twig: if is_granted('ROLE_MANAGER') check is not granted

Twig: if is_granted('ROLE_MANAGER') check is not granted

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The is_NOT_granted Fallacy in Twig

When building role-based access control (RBAC) interfaces, it is tempting to reach for a function that feels like its opposite. If you have {% if is_granted('ROLE_USER') %}, you naturally expect a companion function called {% if is_NOT_granted('ROLE_MANAGER') %} to handle exclusions. The desire for this syntax stems from readability; avoiding the negation of a negative condition inside a logic block seems intuitive. However, in Twig's security context, this specific function simply does not exist.

The core issue with your current implementation—nesting an empty {% if is_granted('ROLE_MANAGER') %} block solely to trigger an else clause—is that it relies on the absence of code rather than a logical check. While it functions, it introduces visual noise and obscures the intent of the template. You are essentially saying, "If the user is a manager, render nothing; otherwise, show this message." This is functionally correct but semantically awkward when you specifically want to target users who lack that permission.

The Logical Inversion

To achieve the logic of "display only if not granted," you must invert the condition within the existing is_granted helper. Twig's security context provides a boolean return value for every check. Therefore, the negation operator not is the standard tool for this job.

Instead of searching for a non-existent {% if is_NOT_granted('ROLE_MANAGER') %}, you apply the logical NOT operator directly to the function call:

{% if not is_granted('ROLE_MANAGER') %}
    {# This block renders only if the user is NOT a manager #}
    <p>Content reserved for users without Manager privileges.</p>
{% endif %}

This approach leverages Twig's short-circuit evaluation. If is_granted('ROLE_MANAGER') returns true, the not operator flips it to false, preventing the block from rendering. If the user is a standard USER, the function returns false, the not operator flips it to true, and your message appears exactly as intended.

Handling Multiple Roles

The is_granted method checks for specific role names against the security context. In Symfony-based applications, which often use Twig templates, roles are frequently stored in a flat array like ['ROLE_USER']. Sometimes, however, you might need to check if a user has any of several roles before excluding them, or if they lack all of a specific group.

If your application uses the standard Symfony security component, you can pass an array to is_granted to check for membership in a role hierarchy:

{% if not is_granted(['ROLE_ADMIN', 'ROLE_MANAGER']) %}
    {# User does not have Admin or Manager access #}
    <p>Please upgrade your account.</p>
{% endif %}

In this scenario, is_granted returns true if the user possesses at least one of the roles listed. Consequently, the not operator ensures the block only renders when the user has neither role. This pattern is cleaner than attempting to enumerate every non-manager role in a negative check.

Performance and Readability Trade-offs

While adding a not operator adds negligible overhead compared to rendering HTML, readability is the primary driver for this syntax choice. The original nested structure you proposed forces the reader to mentally parse an empty block. By moving the negation into the condition itself, the intent becomes immediate: "Show this if they are not authorized."

There is a slight trade-off to consider regarding role naming conventions. In Symfony 5.3 and later, role names are often stored without the ROLE_ prefix (e.g., 'manager' instead of 'ROLE_MANAGER') when using the new authorization voter system or specific bundle configurations. If your application has migrated to this convention, ensure your checks align with the actual attribute names defined in your security configuration files. Using an incorrect role name will result in is_granted returning false for everyone, effectively bypassing your access control logic entirely.

Conclusion

There is no need to invent custom helpers or seek out missing functions like is_NOT_granted. Twig's existing boolean logic combined with the not operator provides a robust, readable solution for exclusionary checks. By restructuring your template to check for the absence of authority directly, you maintain clarity and adhere to standard Symfony security patterns.

Our network